This patch improves the mflag package to ensure that things arguments
to mflag such as `-flag="var"` or `-flag='var'` have the quotes
stripped from the value (to mirror the getopt functionality for similar
flags).
Docker-DCO-1.1-Signed-off-by: Aleksa Sarai <cyphar@cyphar.com> (github: cyphar)
| ... | ... |
@@ -51,6 +51,8 @@ |
| 51 | 51 |
Command line flag syntax: |
| 52 | 52 |
-flag |
| 53 | 53 |
-flag=x |
| 54 |
+ -flag="x" |
|
| 55 |
+ -flag='x' |
|
| 54 | 56 |
-flag x // non-boolean flags only |
| 55 | 57 |
One or two minus signs may be used; they are equivalent. |
| 56 | 58 |
The last form is not permitted for boolean flags because the |
| ... | ... |
@@ -775,6 +777,37 @@ func (f *FlagSet) usage() {
|
| 775 | 775 |
} |
| 776 | 776 |
} |
| 777 | 777 |
|
| 778 |
+func trimQuotes(str string) string {
|
|
| 779 |
+ type quote struct {
|
|
| 780 |
+ start, end byte |
|
| 781 |
+ } |
|
| 782 |
+ |
|
| 783 |
+ // All valid quote types. |
|
| 784 |
+ quotes := []quote{
|
|
| 785 |
+ // Double quotes |
|
| 786 |
+ {
|
|
| 787 |
+ start: '"', |
|
| 788 |
+ end: '"', |
|
| 789 |
+ }, |
|
| 790 |
+ |
|
| 791 |
+ // Single quotes |
|
| 792 |
+ {
|
|
| 793 |
+ start: '\'', |
|
| 794 |
+ end: '\'', |
|
| 795 |
+ }, |
|
| 796 |
+ } |
|
| 797 |
+ |
|
| 798 |
+ for _, quote := range quotes {
|
|
| 799 |
+ // Only strip if outermost match. |
|
| 800 |
+ if str[0] == quote.start && str[len(str)-1] == quote.end {
|
|
| 801 |
+ str = str[1 : len(str)-1] |
|
| 802 |
+ break |
|
| 803 |
+ } |
|
| 804 |
+ } |
|
| 805 |
+ |
|
| 806 |
+ return str |
|
| 807 |
+} |
|
| 808 |
+ |
|
| 778 | 809 |
// parseOne parses one flag. It reports whether a flag was seen. |
| 779 | 810 |
func (f *FlagSet) parseOne() (bool, string, error) {
|
| 780 | 811 |
if len(f.args) == 0 {
|
| ... | ... |
@@ -799,7 +832,7 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
| 799 | 799 |
value := "" |
| 800 | 800 |
for i := 1; i < len(name); i++ { // equals cannot be first
|
| 801 | 801 |
if name[i] == '=' {
|
| 802 |
- value = name[i+1:] |
|
| 802 |
+ value = trimQuotes(name[i+1:]) |
|
| 803 | 803 |
has_value = true |
| 804 | 804 |
name = name[0:i] |
| 805 | 805 |
break |