From 7ec896dde0364a03270854d125a0eab8376babe5 Mon Sep 17 00:00:00 2001 From: Mike Gehard Date: Fri, 6 Sep 2013 09:36:57 -0600 Subject: [PATCH 1/2] Update flag.go Make boolean flags more in line with standard UNIX boolean flags. --- flag.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flag.go b/flag.go index 25fe778..f363f3d 100644 --- a/flag.go +++ b/flag.go @@ -22,7 +22,7 @@ type BoolFlag struct { } func (f BoolFlag) String() string { - return fmt.Sprintf("--%v\t%v", f.Name, f.Usage) + return fmt.Sprintf("-%v\t%v", f.Name, f.Usage) } func (f BoolFlag) Apply(set *flag.FlagSet) { From 1a1d6bd16378b1ce01c47a99fdbcb70a2cf3faf0 Mon Sep 17 00:00:00 2001 From: Mike Gehard Date: Mon, 9 Sep 2013 11:51:46 -0700 Subject: [PATCH 2/2] Vary flag prefix based on name length --- flag.go | 16 ++++++++++--- flag_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 flag_test.go diff --git a/flag.go b/flag.go index f363f3d..e37d2a0 100644 --- a/flag.go +++ b/flag.go @@ -22,7 +22,7 @@ type BoolFlag struct { } func (f BoolFlag) String() string { - return fmt.Sprintf("-%v\t%v", f.Name, f.Usage) + return fmt.Sprintf("%s%v\t%v", prefixFor(f.Name), f.Name, f.Usage) } func (f BoolFlag) Apply(set *flag.FlagSet) { @@ -36,7 +36,7 @@ type StringFlag struct { } func (f StringFlag) String() string { - return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage) + return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage) } func (f StringFlag) Apply(set *flag.FlagSet) { @@ -50,7 +50,7 @@ type IntFlag struct { } func (f IntFlag) String() string { - return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage) + return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, f.Value, f.Usage) } func (f IntFlag) Apply(set *flag.FlagSet) { @@ -69,3 +69,13 @@ func (f helpFlag) Apply(set *flag.FlagSet) { set.Bool("h", false, f.Usage) set.Bool("help", false, f.Usage) } + +func prefixFor(name string) (prefix string) { + if len(name) == 1 { + prefix = "-" + } else { + prefix = "--" + } + + return +} diff --git a/flag_test.go b/flag_test.go new file mode 100644 index 0000000..df99cba --- /dev/null +++ b/flag_test.go @@ -0,0 +1,67 @@ +package cli_test + +import ( + "github.com/codegangsta/cli" + "testing" +) + +var boolFlagTests = []struct { + name string + expected string +}{ + {"help", "--help\t"}, + {"h", "-h\t"}, +} + +func TestBoolFlagHelpOutput(t *testing.T) { + + for _, test := range boolFlagTests { + flag := cli.BoolFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + +var stringFlagTests = []struct { + name string + expected string +}{ + {"help", "--help ''\t"}, + {"h", "-h ''\t"}, +} + +func TestStringFlagHelpOutput(t *testing.T) { + + for _, test := range stringFlagTests { + flag := cli.StringFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} + + +var intFlagTests = []struct { + name string + expected string +}{ + {"help", "--help '0'\t"}, + {"h", "-h '0'\t"}, +} + +func TestIntFlagHelpOutput(t *testing.T) { + + for _, test := range intFlagTests { + flag := cli.IntFlag{Name: test.name} + output := flag.String() + + if output != test.expected { + t.Errorf("%s does not match %s", output, test.expected) + } + } +} \ No newline at end of file