diff --git a/app.go b/app.go index 1d24234..bf05f64 100644 --- a/app.go +++ b/app.go @@ -111,7 +111,7 @@ func (a *App) list(ctx context.Context) error { } arr := &input.Array{} - arr.SetArgument("command_name", value.New(CommandList)) + arr.SetArgument(ArgumentCommandName, value.New(CommandList)) in := input.Chain(arr, a.in) return Run(ctx, cmd, in, a.out) diff --git a/command_test.go b/command_test.go index 91e4207..62e593e 100644 --- a/command_test.go +++ b/command_test.go @@ -43,6 +43,7 @@ func Command() *console.Command { option.String("string", "array string", option.Array), option.Bool("bool", "test bool option"), option.Duration("duration", "test duration with default", option.Default(time.Second)), + option.Time("hidden", "hidden time", option.Default(time.Second), option.Hidden), ) return nil diff --git a/console.go b/console.go index a42346c..937afbe 100644 --- a/console.go +++ b/console.go @@ -18,6 +18,15 @@ const ( verboseInfo = 1 ) +const ( + OptionHelp = "help" + OptionVersion = "version" + OptionAnsi = "ansi" + OptionNoAnsi = "no-ansi" + OptionQuiet = "quiet" + OptionVerbose = "verbose" +) + // Execute the current command with option. func Execute(ctx context.Context, cmd *Command, opts ...func(*App)) { opts = append([]func(*App){WithSkipArgs(1)}, opts...) @@ -42,7 +51,7 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e out = verbose(ctx, in, out) - if in.Option(ctx, "version").Bool() { + if in.Option(ctx, OptionVersion).Bool() { version := cmd.Version if version == "" { version = "unknown" @@ -53,7 +62,7 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e return nil } - if in.Option(ctx, "help").Bool() { + if in.Option(ctx, OptionHelp).Bool() { return showHelp(ctx, cmd, in, out) } @@ -62,9 +71,9 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e func ansi(ctx context.Context, in input.Input, out output.Output) output.Output { switch { - case in.Option(ctx, "ansi").Bool(): + case in.Option(ctx, OptionAnsi).Bool(): out = output.Ansi(out) - case in.Option(ctx, "no-ansi").Bool(): + case in.Option(ctx, OptionNoAnsi).Bool(): out = output.None(out) case lookupEnv("NO_COLOR"): out = output.None(out) @@ -83,10 +92,10 @@ func lookupEnv(name string) bool { func verbose(ctx context.Context, in input.Input, out output.Output) output.Output { switch { - case in.Option(ctx, "quiet").Bool(): + case in.Option(ctx, OptionQuiet).Bool(): out = output.Quiet() default: - verb := in.Option(ctx, "verbose").Bools() + verb := in.Option(ctx, OptionVerbose).Bools() switch { case len(verb) == verboseInfo: @@ -105,8 +114,8 @@ func verbose(ctx context.Context, in input.Input, out output.Output) output.Outp func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Output) error { arr := &input.Array{} - arr.SetArgument(HelpArgumentCommandName, value.New(cmd.Name)) - arr.SetOption("help", value.New(false)) + arr.SetArgument(ArgumentCommandName, value.New(cmd.Name)) + arr.SetOption(OptionHelp, value.New(false)) if _, err := Find(cmd.Name); errors.Is(err, ErrNotFound) { register(cmd) @@ -125,13 +134,13 @@ func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Outp // Default options and argument command. func Default(d *input.Definition) *input.Definition { return d.SetOptions( - option.Bool("no-ansi", "Disable ANSI output"), - option.Bool("ansi", "Do not ask any interactive question"), - option.Bool("version", "Display this application version", option.Short('V')), - option.Bool("help", "Display this help message", option.Short('h')), - option.Bool("verbose", + option.Bool(OptionNoAnsi, "Disable ANSI output"), + option.Bool(OptionAnsi, "Do not ask any interactive question"), + option.Bool(OptionVersion, "Display this application version", option.Short('V')), + option.Bool(OptionHelp, "Display this help message", option.Short('h')), + option.Bool(OptionVerbose, "Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace", option.Short('v'), option.Array), - option.Bool("quiet", "Do not output any message", option.Short('q')), + option.Bool(OptionQuiet, "Do not output any message", option.Short('q')), ) } diff --git a/help.go b/help.go index ceb9775..a1e10ca 100644 --- a/help.go +++ b/help.go @@ -22,8 +22,8 @@ func init() { } const ( - HelpArgumentCommandName = "command_name" - helpOptFormat = "format" + ArgumentCommandName = "command_name" + OptionFormat = "format" ) func help() *Command { @@ -39,8 +39,8 @@ To display the list of available commands, please use the list comm `, Execute: func(ctx context.Context, in input.Input, out output.Output) error { var err error - name := in.Argument(ctx, HelpArgumentCommandName).String() - format := in.Option(ctx, helpOptFormat).String() + name := in.Argument(ctx, ArgumentCommandName).String() + format := in.Option(ctx, OptionFormat).String() des, err := descriptor.Find(format) if err != nil { @@ -81,10 +81,10 @@ To display the list of available commands, please use the list comm formats := descriptor.Descriptors() config. SetArguments( - argument.String(HelpArgumentCommandName, "The command name", argument.Default(value.New("help"))), + argument.String(ArgumentCommandName, "The command name", argument.Default(value.New("help"))), ). SetOptions( - option.String(helpOptFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")), + option.String(OptionFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")), option.Required, option.Default(formats[0]), option.Valid( diff --git a/input/option/option.go b/input/option/option.go index 5d749e7..f3ca977 100644 --- a/input/option/option.go +++ b/input/option/option.go @@ -15,6 +15,10 @@ func Default(in interface{}) variable.Option { return variable.Default(value.New(in)) } +func Hidden(in *variable.Variable) { + variable.Hidden(in) +} + func Required(v *variable.Variable) { variable.Required(v) } diff --git a/input/variable/variable.go b/input/variable/variable.go index c3f5bf2..b2fb335 100644 --- a/input/variable/variable.go +++ b/input/variable/variable.go @@ -40,6 +40,10 @@ func Required(v *Variable) { v.Flag |= flag.Required } +func Hidden(v *Variable) { + v.hidden = true +} + func WithParse(create Create, update Append) Option { return func(v *Variable) { v.append = func(Param) Append { return update } @@ -99,6 +103,7 @@ type Variable struct { Flag flag.Flag Type ArgType Default value.Value + hidden bool Valid []func(value.Value) error params Params create func(Param) Create @@ -115,6 +120,10 @@ func (v Variable) Validate(in value.Value) error { return nil } +func (v Variable) IsHidden() bool { + return v.hidden +} + func (v Variable) IsArray() bool { return v.Flag.IsArray() } diff --git a/list.go b/list.go index 9a73248..e7daabe 100644 --- a/list.go +++ b/list.go @@ -20,6 +20,10 @@ func init() { MustRegister(list()) } +const ( + ArgumentNamespace = "namespace" +) + func list() *Command { return &Command{ Name: CommandList, @@ -37,10 +41,10 @@ You can also output the information in other formats by using the --for formats := descriptor.Descriptors() config. SetArguments( - argument.String("namespace", "The namespace name"), + argument.String(ArgumentNamespace, "The namespace name"), ). SetOptions( - option.String(helpOptFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")), + option.String(OptionFormat, fmt.Sprintf("The output format (%s)", strings.Join(formats, ", ")), option.Required, option.Default(formats[0]), option.Valid( @@ -57,8 +61,8 @@ You can also output the information in other formats by using the --for //nolint:cyclop func executeList(ctx context.Context, in input.Input, out output.Output) error { - ns := in.Argument(ctx, "namespace").String() - format := in.Option(ctx, helpOptFormat).String() + ns := in.Argument(ctx, ArgumentNamespace).String() + format := in.Option(ctx, OptionFormat).String() des, err := descriptor.Find(format) if err != nil { diff --git a/output/descriptor/txt.go b/output/descriptor/txt.go index 7189ee4..3866d41 100644 --- a/output/descriptor/txt.go +++ b/output/descriptor/txt.go @@ -209,6 +209,9 @@ func txtDefinitionOption(maxLen int, def *input.Definition) string { for _, name := range opts { opt, _ := def.Option(name) + if opt.IsHidden() { + continue + } var op bytes.Buffer