type: Rewrite option parsing

Removes a call to `seq` and makes it a bit more readable.
This commit is contained in:
Fabian Homborg 2017-03-24 17:27:48 +01:00
parent a3f28e221f
commit c4d69ea8a1

View file

@ -1,77 +1,58 @@
function type --description 'Print the type of a command'
function type --description "Print the type of a command" # For legacy reasons, no argument simply causes an unsuccessful return.
if not set -q argv[1]
return 1
end
# Initialize # Initialize
set -l res 1 set -l res 1
set -l mode normal set -l mode normal
set -l multi no set -l multi no
set -l selection all set -l selection all
set -l IFS \n\ \t
# Parse options # Parse options
set -l names set -l names
if test (count $argv) -gt 0 while set -q argv[1]
for i in (seq (count $argv)) set -l arg $argv[1]
set -l arg $argv[$i] set -e argv[1]
set -l needbreak 0 switch $arg
while test -n $arg case -t --type
set -l flag $arg # This could also be an error
set arg '' # - printing type without printing anything
switch $flag # doesn't make sense.
case '--*' if test $mode != quiet
# do nothing; this just prevents it matching the next case set mode type
case '-??*'
# combined flags
set -l IFS
echo -n $flag | read __ flag arg
set flag -$flag
set arg -$arg
end end
switch $flag case -p --path
case -t --type if test $mode != quiet
if test $mode != quiet set mode path
set mode type
end
case -p --path
if test $mode != quiet
set mode path
end
case -P --force-path
if test $mode != quiet
set mode path
end
set selection files
case -a --all
set multi yes
case -f --no-functions
set selection files
case -q --quiet
set mode quiet
case -h --help
__fish_print_help type
return 0
case --
set names $argv[$i..-1]
set -e names[1]
set needbreak 1
break
case '*'
set names $argv[$i..-1]
set needbreak 1
break
end end
end case -P --force-path
if test $needbreak -eq 1 if test $mode != quiet
set mode path
end
set selection files
case -a --all
set multi yes
case -f --no-functions
set selection files
case -q --quiet
set mode quiet
case -h --help
__fish_print_help type
return 0
case --
set names $argv
break
case '-?' '--*'
printf (_ "%s: Unknown option %s\n" ) type $arg
return 1
case '-??*'
# Grouped options
set argv -(string sub -s 2 -- $arg | string split "") $argv
case '*'
set names $arg $argv
break break
end
end end
end end