mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-04-27 23:11:15 -03:00
Modified argparse to support one character long only options.
This fixes an issue noticed in the previous commit (the made the -s/--short
option optional to fish_opt): it was impossible to define a single character
long flag, unless you also provided a single-character short flag equivalent.
This commit works by allowing an option spec to start with a '/', treating the
subsequent alpha-numeric characters as a long flag name.
In detail, consider the following:
- s defines a -s short flag
- ss defines an --ss long flag
- /ss (new) also defines a --ss long flag
- s/s defines a -s short flag and an --s long flag
- s-s defines a --s long flag (if there's already an -s short flag, you'd have
to change the first s, e.g. S-s)
- /s (new) defines a --s long flag
- s/ is an error (a long flag name must follow the /)
Note that without using --strict-longopts, a long flag --s can always be
abbreviated as -s, provided that -s isn't defined as a separate short flag.
This 'issue' fixed by this commit is relatively trivial, however it does allow
simplifying the documentation for fish_opt (since it no longer needs to mention
the restriction). In particular, this commit makes the --long-only flag to
fish_opt completely unnecessary (but it is kept for backwards compatibility).
This commit is contained in:
@@ -138,7 +138,9 @@ Each option specification consists of:
|
||||
|
||||
- An optional alphanumeric short flag character.
|
||||
|
||||
- An optional long flag name, seperated from the short flag (if present) by a ``/``. If neither a short flag nor long flag are present, an error is reported.
|
||||
- An optional long flag name preceded by a ``/``. If neither a short flag nor long flag are present, an error is reported.
|
||||
|
||||
- If there is no short flag, and the long flag name is more than one character, the ``/`` can be omitted.
|
||||
|
||||
- For backwards compatibility, if there is a short and a long flag, a ``-`` can be used in place of the ``/``, if the short flag is not to be usable by users (in which case it will also not be exposed as a flag variable).
|
||||
|
||||
@@ -255,6 +257,8 @@ Some *OPTION_SPEC* examples:
|
||||
|
||||
- ``x`` means that only ``-x`` is valid. It is a boolean that can be used more than once. If it is seen then ``_flag_x`` will be set as above.
|
||||
|
||||
- ``/x`` is similar, but only ``--x`` is valid (instead of ``-x``).
|
||||
|
||||
- ``x=``, ``x=?``, and ``x=+`` are similar to the n/name examples above but there is no long flag alternative to the short flag ``-x``.
|
||||
|
||||
- ``#max`` (or ``#-max``) means that flags matching the regex "^--?\\d+$" are valid. When seen they are assigned to the variable ``_flag_max``. This allows any valid positive or negative integer to be specified by prefixing it with a single "-". Many commands support this idiom. For example ``head -3 /a/file`` to emit only the first three lines of /a/file.
|
||||
|
||||
@@ -22,10 +22,10 @@ The following ``argparse`` options are available:
|
||||
Takes a single letter or number that is used as the short flag in the option being defined. Either this option or the **--long** option must be provided.
|
||||
|
||||
**-l** or **--long** *LONG-NAME*
|
||||
Takes a string that is used as the long flag in the option being defined. This option is optional and has no default. If no long flag is defined then only the short flag will be allowed when parsing arguments using the option specification. If there is no **--short** flag, the long flag name must be more than one character (use **--short** together with **--long-only** to bypass this restriction).
|
||||
Takes a string that is used as the long flag in the option being defined. This option is optional and has no default. If no long flag is defined then only the short flag will be allowed when parsing arguments using the option specification.
|
||||
|
||||
**--long-only**
|
||||
The option being defined will only allow the long flag name to be used, even if the short flag is defined (i.e., **--short** is specified).
|
||||
Deprecated. The option being defined will only allow the long flag name to be used, even if the short flag is defined (i.e., **--short** is specified).
|
||||
|
||||
**-o** or **--optional-val**
|
||||
The option being defined can take a value, but it is optional rather than required. If the option is seen more than once when parsing arguments, only the last value seen is saved. This means the resulting flag variable created by ``argparse`` will zero elements if no value was given with the option else it will have exactly one element.
|
||||
|
||||
Reference in New Issue
Block a user