From f156bea1b7183b99d170328bacfda2d54acecb3d Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 13 Jul 2017 11:41:00 -0700 Subject: [PATCH] convert `alias` to use `argparse` --- share/functions/alias.fish | 51 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/share/functions/alias.fish b/share/functions/alias.fish index e7c1d983a..a1bd3603f 100644 --- a/share/functions/alias.fish +++ b/share/functions/alias.fish @@ -1,10 +1,11 @@ function alias --description 'Creates a function wrapping a command' - if count $argv >/dev/null - switch $argv[1] - case -h --h --he --hel --help - __fish_print_help alias - return 0 - end + set -l options 'h/help' + argparse -n alias --max-args=2 $options -- $argv + or return + + if set -q _flag_help + __fish_print_help alias + return 0 end set -l name @@ -12,27 +13,25 @@ function alias --description 'Creates a function wrapping a command' set -l prefix set -l first_word set -l wrapped_cmd - switch (count $argv) - case 0 - for func in (functions -n) - set -l output (functions $func | string match -r -- "function .* --description '(alias .*)'" | string split \n) - set -q output[2] - and echo $output[2] - end - return 0 - case 1 - set -l tmp (string split -m 1 "=" -- $argv) "" - set name $tmp[1] - set body $tmp[2] - - case 2 - set name $argv[1] - set body $argv[2] - - case \* - printf ( _ "%s: Expected one or two arguments, got %d\n") alias (count $argv) - return 1 + if not set -q argv[1] + # Print the known aliases. + for func in (functions -n) + set -l output (functions $func | string match -r -- "function .* --description '(alias .*)'" | string split \n) + set -q output[2] + and echo $output[2] + end + return 0 + else if not set -q argv[2] + # Alias definition of the form "name=value". + set -l tmp (string split -m 1 "=" -- $argv) "" + show $tmp + set name $tmp[1] + set body $tmp[2] + else + # Alias definition of the form "name value". + set name $argv[1] + set body $argv[2] end # sanity check