Skip to content

Choices for arguments #248

@zekroTJA

Description

@zekroTJA

It would be awesome if you could specify choices (like possible/allowed values) for arguments and flags.

I could imagine it in two ways.

a) Via struct tags

var args struct {
    LogLevel string `arg:"-l,--log-level" choices:"panic,error,warn,info,debug"`
}

b) Via a method

In example, a parameter type could implement a Method - i.e. - called ValueChoises, which returns a list of allowed values for the argument.

type Level string

func (t Level) ValueChoises() []string {
	return []string{"panic", "error", "warn", "info", "debug"}
}

var args struct {
    LogLevel Level `arg:"-l,--log-level"`
}

I think I would prefer the second way, because it allows for more flexibility with the definition of choices.

Then, the choice values could be represented in the help text something like as following.

Usage: main [--level LEVEL]

Options:
  --level LEVEL, -l LEVEL
                         log level
                         choices: panic, error, warn, info, debug
  --help, -h             display this help and exit

Let me know what you think of the idea. If you think it would fit in the project, I would be pleased to submit an implementation for it as well. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions