From 5f365989439755579550ba61c141b265b66a29c4 Mon Sep 17 00:00:00 2001 From: Gustavo Sampaio Date: Thu, 3 Oct 2019 22:14:08 -0300 Subject: [PATCH] Take operands in account when configuring automatic help Fixes #5 Signed-off-by: Gustavo Sampaio --- .gitignore | 3 +++ cmd.go | 2 +- help_test.go | 10 ++++++++++ opt.go | 2 +- testdata/help-message-with-operands.golden | 7 +++++++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 testdata/help-message-with-operands.golden diff --git a/.gitignore b/.gitignore index 2598ac0..0be4bec 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # VSCode config .vscode + +# idea +.idea \ No newline at end of file diff --git a/cmd.go b/cmd.go index 8ee5fed..6c95053 100644 --- a/cmd.go +++ b/cmd.go @@ -140,7 +140,7 @@ func (cmd *Cmd) setupHelp() { return } - if (len(cmd.optentries) > 0 || len(cmd.commands) > 0) && cmd.shortopt["-h"] == nil { + if (len(cmd.optentries) > 0 || len(cmd.commands) > 0 || len(cmd.operands) > 0) && cmd.shortopt["-h"] == nil { cmd.Bool("help", "h", false, "Show this help message.") } } diff --git a/help_test.go b/help_test.go index 6907da2..9b7d5ba 100644 --- a/help_test.go +++ b/help_test.go @@ -109,6 +109,16 @@ func TestArgs(t *testing.T) { } } +func TestHelpMessageWithOperands(t *testing.T) { + app := libcmd.NewApp("app", "some brief description") + app.AddOperand("src", "?") + app.AddOperand("dst", "*") + + if err := compareHelpOutput(app, []string{"-h", "test", "test2"}, "testdata/help-message-with-operands.golden"); err != nil { + t.Error(err) + } +} + func TestArgsPartial(t *testing.T) { app := libcmd.NewApp("app", "some brief description") app.Long = "this is a very long description" diff --git a/opt.go b/opt.go index f3a2c57..ae6be23 100644 --- a/opt.go +++ b/opt.go @@ -150,7 +150,7 @@ func (cmd *Cmd) doParse(args []string) error { return unknownArgErr{arg: arg.name} } - // some argument types have autmatic values in certain cases + // some argument types have automatic values in certain cases // fill them in, if necessary entry.fillAutoValue(arg) diff --git a/testdata/help-message-with-operands.golden b/testdata/help-message-with-operands.golden new file mode 100644 index 0000000..5ba82e1 --- /dev/null +++ b/testdata/help-message-with-operands.golden @@ -0,0 +1,7 @@ +app - some brief description + +USAGE: app [OPTIONS...] [src] [dst...] + +Options: + -h, --help + Show this help message.