switch from getopt to argparse

Convert our two functions that use `getopt` to use our new `argparse`
builtin.

Fixes #4190
This commit is contained in:
Kurtis Rader 2017-07-09 15:36:17 -07:00
parent 0d08bfd6ff
commit 3c4e3035fd
2 changed files with 39 additions and 119 deletions

View file

@ -16,71 +16,33 @@ function __trap_switch
end end
function trap -d 'Perform an action when the shell receives a signal' function trap -d 'Perform an action when the shell receives a signal'
set -l options 'h/help' 'l/list-signals' 'p/print'
argparse $options -- $argv
or return
if set -q _flag_help
__fish_print_help trap
return 0
end
set -l mode set -l mode
set -l cmd set -l cmd
set -l sig set -l sig
set -l options # Determine the mode based on either an explicit flag or the non-flag args.
set -l longopt if set -q _flag_print
set -l shortopt lph set mode print
if not getopt -T >/dev/null else if set -q _flag_list_signals
# GNU getopt set mode list
set longopt print,help,list-signals
set options -o $shortopt -l $longopt --
# Verify options
if not getopt -n type $options $argv >/dev/null
return 1
end
else else
# Old getopt, used on OS X switch (count $argv)
set options $shortopt
# Verify options
if not getopt $options $argv >/dev/null
return 1
end
end
# Do the real getopt invocation
set -l tmp (getopt $options $argv)
# Break tmp up into an array
set -l opt
eval set opt $tmp
while count $opt >/dev/null
switch $opt[1]
case -h --help
__fish_print_help trap
return 0
case -p --print
set mode print
case -l --list-signals
set mode list
case --
set -e opt[1]
break
end
set -e opt[1]
end
if not count $mode >/dev/null
switch (count $opt)
case 0 case 0
set mode print set mode print
case 1 case 1
set mode clear set mode clear
case '*' case '*'
if test opt[1] = - if test $argv[1] = -
set -e opt[1] set -e argv[1]
set mode clear set mode clear
else else
set mode set set mode set
@ -90,7 +52,7 @@ function trap -d 'Perform an action when the shell receives a signal'
switch $mode switch $mode
case clear case clear
for i in $opt for i in $argv
set sig (__trap_translate_signal $i) set sig (__trap_translate_signal $i)
if test $sig if test $sig
functions -e __trap_handler_$sig functions -e __trap_handler_$sig
@ -98,10 +60,10 @@ function trap -d 'Perform an action when the shell receives a signal'
end end
case set case set
set -l cmd $opt[1] set -l cmd $argv[1]
set -e opt[1] set -e argv[1]
for i in $opt for i in $argv
set -l sig (__trap_translate_signal $i) set -l sig (__trap_translate_signal $i)
set sw (__trap_switch $sig) set sw (__trap_switch $sig)
@ -114,15 +76,13 @@ function trap -d 'Perform an action when the shell receives a signal'
case print case print
set -l names set -l names
if set -q argv[1]
if count $opt >/dev/null set names $argv
set names $opt
else else
set names (functions -na| string match "__trap_handler_*" | string replace '__trap_handler_' '') set names (functions -na | string match "__trap_handler_*" | string replace '__trap_handler_' '')
end end
for i in $names for i in $names
set sig (__trap_translate_signal $i) set sig (__trap_translate_signal $i)
if test sig if test sig
@ -130,12 +90,9 @@ function trap -d 'Perform an action when the shell receives a signal'
else else
return 1 return 1
end end
end end
case list case list
kill -l kill -l
end end
end end

View file

@ -149,67 +149,30 @@ function __fish_umask_print_symbolic
end end
function umask --description "Set default file permission mask" function umask --description "Set default file permission mask"
set -l as_command 0 set -l options 'h/help' 'p/as-command' 'S/symbolic'
set -l symbolic 0 argparse $options -- $argv
set -l options or return
set -l shortopt pSh
if not getopt -T >/dev/null if set -q _flag_help
# GNU getopt __fish_print_help umask
set longopt -l as-command,symbolic,help return 0
set options -o $shortopt $longopt --
# Verify options
if not getopt -n umask $options $argv >/dev/null
return 1
end
else
# Old getopt, used on OS X
set options $shortopt
# Verify options
if not getopt $options $argv >/dev/null
return 1
end
end end
set -l tmp (getopt $options $argv) switch (count $argv)
eval set opt $tmp
while count $opt >/dev/null
switch $opt[1]
case -h --help
__fish_print_help umask
return 0
case -p --as-command
set as_command 1
case -S --symbolic
set symbolic 1
case --
set -e opt[1]
break
end
set -e opt[1]
end
switch (count $opt)
case 0 case 0
if not set -q umask set -q umask
set -g umask 113 or set -g umask 113
end
if test $as_command -eq 1 if set -q _flag_as_command
echo umask $umask echo umask $umask
else if set -q _flag_symbolic
__fish_umask_print_symbolic $umask
else else
if test $symbolic -eq 1 echo $umask
__fish_umask_print_symbolic $umask
else
echo $umask
end
end end
case 1 case 1
if set -l parsed (__fish_umask_parse $opt) if set -l parsed (__fish_umask_parse $argv)
set -g umask $parsed set -g umask $parsed
return 0 return 0
end end