diff --git a/public_api_test.go b/public_api_test.go index 8792e78..fc0ee02 100644 --- a/public_api_test.go +++ b/public_api_test.go @@ -2266,6 +2266,9 @@ OPTIONS: if opt.Help() != expectedSynopsis+expectedCommandList+expectedOptionList { t.Errorf("Unexpected help:\n---\n%s\n---\n", opt.Help()) } + if opt.GetCommandName() != "go-getoptions.test" { + t.Errorf("Unexpected command name: %s", opt.GetCommandName()) + } }) t.Run("", func(t *testing.T) { @@ -2913,8 +2916,10 @@ Use 'go-getoptions.test help ' for extra details. t.Run("command", func(t *testing.T) { called := false + commandName := "" fn := func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { called = true + commandName = opt.GetCommandName() return nil } opt := getoptions.New() @@ -2931,6 +2936,37 @@ Use 'go-getoptions.test help ' for extra details. if !called { t.Errorf("Fn not called") } + if commandName != "command" { + t.Errorf("Unexpected command name: %s", commandName) + } + }) + + t.Run("sub-command", func(t *testing.T) { + called := false + commandName := "" + fn := func(ctx context.Context, opt *getoptions.GetOpt, args []string) error { + called = true + commandName = opt.GetCommandName() + return nil + } + opt := getoptions.New() + cmd := opt.NewCommand("command", "") + cmd.NewCommand("sub-command", "").SetCommandFn(fn) + opt.HelpCommand("help") + remaining, err := opt.Parse([]string{"command", "sub-command"}) + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + err = opt.Dispatch(context.Background(), remaining) + if err != nil { + t.Errorf("Unexpected error: %s", err) + } + if !called { + t.Errorf("Fn not called") + } + if commandName != "sub-command" { + t.Errorf("Unexpected command name: %s", commandName) + } }) t.Run("command with missing fn", func(t *testing.T) { diff --git a/user.go b/user.go index 63bccd7..d2b622c 100644 --- a/user.go +++ b/user.go @@ -247,6 +247,11 @@ func (gopt *GetOpt) NewCommand(name string, description string) *GetOpt { return cmd } +// GetCommandName - Returns the current command name. +func (gopt *GetOpt) GetCommandName() string { + return gopt.programTree.Name +} + func copyOptionsFromParent(parent *programTree) { for k, v := range parent.ChildOptions { for _, command := range parent.ChildCommands {