update type to use argparse

This commit is contained in:
Kurtis Rader
2017-07-13 15:19:02 -07:00
parent 51bbecc419
commit 49a0841533
3 changed files with 35 additions and 52 deletions

View File

@@ -289,7 +289,7 @@ function __fish_config_interactive -d "Initializations that should be performed
# First check if we are on OpenSUSE since SUSE's handler has no options
# but the same name and path as Ubuntu's.
if contains -- suse $os
and type -q -p command-not-found
and type -q command-not-found
function __fish_command_not_found_handler --on-event fish_command_not_found
/usr/bin/command-not-found $argv[1]
end
@@ -309,7 +309,7 @@ function __fish_config_interactive -d "Initializations that should be performed
/run/current-system/sw/bin/command-not-found $argv
end
# Ubuntu Feisty places this command in the regular path instead
else if type -q -p command-not-found
else if type -q command-not-found
function __fish_command_not_found_handler --on-event fish_command_not_found
command-not-found -- $argv[1]
end

View File

@@ -1,64 +1,45 @@
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
set -q argv[1]
or return 1
set -l options 'h/help' 'a/all' 'f/no-functions' 't/type' 'p/path' 'P/force-path' 'q/quiet'
argparse -n type --min-args=1 -x t,p,P $options -- $argv
or return
if set -q _flag_help
__fish_print_help type
return 0
end
# Initialize
set -l res 1
set -l mode normal
set -l multi no
set -l selection all
# Parse options
set -l names
while set -q argv[1]
set -l arg $argv[1]
set -e argv[1]
switch $arg
case -t --type
# This could also be an error
# - printing type without printing anything
# doesn't make sense.
if test $mode != quiet
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
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
end
# Technically all four of these flags are mutually exclusive. However, we allow -q to be used
# with the other three because old versions of this function explicitly allowed it by making
# --quiet have precedence.
if set -q _flag_quiet
set mode quiet
else if set -q _flag_type
set mode type
else if set -q _flag_path
set mode path
else if set -q _flag_force_path
set mode path
set selection files
end
# Check all possible types for the remaining arguments
for i in $names
# Found will be set to 1 if a match is found
set -q _flag_all
and set multi yes
set -q _flag_no_functions
and set selection files
# Check all possible types for the remaining arguments.
for i in $argv
# Found will be set to 1 if a match is found.
set -l found 0
if test $selection != files