Replaced all uses of argparse -i with argparse -u

This removes the functions/completions that were using the deprecated
--ignore-unknown option and replaces it with the new --move-unknown.

Although some of the code is now more verbose, it has improved the
functionality of two completions:
    the iwctl completion will now skip over options when detecting
        what subcommand is being used
    the ninja completion wil now handle -- correctly: if the completion
        internally invokes ninja, it will correctly interpret the users
        arguments as being arguments if they start with a - and occur
        after the --.
This commit is contained in:
Isaac Oscar Gariano
2025-08-30 01:01:21 +10:00
parent 51d16f017d
commit c94eddaf4b
7 changed files with 17 additions and 22 deletions

View File

@@ -29,9 +29,9 @@ function __fish_conda_subcommand
# get the commandline args without the "conda"
set -l toks (commandline -xpc)[2..-1]
# Remove any important options - if we had options with arguments,
# if we had options with arguments,
# they'd need to be listed here to be removed.
argparse -i h/help v/version -- $toks 2>/dev/null
argparse -u -- $toks 2>/dev/null
# Return false if it fails - this shouldn't really happen,
# so all bets are off
or return 2
@@ -44,19 +44,12 @@ function __fish_conda_subcommand
if test "$subcmds[1]" = "$argv[1]"
set -e argv[1]
set -e subcmds[1]
else if string match -q -- '-*' $argv[1]
set -e argv[1]
else
return 1
end
end
# Skip any remaining options.
while string match -q -- '-*' $argv[1]
set -e argv[1]
end
# If we have no subcommand left,
# If we have no subcommand
# we either matched all given subcommands or we need one.
if not set -q argv[1]
return $have_sub

View File

@@ -8,10 +8,10 @@ function __iwctl_filter -w iwctl
# awk does not work on multiline entries, therefore we use string match,
# which has the added benefit of filtering out the `No devices in ...` lines
argparse -i all-columns -- $argv
argparse -u all-columns -- $argv
# remove color escape sequences
set -l results (iwctl $argv | string replace -ra '\e\[[\d;]+m' '')
set -l results (iwctl $argv_opts -- $argv | string replace -ra '\e\[[\d;]+m' '')
# calculate column widths
set -l headers $results[3]
# We exploit the fact that all column labels will have >2 space to the left, and inside column labels there is always only one space.
@@ -38,7 +38,7 @@ function __iwctl_match_subcoms
set argv (commandline -pxc)
# iwctl allows to specify arguments for username, password, passphrase and dont-ask regardless of any following commands
argparse -i 'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask -- $argv
argparse -u 'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask -- $argv
set argv $argv[2..]
if test (count $argv) != (count $match)
@@ -55,7 +55,7 @@ end
function __iwctl_connect
set argv (commandline -pxc)
# remove all options
argparse -i 'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask -- $argv
argparse -u 'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask -- $argv
# station name should now be the third argument (`iwctl station <wlan>`)
for network in (__iwctl_filter station $argv[3] get-networks rssi-dbms --all-columns)
set network (string split \t -- $network)

View File

@@ -20,7 +20,7 @@ end
function __fish_meson_builddir
# Consider the value of -C option to detect the build directory
set -l cmd (commandline -xpc)
argparse -i 'C=' -- $cmd
argparse -u 'C=' -- $cmd
if set -q _flag_C
echo $_flag_C
else

View File

@@ -1,7 +1,7 @@
function __fish_ninja
set -l saved_args $argv
set -l dir .
if argparse -i C/dir= -- (commandline -xpc)
if argparse -u C/dir= -- (commandline -xpc)
command ninja -C$_flag_C $saved_args
end
end

View File

@@ -61,11 +61,13 @@ function __fish_complete_yadm_like_git
set -l yadm_work_tree (yadm gitconfig --get core.worktree)
set -l yadm_repo (yadm introspect repo)
argparse -i 'R-yadm-repo=' -- $cmdline 2>/dev/null
argparse -u 'R-yadm-repo=&' -- $cmdline 2>/dev/null
if set -q _flag_yadm_repo
set yadm_repo $_flag_yadm_repo
# argparse *always* sets $argv to remaining arguments after consuming specified options
set cmdline $argv
# argparse -u *always* sets $argv to remaining arguments after consuming all options,
# and it *always* sets $argv_opts to any specified non-& options and any unknown options
# (the -- is needed in case $argv originally contained a -- followed by arguments starting with a -)
set cmdline $argv_opts -- $argv
end
set -l git_wrapper_cmd "git --work-tree $yadm_work_tree --git-dir $yadm_repo $cmdline"

View File

@@ -1,5 +1,5 @@
function __fish_mysql_query -a query
argparse -i 'u/user=' 'P/port=' 'h/host=' 'p/password=?' 'S/socket=' -- (commandline -px)
argparse -u 'u/user=' 'P/port=' 'h/host=' 'p/password=?' 'S/socket=' -- (commandline -px)
set -l mysql_cmd mysql
for flag in u P h S
if set -q _flag_$flag

View File

@@ -1,5 +1,5 @@
function __fish_seen_argument --description 'Check whether argument is used'
argparse --ignore-unknown 's/short=+' 'o/old=+' 'l/long=+' 'w/windows=+' -- $argv
argparse --move-unknown 's/short=+&' 'o/old=+&' 'l/long=+&' 'w/windows=+&' -- $argv
set --local tokens (commandline --current-process --tokens-expanded --cut-at-cursor)
set --erase tokens[1]
@@ -30,7 +30,7 @@ function __fish_seen_argument --description 'Check whether argument is used'
end
end
for raw_arg in $argv
for raw_arg in $argv_opts $argv
if string match --quiet -- $t $raw_arg
return 0
end