From 4dc685db6b90d576e1c881c7071528b32c3f1dd0 Mon Sep 17 00:00:00 2001 From: EmilySeville7cfg Date: Thu, 11 Nov 2021 23:12:45 +1000 Subject: [PATCH] Completions for builtin argparse Closes #8434 --- share/completions/argparse.fish | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 share/completions/argparse.fish diff --git a/share/completions/argparse.fish b/share/completions/argparse.fish new file mode 100644 index 000000000..59fefeea9 --- /dev/null +++ b/share/completions/argparse.fish @@ -0,0 +1,72 @@ +function __fish_argparse_exclusive_args --description 'Helper function to list unused options' + set --local all_tokens (commandline --tokenize) + 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 \ + --description 'Ignore unknown options' +complete --command argparse --short-option s --long-option stop-nonopt \ + --description 'Exit on subcommand'