--option-parsing: useful for completion

This commit is contained in:
boyska 2012-01-23 03:34:25 +01:00
parent 4a3d59c3a4
commit 72b44a84f6

View File

@ -1603,19 +1603,19 @@ main() {
# to add some options? Well, keep in mind that an option CAN'T # to add some options? Well, keep in mind that an option CAN'T
# have differente meanings/behaviour in different subcommands. # have differente meanings/behaviour in different subcommands.
# For example, "-s" means "size" and accept an argument. If you are tempted to add # For example, "-s" means "size" and accept an argument. If you are tempted to add
# an option "-s" (that means, for example "silent", and doesn't accept an argument) # an option "-s" (that means, for example "silent", and doesn't accept an argument)
# DON'T DO IT! # DON'T DO IT!
# There are two reasons for that: # There are two reasons for that:
# I. usability; user expect that "-s" is "size" # I. usability; user expect that "-s" is "size"
# II. Option parsing WILL EXPLODE if you do this kind of bad things # II. Option parsing WILL EXPLODE if you do this kind of bad things
# (it will say "option defined more than once, and he's right") # (it will say "option defined more than once, and he's right")
# #
# If you want to use the same option in multiple commands then # If you want to use the same option in multiple commands then
# you can only use the non-abbreviated long-option version like: # you can only use the non-abbreviated long-option version like:
# -force and NOT -f # -force and NOT -f
main_opts=(q -quiet=q D -debug=D h -help=h v -version=v -no-color) main_opts=(q -quiet=q D -debug=D h -help=h v -version=v -no-color)
subcommands_opts[__default]="" subcommands_opts[__default]=""
subcommands_opts[open]="f n -nohook=n k: -key=k o: -mount-options=o" subcommands_opts[open]="f n -nohook=n k: -key=k o: -mount-options=o"
subcommands_opts[mount]=${subcommands_opts[open]} subcommands_opts[mount]=${subcommands_opts[open]}
@ -1648,7 +1648,20 @@ main() {
done done
local -a oldstar local -a oldstar
oldstar=($argv) oldstar=($argv)
zparseopts -M -E -D -Adiscardme ${every_opts} #### detect early: useful for --optiion-parsing
zparseopts -M -D -Adiscardme ${every_opts}
if [[ -n ${(k)discardme[--option-parsing]} ]]; then
echo $1
if [[ -n "$1" ]]; then
return 1
fi
return 0
fi
unset discardme
if ! zparseopts -M -E -D -Adiscardme ${every_opts}; then
error "error parsing"
return 127
fi
unset discardme unset discardme
subcommand=$1 subcommand=$1
if [[ -z $subcommand ]]; then if [[ -z $subcommand ]]; then
@ -1783,8 +1796,9 @@ EOF
# {{{ RUNTIME # {{{ RUNTIME
check_bin check_bin
main $@ main $@
if [[ $? != 0 ]]; then #this "if" seems useless, but avoid source tomb source from exiting ret=$?
exit $? if [[ $ret != 0 ]]; then #this "if" seems useless, but avoid source tomb source from exiting
exit $ret
fi fi
# }}} # }}}