Files
fish-shell/share/completions/argparse.fish
Isaac Oscar Gariano 9d56cdbcbc Added a -U/--unknown-arguments option to argparse
The new -U/--unknown-arguments option takes either 'optional', 'required', or
'none', indicating how many arguments unknown options are assumed to take.
The default is optional, the same behaviour as before this commit, despite
most options in practice taking not taking any arguments. Using
--unknown-arguments=required and --unknown-arguments=none (but not
--unknown-arguments=optional) can give you parse errors if, respectively,
an unknown option has no argument (because it the option is at the end of the
argument list), or is given an argument (with the `--flag=<value> syntax).
See doc_src/cmds/argparse.rst for more details (specifically, the descritpion
of the --unknown-arguments flag and the example at the end
of the examples section).

As a convenience, -U/--unknown-arguments implies -u/--move-unknown.
However you can use it the deprecated -i/--ignore-unknown if you really want to.
2025-08-30 01:55:56 +10:00

82 lines
3.4 KiB
Fish

function __fish_argparse_exclusive_args --description 'Helper function to list unused options'
set --local all_tokens (commandline --tokens-expanded)
set --erase all_tokens[1]
set --local current_token (commandline --current-token)
if set --local index (contains --index -- '--' $all_tokens)
set index (math $index - 1)
else
set index (count $all_tokens)
end
set --local specifications
while test $index -gt 0
and set -l tok $all_tokens[$index]
and not string match --quiet -- '-*' $tok
if string match --quiet '*,*' $tok
set index (math $index - 1)
continue
end
set -l opt (string replace --regex '=.*' '' $tok)
if string match --quiet '*/*' $opt
set --append specifications (string split '/' $opt)
else if string match --quiet '*#*' $opt
set --append specifications (string split '#' $opt)
else
set --append specifications $opt
end
set index (math $index - 1)
end
if string match --quiet -- '-x*' $current_token
set current_token (string replace --regex -- '^-x' '' $current_token)
end
if test $index -gt 1
and string match --regex --quiet -- '^(-x|--exclusive)$' $all_tokens[(math $index - 1)]
and not test -z $current_token
set --erase specifications[(count $specifications)]
end
set --local used_options (string split -- ',' $current_token)
set --local unused_specifications
for item in $specifications
if not contains -- $item $used_options
set --append unused_specifications $item
end
end
string join \n $unused_specifications
end
complete --command argparse --no-files
complete --command argparse --short-option h --long-option help --description 'Show help'
complete --command argparse --short-option n --long-option name --require-parameter --no-files \
--arguments '(functions --all | string replace ", " "\n")' --description 'Use function name'
complete --command argparse --short-option x --long-option exclusive --no-files --require-parameter \
--arguments '(__fish_append "," (__fish_argparse_exclusive_args))' \
--description 'Specify mutually exclusive options'
complete --command argparse --short-option N --long-option min-args --no-files --require-parameter \
--description 'Specify minimum non-option argument count'
complete --command argparse --short-option X --long-option max-args --no-files --require-parameter \
--description 'Specify maximum non-option argument count'
complete --command argparse --short-option i --long-option ignore-unknown \
-n '! __fish_seen_argument --short u --long move-unknown' \
--description 'Ignore unknown options'
complete --command argparse --short-option u --long-option move-unknown \
-n '! __fish_seen_argument --short i --long ignore-unknown' \
--description 'Move unknown options into \$argv_opts'
complete --command argparse --short-option S --long-option strict-longopts \
--description 'Pass long options strictly'
complete --command argparse --short-option U --long-option unknown-arguments --no-files --require-parameter \
--arguments "optional required none" \
--description 'Whether unknown options can have arguments'
complete --command argparse --short-option s --long-option stop-nonopt \
--description 'Exit on subcommand'