From 6dc74d3b6cae9178c3707b060712a5b35824a0b5 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 6 May 2018 18:46:56 -0500 Subject: [PATCH] New helper functions `__fish_is_first_arg` and `__fish_prev_arg_in` For usage in completion scripts. Unlike `__fish_is_first_token` (which is probably not correctly named), `__fish_is_first_arg` returns true regardless of whether existing tokens start with `-` or not, to be used when an arg cannot be used with any other argument. `__fish_prev_arg_in` is similar to `__fish_seen_...` but it explicitly tests the preceding token only, for arguments that take only a single parameter. --- share/functions/__fish_is_first_arg.fish | 5 +++++ share/functions/__fish_prev_arg_in.fish | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 share/functions/__fish_is_first_arg.fish create mode 100644 share/functions/__fish_prev_arg_in.fish diff --git a/share/functions/__fish_is_first_arg.fish b/share/functions/__fish_is_first_arg.fish new file mode 100644 index 000000000..b026a11df --- /dev/null +++ b/share/functions/__fish_is_first_arg.fish @@ -0,0 +1,5 @@ +# determine if this is the very first argument (regardless if switch or not) +function __fish_is_first_arg + set -l tokens (commandline -co) + test (count $tokens) -eq 1 +end diff --git a/share/functions/__fish_prev_arg_in.fish b/share/functions/__fish_prev_arg_in.fish new file mode 100644 index 000000000..1fd1662bd --- /dev/null +++ b/share/functions/__fish_prev_arg_in.fish @@ -0,0 +1,16 @@ +# returns 0 only if previous argument is one of the supplied arguments +function __fish_prev_arg_in + set -l tokens (commandline -co) + set -l tokenCount (count $tokens) + if test $tokenCount -lt 2 + # need at least cmd and prev argument + return 1 + end + for arg in $argv + if string match -q -- $tokens[-1] $arg + return 0 + end + end + + return 1 +end