coder / serpent

CLI framework for scale and configurability inspired by Cobra
Creative Commons Zero v1.0 Universal
6 stars 1 forks source link

How to configure alias options? #15

Open ammario opened 1 month ago

ammario commented 1 month ago

Re the discussion here, it's common to want multiple envs/flags to configure the same underlying option value esp. when refactoring the command line interface.

@mafredri proposes the concept of an OptionAlias that embeds within each Option like so:

type OptionAlias struct {
    Flag          string `json:"flag,omitempty"`
    FlagShorthand string `json:"flag_shorthand,omitempty"`
    Env           string `json:"env,omitempty"`
    Deprecated    string `json:"deprecated,omitempty"`
}

// Option is a configuration option for a CLI application.
type Option struct {
    Aliases []OptionAlias `json:"aliases,omitempty"`
    // ...
}

// Usage:
{
    // ...
    Env: "CODER_FOOBAR",
    Alias: []serpent.OptionAlias{
        {Env: "FOOBAR", Deprecated: "Use CODER_FOOBAR instead."},
    },
},

I think the extra level of nesting on all options is unjustified when in the vast majority of options there is only one "alias".