From 49a0841533a457792f453c9e69e0b5c91bb86692 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 13 Jul 2017 15:19:02 -0700 Subject: [PATCH] update `type` to use `argparse` --- doc_src/type.txt | 2 + .../functions/__fish_config_interactive.fish | 4 +- share/functions/type.fish | 81 +++++++------------ 3 files changed, 35 insertions(+), 52 deletions(-) diff --git a/doc_src/type.txt b/doc_src/type.txt index 2da88c94d..2d1a56abf 100644 --- a/doc_src/type.txt +++ b/doc_src/type.txt @@ -23,6 +23,8 @@ The following options are available: - `-q` or `--quiet` suppresses all output; this is useful when testing the exit status. +The `-q`, `-p`, `-t` and `-P` flags (and their long flag aliases) are mutually exclusive. Only one can be specified at a time. + \subsection type-example Example diff --git a/share/functions/__fish_config_interactive.fish b/share/functions/__fish_config_interactive.fish index 5c191311d..b573b26f3 100644 --- a/share/functions/__fish_config_interactive.fish +++ b/share/functions/__fish_config_interactive.fish @@ -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 diff --git a/share/functions/type.fish b/share/functions/type.fish index 25ba5953e..32ddc4ef2 100644 --- a/share/functions/type.fish +++ b/share/functions/type.fish @@ -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