| 1 |
#!/bin/fish |
| 2 |
|
| 3 |
function __fish_query |
| 4 |
# =========================================================================== |
| 5 |
# |
| 6 |
# This function implements the parsing of options and positionals in the Fish shell. |
| 7 |
# |
| 8 |
# Usage: __fish_query <OPTIONS> <COMMAND> [ARGS...] |
| 9 |
# |
| 10 |
# The first argument is a comma-separated list of options that the parser should know about. |
| 11 |
# Short options (-o), long options (--option), and old-style options (-option) are supported. |
| 12 |
# |
| 13 |
# If an option takes an argument, it is suffixed by '='. |
| 14 |
# If an option takes an optional argument, it is suffixed by '=?'. |
| 15 |
# |
| 16 |
# For example: |
| 17 |
# __fish_query '-f,--flag,-old-style,--with-arg=,--with-optional=?' [...] |
| 18 |
# |
| 19 |
# Here, -f, --flag and -old-style don't take options, --with-arg requires an |
| 20 |
# argument and --with-optional takes an optional argument. |
| 21 |
# |
| 22 |
# COMMANDS |
| 23 |
# positional_contains <NUM> <WORDS...> |
| 24 |
# Checks if the positional argument number NUM is one of WORDS. |
| 25 |
# NUM counts from one. |
| 26 |
# |
| 27 |
# has_option [WITH_INCOMPLETE] <OPTIONS...> |
| 28 |
# Checks if an option given in OPTIONS is passed on commandline. |
| 29 |
# If an option requires an argument, this command returns true only if the |
| 30 |
# option includes an argument. If 'WITH_INCOMPLETE' is specified, it also |
| 31 |
# returns true for options missing their arguments. |
| 32 |
# |
| 33 |
# option_is <OPTIONS...> -- <VALUES...> |
| 34 |
# Checks if any option in OPTIONS has a value of VALUES. |
| 35 |
# |
| 36 |
# num_of_positionals [<OPERATOR> <NUMBER>] |
| 37 |
# Checks the number of positional arguments. |
| 38 |
# If no arguments are provided, print the total count of positional arguments. |
| 39 |
# If two arguments are provided, the first argument should be one of |
| 40 |
# the comparison operators: '-lt', '-le', '-eq', '-ne', '-gt', '-ge'. |
| 41 |
# Returns 0 if the count of positional arguments matches the |
| 42 |
# specified NUMBER according to the comparison operator, otherwise returns 1. |
| 43 |
# |
| 44 |
# =========================================================================== |
| 45 |
|
| 46 |
set -l positionals |
| 47 |
set -l having_options |
| 48 |
set -l option_values |
| 49 |
|
| 50 |
#ifdef DEBUG |
| 51 |
switch (count $argv) |
| 52 |
case 0 |
| 53 |
echo "%FUNCNAME%: missing OPTIONS argument" >&2 |
| 54 |
return 1 |
| 55 |
case 1 |
| 56 |
echo "%FUNCNAME%: missing COMMAND" >&2 |
| 57 |
return 1 |
| 58 |
end |
| 59 |
#endif |
| 60 |
|
| 61 |
set -l options $argv[1] |
| 62 |
set -e argv[1] |
| 63 |
|
| 64 |
set -l cmd $argv[1] |
| 65 |
set -e argv[1] |
| 66 |
|
| 67 |
set -l my_cache_key "$(commandline -b) $options" |
| 68 |
|
| 69 |
if test "$__QUERY_CACHE_KEY" = "$my_cache_key" |
| 70 |
set positionals $__QUERY_CACHE_POSITIONALS |
| 71 |
set having_options $__QUERY_CACHE_HAVING_OPTIONS |
| 72 |
set option_values $__QUERY_CACHE_OPTION_VALUES |
| 73 |
else |
| 74 |
# ========================================================================= |
| 75 |
# Parsing of OPTIONS argument |
| 76 |
# ========================================================================= |
| 77 |
|
| 78 |
#ifdef short_options |
| 79 |
set -l short_opts_with_arg |
| 80 |
set -l short_opts_without_arg |
| 81 |
set -l short_opts_with_optional_arg |
| 82 |
#endif |
| 83 |
#ifdef long_options |
| 84 |
set -l long_opts_with_arg |
| 85 |
set -l long_opts_without_arg |
| 86 |
set -l long_opts_with_optional_arg |
| 87 |
#endif |
| 88 |
#ifdef old_options |
| 89 |
set -l old_opts_with_arg |
| 90 |
set -l old_opts_without_arg |
| 91 |
set -l old_opts_with_optional_arg |
| 92 |
#endif |
| 93 |
|
| 94 |
set -l option |
| 95 |
|
| 96 |
if test -n "$options" |
| 97 |
for option in (string split -- ',' $options) |
| 98 |
# Using one big switch case is the fastest way |
| 99 |
switch $option |
| 100 |
#ifdef long_options |
| 101 |
case '--?*='; set -a long_opts_with_arg (string replace -- '=' '' $option) |
| 102 |
case '--?*=\?'; set -a long_opts_with_optional_arg (string replace -- '=?' '' $option) |
| 103 |
case '--?*'; set -a long_opts_without_arg $option |
| 104 |
#endif |
| 105 |
#ifdef short_options |
| 106 |
case '-?='; set -a short_opts_with_arg (string replace -- '=' '' $option) |
| 107 |
case '-?=\?'; set -a short_opts_with_optional_arg (string replace -- '=?' '' $option) |
| 108 |
case '-?'; set -a short_opts_without_arg $option |
| 109 |
#endif |
| 110 |
#ifdef old_options |
| 111 |
case '-??*='; set -a old_opts_with_arg (string replace -- '=' '' $option) |
| 112 |
case '-??*=\?'; set -a old_opts_with_optional_arg (string replace -- '=?' '' $option) |
| 113 |
case '-??*'; set -a old_opts_without_arg $option |
| 114 |
#endif |
| 115 |
#ifdef DEBUG |
| 116 |
case '*' |
| 117 |
echo "%FUNCNAME%: argv[1]: '$option' is not a short, long or old-style option" >&2 |
| 118 |
return 1 |
| 119 |
#endif |
| 120 |
end |
| 121 |
end |
| 122 |
end |
| 123 |
|
| 124 |
# ========================================================================= |
| 125 |
# Parsing of options and positionals |
| 126 |
# ========================================================================= |
| 127 |
|
| 128 |
set -l cmdline (commandline -poc) |
| 129 |
set -l cmdline_count (count $cmdline) |
| 130 |
|
| 131 |
set -l argi 2 # cmdline[1] is command name |
| 132 |
while test $argi -le $cmdline_count |
| 133 |
set -l arg "$cmdline[$argi]" |
| 134 |
set -l have_trailing_arg (test $argi -lt $cmdline_count && echo true || echo false) |
| 135 |
|
| 136 |
switch $arg |
| 137 |
case '-' |
| 138 |
set -a positionals - |
| 139 |
case '--' |
| 140 |
for argi in (seq (math $argi + 1) $cmdline_count) |
| 141 |
set -a positionals $cmdline[$argi] |
| 142 |
end |
| 143 |
break |
| 144 |
case '--*=*' |
| 145 |
set -l split (string split -m 1 -- '=' $arg) |
| 146 |
set -a having_options $split[1] |
| 147 |
set -a option_values "$split[2]" |
| 148 |
case '--*' |
| 149 |
#ifdef long_options |
| 150 |
if contains -- $arg $long_opts_with_arg |
| 151 |
if $have_trailing_arg |
| 152 |
set -a having_options $arg |
| 153 |
set -a option_values $cmdline[(math $argi + 1)] |
| 154 |
set argi (math $argi + 1) |
| 155 |
end |
| 156 |
else |
| 157 |
set -a having_options $arg |
| 158 |
set -a option_values "" |
| 159 |
end |
| 160 |
#endif |
| 161 |
case '-*' |
| 162 |
set -l end_of_parsing false |
| 163 |
|
| 164 |
#ifdef old_options |
| 165 |
if string match -q -- "*=*" $arg |
| 166 |
set -l split (string split -m 1 -- '=' $arg) |
| 167 |
if contains -- $split[1] $old_opts_with_arg $old_opts_with_optional_arg |
| 168 |
set -a having_options $split[1] |
| 169 |
set -a option_values "$split[2]" |
| 170 |
set end_of_parsing true |
| 171 |
end |
| 172 |
else if contains -- $arg $old_opts_with_arg |
| 173 |
set end_of_parsing true |
| 174 |
if $have_trailing_arg |
| 175 |
set -a having_options $arg |
| 176 |
set -a option_values $cmdline[(math $argi + 1)] |
| 177 |
set argi (math $argi + 1) |
| 178 |
end |
| 179 |
else if contains -- $arg $old_opts_without_arg $old_opts_with_optional_arg |
| 180 |
set -a having_options $arg |
| 181 |
set -a option_values "" |
| 182 |
set end_of_parsing true |
| 183 |
end |
| 184 |
#endif |
| 185 |
|
| 186 |
#ifdef short_options |
| 187 |
set -l arg_length (string length -- $arg) |
| 188 |
set -l i 2 |
| 189 |
while not $end_of_parsing; and test $i -le $arg_length |
| 190 |
set -l option "-$(string sub -s $i -l 1 -- $arg)" |
| 191 |
set -l trailing_chars "$(string sub -s (math $i + 1) -- $arg)" |
| 192 |
|
| 193 |
if contains -- $option $short_opts_without_arg |
| 194 |
set -a having_options $option |
| 195 |
set -a option_values "" |
| 196 |
else if contains -- $option $short_opts_with_arg |
| 197 |
set end_of_parsing true |
| 198 |
|
| 199 |
if test -n "$trailing_chars" |
| 200 |
set -a having_options $option |
| 201 |
set -a option_values $trailing_chars |
| 202 |
else if $have_trailing_arg |
| 203 |
set -a having_options $option |
| 204 |
set -a option_values $cmdline[(math $argi + 1)] |
| 205 |
set argi (math $argi + 1) |
| 206 |
end |
| 207 |
else if contains -- $option $short_opts_with_optional_arg |
| 208 |
set end_of_parsing true |
| 209 |
set -a having_options $option |
| 210 |
set -a option_values "$trailing_chars" # may be empty |
| 211 |
end |
| 212 |
|
| 213 |
set i (math $i + 1) |
| 214 |
end |
| 215 |
#endif |
| 216 |
case '*' |
| 217 |
set -a positionals $arg |
| 218 |
end |
| 219 |
|
| 220 |
set argi (math $argi + 1) |
| 221 |
end |
| 222 |
|
| 223 |
set -g __QUERY_CACHE_POSITIONALS $positionals |
| 224 |
set -g __QUERY_CACHE_HAVING_OPTIONS $having_options |
| 225 |
set -g __QUERY_CACHE_OPTION_VALUES $option_values |
| 226 |
set -g __QUERY_CACHE_KEY $my_cache_key |
| 227 |
end |
| 228 |
|
| 229 |
# =========================================================================== |
| 230 |
# Commands |
| 231 |
# =========================================================================== |
| 232 |
|
| 233 |
switch $cmd |
| 234 |
#ifdef positional_contains |
| 235 |
case 'positional_contains' |
| 236 |
if test (count $argv) -eq 0 |
| 237 |
echo "%FUNCNAME%: positional_contains: argv[3]: missing number" >&2 |
| 238 |
return 1 |
| 239 |
end |
| 240 |
|
| 241 |
set -l positional_num $argv[1] |
| 242 |
set -e argv[1] |
| 243 |
contains -- $positionals[$positional_num] $argv && return 0 || return 1 |
| 244 |
#endif |
| 245 |
#ifdef has_option |
| 246 |
case 'has_option' |
| 247 |
#ifdef with_incomplete |
| 248 |
set -l with_incomplete false |
| 249 |
|
| 250 |
if test $argv[1] = "WITH_INCOMPLETE" |
| 251 |
set with_incomplete true |
| 252 |
set -e argv[1] |
| 253 |
end |
| 254 |
|
| 255 |
#endif |
| 256 |
for option in $having_options |
| 257 |
contains -- $option $argv && return 0 |
| 258 |
end |
| 259 |
|
| 260 |
#ifdef with_incomplete |
| 261 |
if $with_incomplete |
| 262 |
set -l tokens (commandline -po) |
| 263 |
set -e tokens[1] |
| 264 |
for option in $argv |
| 265 |
switch $option |
| 266 |
case '-?' |
| 267 |
set option (string sub -l 1 -s 2 -- $option) |
| 268 |
string match -q -r -- "^-[A-z0-9]*$option\$" $tokens[-2] && return 0 |
| 269 |
string match -q -r -- "^-[A-z0-9]*$option.*\$" $tokens[-1] && return 0 |
| 270 |
case '*' |
| 271 |
string match -q -r -- "^$option\$" $tokens[-2] && return 0 |
| 272 |
string match -q -r -- "^$option(=.*)?\$" $tokens[-1] && return 0 |
| 273 |
contains -- $tokens[-1] $argv && return 0 |
| 274 |
end |
| 275 |
end |
| 276 |
end |
| 277 |
|
| 278 |
#endif |
| 279 |
return 1 |
| 280 |
#endif |
| 281 |
#ifdef num_of_positionals |
| 282 |
case 'num_of_positionals' |
| 283 |
switch (count $argv) |
| 284 |
case 0 |
| 285 |
count $positionals |
| 286 |
case 1 |
| 287 |
echo "%FUNCNAME%: num_of_positionals: $argv[1]: missing operand" >&2 |
| 288 |
return 1 |
| 289 |
case 2 |
| 290 |
if contains -- $argv[1] -lt -le -eq -ne -gt -ge; |
| 291 |
test (count $positionals) $argv[1] $argv[2] && return 0 || return 1 |
| 292 |
else |
| 293 |
echo "%FUNCNAME%: num_of_positionals: $argv[1]: unknown operator" >&2 |
| 294 |
return 1 |
| 295 |
end |
| 296 |
case '*' |
| 297 |
echo "%FUNCNAME%: num_of_positionals: too many arguments" >&2 |
| 298 |
return 1 |
| 299 |
end |
| 300 |
#endif |
| 301 |
#ifdef option_is |
| 302 |
case 'option_is' |
| 303 |
set -l options |
| 304 |
set -l values |
| 305 |
|
| 306 |
for arg in $argv |
| 307 |
set -e argv[1] |
| 308 |
|
| 309 |
if string match -q -- -- $arg |
| 310 |
set values $argv |
| 311 |
break |
| 312 |
else |
| 313 |
set -a options $arg |
| 314 |
end |
| 315 |
end |
| 316 |
|
| 317 |
if test (count $values) -eq 0 |
| 318 |
echo "%FUNCNAME%: missing values" >&2 |
| 319 |
return 1 |
| 320 |
end |
| 321 |
|
| 322 |
set -l i (count $having_options) |
| 323 |
while test $i -ge 1 |
| 324 |
if contains -- $having_options[$i] $options |
| 325 |
if contains -- $option_values[$i] $values |
| 326 |
return 0 |
| 327 |
end |
| 328 |
end |
| 329 |
|
| 330 |
set i (math $i - 1) |
| 331 |
end |
| 332 |
|
| 333 |
return 1 |
| 334 |
#endif |
| 335 |
#ifdef DEBUG |
| 336 |
case '*' |
| 337 |
echo "%FUNCNAME%: argv[2]: invalid command" >&2 |
| 338 |
return 1 |
| 339 |
#endif |
| 340 |
end |
| 341 |
end |
| 342 |
|
| 343 |
function __fish_complete_filedir -d "Complete files or directories" |
| 344 |
# Function for completing files or directories |
| 345 |
# |
| 346 |
# Options: |
| 347 |
# -d|--description=DESC The description for completed entries |
| 348 |
# -c|--comp=STR Complete STR instead of current command line argument |
| 349 |
# -D|--directories Only complete directories |
| 350 |
# -C|--cd=DIR List contents in DIR |
| 351 |
# |
| 352 |
# This function is made out of /usr/share/fish/functions/__fish_complete_directories.fish |
| 353 |
|
| 354 |
argparse --max-args 0 'd/description=' 'c/comp=' 'D/directories' 'C/cd=' -- $argv || return 1 |
| 355 |
|
| 356 |
set -l comp |
| 357 |
set -l desc |
| 358 |
|
| 359 |
if set -q _flag_description[1] |
| 360 |
set desc $_flag_description |
| 361 |
else if set -g _flag_directories |
| 362 |
set desc 'Directory' |
| 363 |
end |
| 364 |
|
| 365 |
if set -q _flag_comp[1] |
| 366 |
set comp $_flag_comp |
| 367 |
else |
| 368 |
set comp (commandline -ct | string replace -r -- '^-[^=]*=' '') |
| 369 |
end |
| 370 |
|
| 371 |
if set -q _flag_cd[1] |
| 372 |
pushd $_flag_cd || return 1 |
| 373 |
end |
| 374 |
|
| 375 |
set -l files (complete -C"'' $comp") |
| 376 |
|
| 377 |
if set -q _flag_cd[1] |
| 378 |
popd |
| 379 |
end |
| 380 |
|
| 381 |
if set -q files[1] |
| 382 |
if set -q _flag_directories[1] |
| 383 |
set files (printf "%s\n" $files | string match -r '.*/$') |
| 384 |
end |
| 385 |
|
| 386 |
printf "%s\n" $files\t"$desc" |
| 387 |
end |
| 388 |
end |
| 389 |
|
| 390 |
function simple_values |
| 391 |
set -l SEPARATOR $argv[1] |
| 392 |
set -e argv[1] |
| 393 |
set -l VALUES $argv |
| 394 |
|
| 395 |
set -l cmdline (commandline -po) |
| 396 |
set -l cur $cmdline[(count $cmdline)] |
| 397 |
|
| 398 |
set -S cmdline cur >> /tmp/trace |
| 399 |
|
| 400 |
set -l VALUE |
| 401 |
set -l c (commandline -b) |
| 402 |
|
| 403 |
if test (string sub -s -1 -l 1 -- $c) = ' ' |
| 404 |
for VALUE in $VALUES |
| 405 |
printf "%s\n" $VALUE |
| 406 |
end |
| 407 |
return |
| 408 |
end |
| 409 |
|
| 410 |
set -l HAVING_VALUES (string split -- $SEPARATOR $cur) |
| 411 |
set -l REMAINING_VALUES |
| 412 |
set -l HAVING_VALUE |
| 413 |
|
| 414 |
for VALUE in $VALUES |
| 415 |
set -l FOUND_VALUE false |
| 416 |
|
| 417 |
for HAVING_VALUE in $HAVING_VALUES |
| 418 |
echo "test '$VALUE' = '$HAVING_VALUE'" >> /tmp/trace |
| 419 |
if test $VALUE = $HAVING_VALUE |
| 420 |
set FOUND_VALUE true |
| 421 |
break |
| 422 |
end |
| 423 |
end |
| 424 |
|
| 425 |
if ! $FOUND_VALUE |
| 426 |
set -a REMAINING_VALUES $VALUE |
| 427 |
end |
| 428 |
end |
| 429 |
|
| 430 |
if string match -q -r -- $SEPARATOR'$' $cur # TODO |
| 431 |
for VALUE in $REMAINING_VALUES |
| 432 |
printf "%s%s\n" $cur $VALUE |
| 433 |
end |
| 434 |
else if test (count $HAVING_VALUES) -gt 0 |
| 435 |
set CUR_LAST_VALUE $HAVING_VALUES[-1] |
| 436 |
|
| 437 |
for VALUE in $REMAINING_VALUES |
| 438 |
if string match -q -- $CUR_LAST_VALUE'*' $VALUE |
| 439 |
printf "%s\n" $VALUE |
| 440 |
end |
| 441 |
end |
| 442 |
end |
| 443 |
end |
| 444 |
|
| 445 |
function _has_hidden_option |
| 446 |
set -l option |
| 447 |
set -l tokens (commandline -po) |
| 448 |
set -e tokens[1] |
| 449 |
|
| 450 |
for option in $argv |
| 451 |
switch $option |
| 452 |
case '-?' |
| 453 |
set option (string sub -l 1 -s 2 -- $option) |
| 454 |
string match -q -r -- "^-[A-z0-9]*$option\$" $tokens[-2] && return 0 |
| 455 |
string match -q -r -- "^-[A-z0-9]*$option.*\$" $tokens[-1] && return 0 |
| 456 |
case '*' |
| 457 |
string match -q -r -- "^$option\$" $tokens[-2] && return 0 |
| 458 |
string match -q -r -- "^$option(=.*)?\$" $tokens[-1] && return 0 |
| 459 |
end |
| 460 |
end |
| 461 |
|
| 462 |
echo not enabled >> /tmp/trace |
| 463 |
return 1 |
| 464 |
end |
| 465 |
|
| 466 |
# ============================================================================= |
| 467 |
# Test code |
| 468 |
# ============================================================================= |
| 469 |
|
| 470 |
set -l options "--arg=,-a=,-oldarg=,-optional=?,--flag,-f,-flag" |
| 471 |
|
| 472 |
complete -c foo -e |
| 473 |
complete -c foo -l flag -o flag -s f -f |
| 474 |
complete -c foo -n "__fish_query '$options' num_of_positionals -eq 0" -x -a 'first' |
| 475 |
complete -c foo -n "__fish_query '$options' positional_contains 1 first && not __fish_query '$options' has_option -a --arg -oldarg" -f -s a -l arg -o oldarg -r -a '1 2 3' |
| 476 |
complete -c foo -n "__fish_query '$options' positional_contains 1 first && not __fish_query '$options' has_option -optional" -f -o optional -a '1 2 3' |
| 477 |
|
| 478 |
complete -c foo -l files -x -a '(__fish_complete_filedir)' |
| 479 |
complete -c foo -l dirs -x -a '(__fish_complete_filedir -D)' |
| 480 |
complete -c foo -l temp-files -x -a '(__fish_complete_filedir -C /tmp)' |
| 481 |
complete -c foo -l temp-dirs -x -a '(__fish_complete_filedir -D -C /tmp)' |
| 482 |
complete -c foo -l desc-files -x -a '(__fish_complete_filedir -d Foo)' |
| 483 |
|
| 484 |
complete -c foo -l simple-values -x -a '(simple_values , foo bar baz)' |
| 485 |
|
| 486 |
complete -c foo -l hidden -o hidden -s h -x -a 'one two three' -n '_has_hidden_option -h --hidden -hidden' |