Skip to content

Some form of flag inheritance for def and extern #12363

Open
@KAAtheWiseGit

Description

Related problem

A common pattern in CLI utilities with subcommands is a set of flags, which are shared between all commands (--verbose, --force, and so on).

In Nushell, currently, subcommands are independent of their parent commands. This makes implementing nested commands and extern completions for tools like git or apk tricky, because the global options have to be repeated several times.

Describe the solution you'd like

My idea is to add some kind of inheritance, so that a custom function or an extern can inherit some other command's flags. This allows to enable completely separate subcommands (so, aa x and aa y will, by default, be unrelated), but still provide the option to share flags as an opt-in.

Describe alternatives you've considered

  • Flags as data. For example, flags could be written down as a long, short, doc, type, default, completer table and be added to a command:

    const flags = [
     {
     	long: "verbose",
     	short: "v",
     	doc: "Print debug information",
     },
     {
     	long: "format",
     	doc: "The format for the dates",
     	type: string,
     	completer: {|| format_func}
     },
     ...
    ]
    
    def command [
     ...flags
    ] {
     # stuff
    }
    
  • Automatically inheriting flags for subcommands. So def aa x ... would inherit the flags of def aa, with an opt-out flag in def. Not very elegant, and doesn't work when the root command is called main.

Additional context and details

No response

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestneeds-designthis feature requires designsemanticsPlaces where we should define/clarify nushell's semanticssyntaxChanges to the grammar or syntax beyond parser bugfixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions