Browse code

Remove defaults for flags/options that expect no value

- isZeroValue function from upstream go
- covers booleans, strings and numbers
- change integration to reflect new behavior
- resolves #9406

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>

Morgan Bauer authored on 2015/11/19 11:27:59
Showing 2 changed files
... ...
@@ -126,7 +126,7 @@ func (s *DockerSuite) TestHelpTextVerify(c *check.C) {
126 126
 			out, stderr, _, err = runCommandWithStdoutStderr(helpCmd)
127 127
 			c.Assert(len(stderr), checker.Equals, 0, check.Commentf("Error on %q help. non-empty stderr:%q", cmd, stderr))
128 128
 			c.Assert(out, checker.Not(checker.HasSuffix), "\n\n", check.Commentf("Should not have blank line on %q\n", cmd))
129
-			c.Assert(out, checker.Contains, "--help=false", check.Commentf("Should show full usage on %q\n", cmd))
129
+			c.Assert(out, checker.Contains, "--help", check.Commentf("All commands should mention '--help'. Command '%v' did not.\n", cmd))
130 130
 
131 131
 			c.Assert(err, checker.IsNil, check.Commentf(out))
132 132
 
... ...
@@ -520,6 +520,20 @@ func Set(name, value string) error {
520 520
 	return CommandLine.Set(name, value)
521 521
 }
522 522
 
523
+// isZeroValue guesses whether the string represents the zero
524
+// value for a flag. It is not accurate but in practice works OK.
525
+func isZeroValue(value string) bool {
526
+	switch value {
527
+	case "false":
528
+		return true
529
+	case "":
530
+		return true
531
+	case "0":
532
+		return true
533
+	}
534
+	return false
535
+}
536
+
523 537
 // PrintDefaults prints, to standard error unless configured
524 538
 // otherwise, the default values of all defined flags in the set.
525 539
 func (fs *FlagSet) PrintDefaults() {
... ...
@@ -537,7 +551,6 @@ func (fs *FlagSet) PrintDefaults() {
537 537
 	}
538 538
 
539 539
 	fs.VisitAll(func(flag *Flag) {
540
-		format := "  -%s=%s"
541 540
 		names := []string{}
542 541
 		for _, name := range flag.Names {
543 542
 			if name[0] != '#' {
... ...
@@ -551,7 +564,13 @@ func (fs *FlagSet) PrintDefaults() {
551 551
 				val = homedir.GetShortcutString() + val[len(home):]
552 552
 			}
553 553
 
554
-			fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
554
+			if isZeroValue(val) {
555
+				format := "  -%s"
556
+				fmt.Fprintf(writer, format, strings.Join(names, ", -"))
557
+			} else {
558
+				format := "  -%s=%s"
559
+				fmt.Fprintf(writer, format, strings.Join(names, ", -"), val)
560
+			}
555 561
 			for i, line := range strings.Split(flag.Usage, "\n") {
556 562
 				if i != 0 {
557 563
 					line = "  " + line