diff options
| author | Giovanni Bajo <[email protected]> | 2015-10-24 12:54:09 +0200 |
|---|---|---|
| committer | Steve Francia <[email protected]> | 2015-12-18 08:47:03 -0500 |
| commit | 7f60f83a2c81bc3c3c0d5297f61ddfa68da9d3b7 (patch) | |
| tree | b72931e6be5530540908afd5175e97d9cb9a0915 /flag_test.go | |
| parent | 76a42eaba0307accce867b21d302cc6586bc3ad3 (diff) | |
Issue #55: implement Go 1.5-style default usage formatting.
Compare to 1.4, the main difference in formatting is
the placement of default values. Moreover, UnquoteUsage()
is now added (and exported, for full API compatibility
with the standard flag package), so backtick words in
usage messages can be used to set the placeholder name.
Compared to the standard flag package, this patch
always prints usage in one-line, with automatic
alignment, because I feel that the 1.4 output is very
confusing when modified to include also dash-dash flags.
Diffstat (limited to 'flag_test.go')
| -rw-r--r-- | flag_test.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go index e17b2aa..0ae2e4f 100644 --- a/flag_test.go +++ b/flag_test.go @@ -872,3 +872,42 @@ func TestHiddenFlagUsage(t *testing.T) { t.Errorf("usage message printed when using a hidden flag!") } } + +const defaultOutput = ` --A for bootstrapping, allow 'any' type + --Alongflagname disable bounds checking + -C, --CCC a boolean defaulting to true (default true) + --D path set relative path for local imports + --F number a non-zero number (default 2.7) + --G float a float that defaults to zero + --N int a non-zero int (default 27) + --ND1 string[="bar"] a string with NoOptDefVal (default "foo") + --ND2 num[=4321] a num with NoOptDefVal (default 1234) + --Z int an int that defaults to zero + --maxT timeout set timeout for dial +` + +func TestPrintDefaults(t *testing.T) { + fs := NewFlagSet("print defaults test", ContinueOnError) + var buf bytes.Buffer + fs.SetOutput(&buf) + fs.Bool("A", false, "for bootstrapping, allow 'any' type") + fs.Bool("Alongflagname", false, "disable bounds checking") + fs.BoolP("CCC", "C", true, "a boolean defaulting to true") + fs.String("D", "", "set relative `path` for local imports") + fs.Float64("F", 2.7, "a non-zero `number`") + fs.Float64("G", 0, "a float that defaults to zero") + fs.Int("N", 27, "a non-zero int") + fs.Int("Z", 0, "an int that defaults to zero") + fs.Duration("maxT", 0, "set `timeout` for dial") + fs.String("ND1", "foo", "a string with NoOptDefVal") + fs.Lookup("ND1").NoOptDefVal = "bar" + fs.Int("ND2", 1234, "a `num` with NoOptDefVal") + fs.Lookup("ND2").NoOptDefVal = "4321" + fs.PrintDefaults() + got := buf.String() + if got != defaultOutput { + fmt.Println("\n" + got) + fmt.Println("\n" + defaultOutput) + t.Errorf("got %q want %q\n", got, defaultOutput) + } +} |
