Skip to content

Collect: global option parsing and values #1229

Closed
@shadowspawn

Description

@shadowspawn

Commander looks for the program options anywhere on the command line, including after operands (quite common) and after subcommands (less common). When a program option is defined the value is only available from the program options.

The scenarios people have asked about are:

  1. only looking for program option before subcommand, so say subcommand could use same option name
  2. treating the option as "global" so value available from the subcommand, or even appear in the subcommand help
  3. stop parsing at the first operand and and subsequent "options" are included as ordinary arguments, and can be used for calling another program (without needing to use .allowUnknownOptions or --).

(This discussion uses program and subcommand for simplicity as though there were only two levels, but there could be more levels.)

Related issues:

Standards

POSIX and GNU differ on how options are parsed relative to operands, but do not mention subcommands as such.

POSIX (Open Group)

POSIX has options strictly before operands.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html

Guideline 9:
All options should precede operands on the command line.

GNU

GNU relaxes the order.

https://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfaces

Note that the GNU version of getopt will normally permit options anywhere among the arguments unless the special argument ‘--’ is used. This is not what POSIX specifies; it is a GNU extension.

https://www.gnu.org/software/coreutils/manual/html_node/Common-options.html

Normally options and operands can appear in any order, and programs act as if all the options appear before any operands. For example, ‘sort -r passwd -t :’ acts like ‘sort -r -t : passwd’, since ‘:’ is an option-argument of -t. However, if the POSIXLY_CORRECT environment variable is set, options must appear before operands, unless otherwise specified for a particular command.

Examples from Other Implementations

Yargs has halt-at-non-option which defaults to false.

Should parsing stop at the first positional argument?

Cobra has Flags and PersistentFlags for whether flag [value] available to children,
and TraverseChildren for whether to look for local flags of parent in children arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions