add hidden option #4

Merged
andrey merged 1 commits from hidden into master 2 years ago
  1. 2
      app.go
  2. 1
      command_test.go
  3. 37
      console.go
  4. 12
      help.go
  5. 4
      input/option/option.go
  6. 9
      input/variable/variable.go
  7. 12
      list.go
  8. 3
      output/descriptor/txt.go

2
app.go

@ -111,7 +111,7 @@ func (a *App) list(ctx context.Context) error {
} }
arr := &input.Array{} arr := &input.Array{}
arr.SetArgument("command_name", value.New(CommandList)) arr.SetArgument(ArgumentCommandName, value.New(CommandList))
in := input.Chain(arr, a.in) in := input.Chain(arr, a.in)
return Run(ctx, cmd, in, a.out) return Run(ctx, cmd, in, a.out)

1
command_test.go

@ -43,6 +43,7 @@ func Command() *console.Command {
option.String("string", "array string", option.Array), option.String("string", "array string", option.Array),
option.Bool("bool", "test bool option"), option.Bool("bool", "test bool option"),
option.Duration("duration", "test duration with default", option.Default(time.Second)), option.Duration("duration", "test duration with default", option.Default(time.Second)),
option.Time("hidden", "hidden time", option.Default(time.Second), option.Hidden),
) )
return nil return nil

37
console.go

@ -18,6 +18,15 @@ const (
verboseInfo = 1 verboseInfo = 1
) )
const (
OptionHelp = "help"
OptionVersion = "version"
OptionAnsi = "ansi"
OptionNoAnsi = "no-ansi"
OptionQuiet = "quiet"
OptionVerbose = "verbose"
)
// Execute the current command with option. // Execute the current command with option.
func Execute(ctx context.Context, cmd *Command, opts ...func(*App)) { func Execute(ctx context.Context, cmd *Command, opts ...func(*App)) {
opts = append([]func(*App){WithSkipArgs(1)}, opts...) 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) out = verbose(ctx, in, out)
if in.Option(ctx, "version").Bool() { if in.Option(ctx, OptionVersion).Bool() {
version := cmd.Version version := cmd.Version
if version == "" { if version == "" {
version = "unknown" version = "unknown"
@ -53,7 +62,7 @@ func Run(ctx context.Context, cmd *Command, in input.Input, out output.Output) e
return nil return nil
} }
if in.Option(ctx, "help").Bool() { if in.Option(ctx, OptionHelp).Bool() {
return showHelp(ctx, cmd, in, out) 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 { func ansi(ctx context.Context, in input.Input, out output.Output) output.Output {
switch { switch {
case in.Option(ctx, "ansi").Bool(): case in.Option(ctx, OptionAnsi).Bool():
out = output.Ansi(out) out = output.Ansi(out)
case in.Option(ctx, "no-ansi").Bool(): case in.Option(ctx, OptionNoAnsi).Bool():
out = output.None(out) out = output.None(out)
case lookupEnv("NO_COLOR"): case lookupEnv("NO_COLOR"):
out = output.None(out) 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 { func verbose(ctx context.Context, in input.Input, out output.Output) output.Output {
switch { switch {
case in.Option(ctx, "quiet").Bool(): case in.Option(ctx, OptionQuiet).Bool():
out = output.Quiet() out = output.Quiet()
default: default:
verb := in.Option(ctx, "verbose").Bools() verb := in.Option(ctx, OptionVerbose).Bools()
switch { switch {
case len(verb) == verboseInfo: 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 { func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Output) error {
arr := &input.Array{} arr := &input.Array{}
arr.SetArgument(HelpArgumentCommandName, value.New(cmd.Name)) arr.SetArgument(ArgumentCommandName, value.New(cmd.Name))
arr.SetOption("help", value.New(false)) arr.SetOption(OptionHelp, value.New(false))
if _, err := Find(cmd.Name); errors.Is(err, ErrNotFound) { if _, err := Find(cmd.Name); errors.Is(err, ErrNotFound) {
register(cmd) register(cmd)
@ -125,13 +134,13 @@ func showHelp(ctx context.Context, cmd *Command, in input.Input, out output.Outp
// Default options and argument command. // Default options and argument command.
func Default(d *input.Definition) *input.Definition { func Default(d *input.Definition) *input.Definition {
return d.SetOptions( return d.SetOptions(
option.Bool("no-ansi", "Disable ANSI output"), option.Bool(OptionNoAnsi, "Disable ANSI output"),
option.Bool("ansi", "Do not ask any interactive question"), option.Bool(OptionAnsi, "Do not ask any interactive question"),
option.Bool("version", "Display this application version", option.Short('V')), option.Bool(OptionVersion, "Display this application version", option.Short('V')),
option.Bool("help", "Display this help message", option.Short('h')), option.Bool(OptionHelp, "Display this help message", option.Short('h')),
option.Bool("verbose", option.Bool(OptionVerbose,
"Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace", "Increase the verbosity of messages: -v for info output, -vv for debug and -vvv for trace",
option.Short('v'), option.Array), 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')),
) )
} }

12
help.go

@ -22,8 +22,8 @@ func init() {
} }
const ( const (
HelpArgumentCommandName = "command_name" ArgumentCommandName = "command_name"
helpOptFormat = "format" OptionFormat = "format"
) )
func help() *Command { func help() *Command {
@ -39,8 +39,8 @@ To display the list of available commands, please use the <info>list</info> comm
`, `,
Execute: func(ctx context.Context, in input.Input, out output.Output) error { Execute: func(ctx context.Context, in input.Input, out output.Output) error {
var err error var err error
name := in.Argument(ctx, HelpArgumentCommandName).String() name := in.Argument(ctx, ArgumentCommandName).String()
format := in.Option(ctx, helpOptFormat).String() format := in.Option(ctx, OptionFormat).String()
des, err := descriptor.Find(format) des, err := descriptor.Find(format)
if err != nil { if err != nil {
@ -81,10 +81,10 @@ To display the list of available commands, please use the <info>list</info> comm
formats := descriptor.Descriptors() formats := descriptor.Descriptors()
config. config.
SetArguments( SetArguments(
argument.String(HelpArgumentCommandName, "The command name", argument.Default(value.New("help"))), argument.String(ArgumentCommandName, "The command name", argument.Default(value.New("help"))),
). ).
SetOptions( 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.Required,
option.Default(formats[0]), option.Default(formats[0]),
option.Valid( option.Valid(

4
input/option/option.go

@ -15,6 +15,10 @@ func Default(in interface{}) variable.Option {
return variable.Default(value.New(in)) return variable.Default(value.New(in))
} }
func Hidden(in *variable.Variable) {
variable.Hidden(in)
}
func Required(v *variable.Variable) { func Required(v *variable.Variable) {
variable.Required(v) variable.Required(v)
} }

9
input/variable/variable.go

@ -40,6 +40,10 @@ func Required(v *Variable) {
v.Flag |= flag.Required v.Flag |= flag.Required
} }
func Hidden(v *Variable) {
v.hidden = true
}
func WithParse(create Create, update Append) Option { func WithParse(create Create, update Append) Option {
return func(v *Variable) { return func(v *Variable) {
v.append = func(Param) Append { return update } v.append = func(Param) Append { return update }
@ -99,6 +103,7 @@ type Variable struct {
Flag flag.Flag Flag flag.Flag
Type ArgType Type ArgType
Default value.Value Default value.Value
hidden bool
Valid []func(value.Value) error Valid []func(value.Value) error
params Params params Params
create func(Param) Create create func(Param) Create
@ -115,6 +120,10 @@ func (v Variable) Validate(in value.Value) error {
return nil return nil
} }
func (v Variable) IsHidden() bool {
return v.hidden
}
func (v Variable) IsArray() bool { func (v Variable) IsArray() bool {
return v.Flag.IsArray() return v.Flag.IsArray()
} }

12
list.go

@ -20,6 +20,10 @@ func init() {
MustRegister(list()) MustRegister(list())
} }
const (
ArgumentNamespace = "namespace"
)
func list() *Command { func list() *Command {
return &Command{ return &Command{
Name: CommandList, Name: CommandList,
@ -37,10 +41,10 @@ You can also output the information in other formats by using the <comment>--for
formats := descriptor.Descriptors() formats := descriptor.Descriptors()
config. config.
SetArguments( SetArguments(
argument.String("namespace", "The namespace name"), argument.String(ArgumentNamespace, "The namespace name"),
). ).
SetOptions( 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.Required,
option.Default(formats[0]), option.Default(formats[0]),
option.Valid( option.Valid(
@ -57,8 +61,8 @@ You can also output the information in other formats by using the <comment>--for
//nolint:cyclop //nolint:cyclop
func executeList(ctx context.Context, in input.Input, out output.Output) error { func executeList(ctx context.Context, in input.Input, out output.Output) error {
ns := in.Argument(ctx, "namespace").String() ns := in.Argument(ctx, ArgumentNamespace).String()
format := in.Option(ctx, helpOptFormat).String() format := in.Option(ctx, OptionFormat).String()
des, err := descriptor.Find(format) des, err := descriptor.Find(format)
if err != nil { if err != nil {

3
output/descriptor/txt.go

@ -209,6 +209,9 @@ func txtDefinitionOption(maxLen int, def *input.Definition) string {
for _, name := range opts { for _, name := range opts {
opt, _ := def.Option(name) opt, _ := def.Option(name)
if opt.IsHidden() {
continue
}
var op bytes.Buffer var op bytes.Buffer

Loading…
Cancel
Save