diff --git a/subcmd.go b/subcmd.go index ee5c303..09214f3 100644 --- a/subcmd.go +++ b/subcmd.go @@ -181,6 +181,7 @@ func rootName() string { return stripExeExt(exe) } +// FlagSet creates a new flag.FlagSet with name of subcommand. func FlagSet(ctx context.Context) *flag.FlagSet { name := strings.Join(Names(ctx), " ") return flag.NewFlagSet(name, flag.ExitOnError) diff --git a/subcmd_test.go b/subcmd_test.go index ba93f51..967cbc1 100644 --- a/subcmd_test.go +++ b/subcmd_test.go @@ -174,3 +174,34 @@ func TestRootSet(t *testing.T) { t.Errorf("unexpected name: -want +got\n%s", d) } } + +func TestFlagSet(t *testing.T) { + t.Run("no name", func(t *testing.T) { + ctx := context.Background() + fs := subcmd.FlagSet(ctx) + got := fs.Name() + if got != "" { + t.Errorf("wrong name of FlagSet: got=%q", got) + } + }) + + t.Run("with names", func(t *testing.T) { + var gotName string + set := subcmd.DefineSet("first", "", + subcmd.DefineSet("second", "", + subcmd.DefineCommand("third", "", func(ctx context.Context, args []string) error { + fs := subcmd.FlagSet(ctx) + gotName = fs.Name() + return nil + }), + ), + ) + err := subcmd.Run(set, "second", "third") + if err != nil { + t.Fatalf("failed: %s", err) + } + if gotName != "first second third" { + t.Errorf("wrong name of FlagSet: got=%q", gotName) + } + }) +}