mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-12 13:08:49 +00:00
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:
parent
0d08bfd6ff
commit
3c4e3035fd
2 changed files with 39 additions and 119 deletions
|
@ -16,71 +16,33 @@ function __trap_switch
|
|||
end
|
||||
|
||||
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 cmd
|
||||
set -l sig
|
||||
|
||||
set -l options
|
||||
set -l longopt
|
||||
set -l shortopt lph
|
||||
if not getopt -T >/dev/null
|
||||
# GNU getopt
|
||||
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
|
||||
# Determine the mode based on either an explicit flag or the non-flag args.
|
||||
if set -q _flag_print
|
||||
set mode print
|
||||
else if set -q _flag_list_signals
|
||||
set mode list
|
||||
else
|
||||
# Old getopt, used on OS X
|
||||
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)
|
||||
|
||||
switch (count $argv)
|
||||
case 0
|
||||
set mode print
|
||||
|
||||
case 1
|
||||
set mode clear
|
||||
|
||||
case '*'
|
||||
if test opt[1] = -
|
||||
set -e opt[1]
|
||||
if test $argv[1] = -
|
||||
set -e argv[1]
|
||||
set mode clear
|
||||
else
|
||||
set mode set
|
||||
|
@ -90,7 +52,7 @@ function trap -d 'Perform an action when the shell receives a signal'
|
|||
|
||||
switch $mode
|
||||
case clear
|
||||
for i in $opt
|
||||
for i in $argv
|
||||
set sig (__trap_translate_signal $i)
|
||||
if test $sig
|
||||
functions -e __trap_handler_$sig
|
||||
|
@ -98,10 +60,10 @@ function trap -d 'Perform an action when the shell receives a signal'
|
|||
end
|
||||
|
||||
case set
|
||||
set -l cmd $opt[1]
|
||||
set -e opt[1]
|
||||
set -l cmd $argv[1]
|
||||
set -e argv[1]
|
||||
|
||||
for i in $opt
|
||||
for i in $argv
|
||||
set -l sig (__trap_translate_signal $i)
|
||||
set sw (__trap_switch $sig)
|
||||
|
||||
|
@ -114,15 +76,13 @@ function trap -d 'Perform an action when the shell receives a signal'
|
|||
|
||||
case print
|
||||
set -l names
|
||||
|
||||
if count $opt >/dev/null
|
||||
set names $opt
|
||||
if set -q argv[1]
|
||||
set names $argv
|
||||
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
|
||||
|
||||
for i in $names
|
||||
|
||||
set sig (__trap_translate_signal $i)
|
||||
|
||||
if test sig
|
||||
|
@ -130,12 +90,9 @@ function trap -d 'Perform an action when the shell receives a signal'
|
|||
else
|
||||
return 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
case list
|
||||
kill -l
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -149,67 +149,30 @@ function __fish_umask_print_symbolic
|
|||
end
|
||||
|
||||
function umask --description "Set default file permission mask"
|
||||
set -l as_command 0
|
||||
set -l symbolic 0
|
||||
set -l options
|
||||
set -l shortopt pSh
|
||||
if not getopt -T >/dev/null
|
||||
# GNU getopt
|
||||
set longopt -l as-command,symbolic,help
|
||||
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
|
||||
set -l options 'h/help' 'p/as-command' 'S/symbolic'
|
||||
argparse $options -- $argv
|
||||
or return
|
||||
|
||||
if set -q _flag_help
|
||||
__fish_print_help umask
|
||||
return 0
|
||||
end
|
||||
|
||||
set -l tmp (getopt $options $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)
|
||||
switch (count $argv)
|
||||
case 0
|
||||
if not set -q umask
|
||||
set -g umask 113
|
||||
end
|
||||
if test $as_command -eq 1
|
||||
set -q umask
|
||||
or set -g umask 113
|
||||
|
||||
if set -q _flag_as_command
|
||||
echo umask $umask
|
||||
else if set -q _flag_symbolic
|
||||
__fish_umask_print_symbolic $umask
|
||||
else
|
||||
if test $symbolic -eq 1
|
||||
__fish_umask_print_symbolic $umask
|
||||
else
|
||||
echo $umask
|
||||
end
|
||||
echo $umask
|
||||
end
|
||||
|
||||
case 1
|
||||
if set -l parsed (__fish_umask_parse $opt)
|
||||
if set -l parsed (__fish_umask_parse $argv)
|
||||
set -g umask $parsed
|
||||
return 0
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue