mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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
4edcbfdeb7
commit
0e9c40eb82
1 changed files with 34 additions and 1 deletions
|
@ -51,6 +51,8 @@
|
||||||
Command line flag syntax:
|
Command line flag syntax:
|
||||||
-flag
|
-flag
|
||||||
-flag=x
|
-flag=x
|
||||||
|
-flag="x"
|
||||||
|
-flag='x'
|
||||||
-flag x // non-boolean flags only
|
-flag x // non-boolean flags only
|
||||||
One or two minus signs may be used; they are equivalent.
|
One or two minus signs may be used; they are equivalent.
|
||||||
The last form is not permitted for boolean flags because the
|
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.
|
// parseOne parses one flag. It reports whether a flag was seen.
|
||||||
func (f *FlagSet) parseOne() (bool, string, error) {
|
func (f *FlagSet) parseOne() (bool, string, error) {
|
||||||
if len(f.args) == 0 {
|
if len(f.args) == 0 {
|
||||||
|
@ -799,7 +832,7 @@ func (f *FlagSet) parseOne() (bool, string, error) {
|
||||||
value := ""
|
value := ""
|
||||||
for i := 1; i < len(name); i++ { // equals cannot be first
|
for i := 1; i < len(name); i++ { // equals cannot be first
|
||||||
if name[i] == '=' {
|
if name[i] == '=' {
|
||||||
value = name[i+1:]
|
value = trimQuotes(name[i+1:])
|
||||||
has_value = true
|
has_value = true
|
||||||
name = name[0:i]
|
name = name[0:i]
|
||||||
break
|
break
|
||||||
|
|
Loading…
Add table
Reference in a new issue