pkg: mflag: flag: make mflag strip quotes in -flag="var" forms
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)
This commit is contained in:
parent
1a38e6ff10
commit
4941e364a9
1 changed files with 34 additions and 1 deletions
|
@ -51,6 +51,8 @@
|
|||
Command line flag syntax:
|
||||
-flag
|
||||
-flag=x
|
||||
-flag="x"
|
||||
-flag='x'
|
||||
-flag x // non-boolean flags only
|
||||
One or two minus signs may be used; they are equivalent.
|
||||
The last form is not permitted for boolean flags because the
|
||||
|
@ -775,6 +777,37 @@ func (f *FlagSet) usage() {
|
|||
}
|
||||
}
|
||||
|
||||
func trimQuotes(str string) string {
|
||||
type quote struct {
|
||||
start, end byte
|
||||
}
|
||||
|
||||
// All valid quote types.
|
||||
quotes := []quote{
|
||||
// Double quotes
|
||||
{
|
||||
start: '"',
|
||||
end: '"',
|
||||
},
|
||||
|
||||
// Single quotes
|
||||
{
|
||||
start: '\'',
|
||||
end: '\'',
|
||||
},
|
||||
}
|
||||
|
||||
for _, quote := range quotes {
|
||||
// Only strip if outermost match.
|
||||
if str[0] == quote.start && str[len(str)-1] == quote.end {
|
||||
str = str[1 : len(str)-1]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
// parseOne parses one flag. It reports whether a flag was seen.
|
||||
func (f *FlagSet) parseOne() (bool, string, error) {
|
||||
if len(f.args) == 0 {
|
||||
|
@ -799,7 +832,7 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
|||
value := ""
|
||||
for i := 1; i < len(name); i++ { // equals cannot be first
|
||||
if name[i] == '=' {
|
||||
value = name[i+1:]
|
||||
value = trimQuotes(name[i+1:])
|
||||
has_value = true
|
||||
name = name[0:i]
|
||||
break
|
||||
|
|
Loading…
Reference in a new issue