From: Matthew Martin <phy1729@gmail.com>
To: zsh-workers@zsh.org
Subject: _os_arguments
Date: Sun, 11 Dec 2016 23:28:03 -0600 [thread overview]
Message-ID: <20161212052803.GA53519@CptOrmolo.darkstar> (raw)
Also for when 5.3 is settled.
When updating completions for the BSDs, it can be a bit of a pain to
check each case or if block especially since the alphabetization of the
options is disrupted. This is an attempt to ease that pain. First an
example usage converting _rm to _os_arguments then the patch itself.
Hopefully the yodl looks decent.
- Matthew Martin
diff --git a/Completion/Unix/Command/_rm b/Completion/Unix/Command/_rm
index 6d728b157..0c5b526f1 100644
--- a/Completion/Unix/Command/_rm
+++ b/Completion/Unix/Command/_rm
@@ -23,32 +23,20 @@ if _pick_variant gnu=gnu unix --help; then
)
else
args=(${args:#*)--*\[*})
- case $OSTYPE in
- dragonfly*|freebsd*|netbsd*|openbsd*)
- args+=(
- '-d[remove directories as well]'
- '-P[overwrite files before deleting them]'
- )
- ;|
- dragonfly*|freebsd*|netbsd*)
- args+=(
- '-v[explain what is being done]'
- '-W[attempt to undelete named files]'
- "-x[don't cross file systems when removing a hierarchy]"
- )
- ;|
- dragonfly*|freebsd*)
- args+=(
- '(-i)-I[prompt when removing many files]'
- )
- ;;
- esac
+ args+=(
+ DFNO '-d[remove directories as well]'
+ DF '(-i)-I[prompt when removing many files]'
+ DFNO '-P[overwrite files before deleting them]'
+ DFN '-v[explain what is being done]'
+ DFN '-W[attempt to undelete named files]'
+ DFN "-x[don't cross file systems when removing a hierarchy]"
+ )
fi
local curcontext=$curcontext state line ret=1
declare -A opt_args
-_arguments -C -s $opts \
+_os_arguments -C -s $opts : \
$args && ret=0
case $state in
diff --git a/Completion/Base/Utility/_os_arguments b/Completion/Base/Utility/_os_arguments
new file mode 100755
index 000000000..44ba19c05
--- /dev/null
+++ b/Completion/Base/Utility/_os_arguments
@@ -0,0 +1,36 @@
+#autoload
+
+local i os s=2
+local -a args
+
+i=${@[(i):]}
+(( i > $# )) && i=0
+args=(${@[1,i]})
+shift $i
+
+case $OSTYPE in
+ aix*) os=A;;
+ cygwin*) os=C;;
+ dragonfly*) os=D;;
+ freebsd*) os=F;;
+ hpux*) os=H;;
+ irix*) os=I;;
+ linux*) os=L;;
+ darwin*) os=M;;
+ netbsd*) os=N;;
+ openbsd*) os=O;;
+ solaris*) os=S;;
+esac
+
+for 1; do
+ if [[ $1 == [[:upper:]]## ]]; then
+ (( s == 2 )) && s=0
+ [[ $1 == *$os* ]] && s=1
+ else
+ (( s )) || continue
+ args+=($1)
+ s=2
+ fi
+done
+
+_arguments "$args[@]"
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 953d51c4c..e75aa7062 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -4624,6 +4624,60 @@ are used to store the option settings in effect before the completion
widget locally sets the options it needs. Hence these functions are not
generally used by the completion system.
)
+findex(_os_arguments)
+xitem(tt(_os_arguments )[ var(_arguments flags) tt(:) ] {var(OS selector)|var(spec)} )
+This function wraps tt(_arguments) to ease completing multiple OSes with
+differing flags. The arguments up to the first colon are passed verbatim
+to tt(_arguments). Following arguments are either OS selectors,
+consisting of all uppercase letters, or tt(_arguments) specs. Each OS is
+assigned a letter (see below). If that letter appears in an OS selector
+since the last spec (or if there is no proceeding OS selector), the
+following specs are passed to tt(_arguments) until the next OS selector.
+
+startitem()
+item(tt(A))(
+AIX
+)
+item(tt(C))(
+Cygwin
+)
+item(tt(D))(
+DragonFlyBSD
+)
+item(tt(F))(
+FreeBSD
+)
+item(tt(H))(
+HP-UX
+)
+item(tt(I))(
+IRIX
+)
+item(tt(L))(
+Linux
+)
+item(tt(M))(
+macOS
+)
+item(tt(N))(
+NetBSD
+)
+item(tt(O))(
+OpenBSD
+)
+item(tt(S))(
+Solaris
+)
+enditem()
+
+example(_os_arguments -s : \\ \
+ '-n[number all output lines]' \\ \
+ '-u[do not buffer output]' \\ \
+ '(-)*:files:_files' \\ \
+ N '-B[use specified buffer size]:buffer size:' \\ \
+ F N '-l[set a lock on the stdout file descriptor]' \\ \
+ DFMNO '-v[display non-printing chars as ^X or M-a]'
+)
findex(_parameters)
item(tt(_parameters))(
This is used to complete the names of shell parameters.
next reply other threads:[~2016-12-12 5:43 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-12 5:28 Matthew Martin [this message]
2016-12-12 8:52 ` _os_arguments Daniel Shahaf
2016-12-13 0:54 ` _os_arguments Oliver Kiddle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161212052803.GA53519@CptOrmolo.darkstar \
--to=phy1729@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).