1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #18082 from MHBauer/gh9406

Remove defaults for flags/options that expect no value
This commit is contained in:
Alexander Morozov 2015-11-19 15:06:21 -08:00
commit 9c21e852b2
2 changed files with 22 additions and 3 deletions

View file

@ -126,7 +126,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
out, stderr, _, err = runCommandWithStdoutStderr(helpCmd) out, stderr, _, err = runCommandWithStdoutStderr(helpCmd)
c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr)) c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr))
c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd)) c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd))
c.Assert(out, checker.Contains, "--help=false", check.Commentf("Should show full usage on %q\n", cmd)) c.Assert(out, checker.Contains, "--help", check.Commentf("All commands should mention '--help'. Command '%v' did not.\n", cmd))
c.Assert(err, checker.IsNil, check.Commentf(out)) c.Assert(err, checker.IsNil, check.Commentf(out))

View file

@ -520,6 +520,20 @@ func Set(name, value string) error {
return CommandLine.Set(name, value) return CommandLine.Set(name, value)
} }
// isZeroValue guesses whether the string represents the zero
// value for a flag. It is not accurate but in practice works OK.
func isZeroValue(value string) bool {
switch value {
case "false":
return true
case "":
return true
case "0":
return true
}
return false
}
// PrintDefaults prints, to standard error unless configured // PrintDefaults prints, to standard error unless configured
// otherwise, the default values of all defined flags in the set. // otherwise, the default values of all defined flags in the set.
func (fs *FlagSet) PrintDefaults() { func (fs *FlagSet) PrintDefaults() {
@ -537,7 +551,6 @@ func (fs *FlagSet) PrintDefaults() {
} }
fs.VisitAll(func(flag *Flag) { fs.VisitAll(func(flag *Flag) {
format := " -%s=%s"
names := []string{} names := []string{}
for _, name := range flag.Names { for _, name := range flag.Names {
if name[0] != '#' { if name[0] != '#' {
@ -551,7 +564,13 @@ func (fs *FlagSet) PrintDefaults() {
val = homedir.GetShortcutString() + val[len(home):] val = homedir.GetShortcutString() + val[len(home):]
} }
if isZeroValue(val) {
format := " -%s"
fmt.Fprintf(writer, format, strings.Join(names, ", -"))
} else {
format := " -%s=%s"
fmt.Fprintf(writer, format, strings.Join(names, ", -"), val) fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
}
for i, line := range strings.Split(flag.Usage, "\n") { for i, line := range strings.Split(flag.Usage, "\n") {
if i != 0 { if i != 0 {
line = " " + line line = " " + line