fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerPrepare to not localize private function descriptionsd835c5252

master
1.3 KiB34 linesraw
1# localization: tier1
2#
3# This function is intended to be used as a validation command for individual option specifications
4# given to the `argparse` command. It checks that the argument is a valid integer and optionally
5# whether it is in a reasonable range.
6function _validate_int --no-scope-shadowing
7    # Note that we can't use ourself to validate the arguments to the --min and --max flags this
8    # function recognizes.
9    set -l options 'm-min=' 'x-max='
10    argparse -n _argparse_validate_int $options -- $argv
11    or return
12
13    if not string match -qr '^-?\d+$' -- $_flag_value
14        set -l msg (_ "%s: Value '%s' for flag '%s' is not an integer\n")
15        printf $msg $_argparse_cmd $_flag_value $_flag_name >&2
16        return 1
17    end
18
19    if set -q _flag_min
20        and test $_flag_value -lt $_flag_min
21        set -l msg (_ "%s: Value '%s' for flag '%s' less than min allowed of '%s'\n")
22        printf $msg $_argparse_cmd $_flag_value $_flag_name $_flag_min >&2
23        return 1
24    end
25
26    if set -q _flag_max
27        and test $_flag_value -gt $_flag_max
28        set -l msg (_ "%s: Value '%s' for flag '%s' greater than max allowed of '%s'\n")
29        printf $msg $_argparse_cmd $_flag_value $_flag_name $_flag_max >&2
30        return 1
31    end
32
33    return 0
34end