fish-shell/tests/argparse.in
Kurtis Rader 3e226f0a5e implement a new implicit int option spec
While updating the `history` function to use `argparse` I realized it is
useful to define an option that can be used in three ways. First by
using the short flag; e.g., `-n NNN`. Second by using the long flag;
e.g., `--max NNN`. Third, as an implicit int flag; e.g., `-NNN`. This
use case is now supported by a spec of the form `n#max`.
2017-07-16 18:27:41 -07:00

100 lines
2.6 KiB
Fish

# Test the `argparse` builtin.
##########
# Start by verifying a bunch of error conditions.
echo '# No args is an error' >&2
argparse
echo '# Missing -- is an error' >&2
argparse h/help
echo '# Flags but no option specs is an error' >&2
argparse -s -- hello
echo '# Invalid option specs' >&2
argparse h-
argparse +help
argparse h/help:
argparse h-help::
argparse h-help=x
echo '# --max-args and --min-args work' >&2
begin
argparse --name min-max --min-args 1 h/help --
argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1
argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1 arg2
argparse --name min-max --min-args 1 --max-args 3 h/help -- --help arg1 arg2 arg3
argparse --name min-max --min-args 1 --max-args 3 h/help -- arg1 arg2 -h arg3 arg4
argparse --name min-max --max-args 1 h/help --
argparse --name min-max --max-args 1 h/help -- arg1
argparse --name min-max --max-args 1 h/help -- arg1 arg2
end
echo '# Invalid "#-val" spec' >&2
argparse '#-val=' -- abc -x def
echo '# Invalid arg in the face of a "#-val" spec' >&2
argparse '#-val' -- abc -x def
echo '# Defining a short flag more than once' >&2
argparse 's/short' 'x/xray' 's/long' -- -s -x --long
echo '# Defining a long flag more than once' >&2
argparse 's/short' 'x/xray' 'l/short' -- -s -x --long
echo '# Defining an implicit int flag more than once' >&2
argparse '#-val' 'x/xray' 'v#val' -- -s -x --long
echo '# Defining an implicit int flag with modifiers' >&2
argparse 'v#val=' --
##########
# Now verify that validly formed invocations work as expected.
echo '# No args'
argparse h/help --
echo '# One arg and no matching flags'
begin
argparse h/help -- help
set -l
show $argv
end
echo '# Five args with two matching a flag'
begin
argparse h/help -- help --help me -h 'a lot more'
set -l
show $argv
end
echo '# Required, optional, and multiple flags'
begin
argparse 'h/help' 'a/abc=' 'd/def=?' 'g/ghk=+' -- help --help me --ghk=g1 --abc=ABC --ghk g2 --d -g g3
set -l
show $argv
end
echo '# --stop-nonopt works'
begin
argparse --stop-nonopt 'h/help' 'a/abc=' -- -a A1 -h --abc A2 non-opt 'second non-opt' --help
set -l
show $argv
end
echo '# Implicit int flags work'
for v in (set -l -n); set -e $v; end
argparse '#-val' -- abc -123 def
set -l
for v in (set -l -n); set -e $v; end
argparse 'v/verbose' '#-val' 't/token=' -- -123 a1 --token woohoo --234 -v a2 --verbose
set -l
echo '# Should be set to 987'
for v in (set -l -n); set -e $v; end
argparse 'm#max' -- argle -987 bargle
set -l
echo '# Should be set to 765'
for v in (set -l -n); set -e $v; end
argparse 'm#max' -- argle -987 bargle --max 765
set -l