2017-07-13 13:12:41 +08:00
|
|
|
# Test the `argparse` builtin.
|
|
|
|
|
|
|
|
##########
|
|
|
|
# Start by verifying a bunch of error conditions.
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg No args is an error
|
2017-07-13 13:12:41 +08:00
|
|
|
argparse
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Missing -- is an error
|
2017-07-13 13:12:41 +08:00
|
|
|
argparse h/help
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Flags but no option specs is an error
|
2017-07-13 13:12:41 +08:00
|
|
|
argparse -s -- hello
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Invalid option specs
|
2017-07-13 13:12:41 +08:00
|
|
|
argparse h-
|
|
|
|
argparse +help
|
|
|
|
argparse h/help:
|
|
|
|
argparse h-help::
|
|
|
|
argparse h-help=x
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg --max-args and --min-args work
|
2017-07-13 13:12:41 +08:00
|
|
|
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
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Invalid \"#-val\" spec
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse '#-val=' -- abc -x def
|
|
|
|
end
|
2017-07-14 11:36:59 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Invalid arg in the face of a \"#-val\" spec
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse '#-val' -- abc -x def
|
|
|
|
end
|
2017-07-14 11:36:59 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Defining a short flag more than once
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 's/short' 'x/xray' 's/long' -- -s -x --long
|
|
|
|
end
|
2017-07-17 09:27:41 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Defining a long flag more than once
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 's/short' 'x/xray' 'l/short' -- -s -x --long
|
|
|
|
end
|
2017-07-17 09:27:41 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Defining an implicit int flag more than once
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse '#-val' 'x/xray' 'v#val' -- -s -x --long
|
|
|
|
end
|
2017-07-17 09:27:41 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Defining an implicit int flag with modifiers
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'v#val=' --
|
|
|
|
end
|
2017-07-17 09:27:41 +08:00
|
|
|
|
2017-07-13 13:12:41 +08:00
|
|
|
##########
|
|
|
|
# Now verify that validly formed invocations work as expected.
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg No args
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse h/help --
|
|
|
|
end
|
2017-07-13 13:12:41 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg One arg and no matching flags
|
2017-07-13 13:12:41 +08:00
|
|
|
begin
|
|
|
|
argparse h/help -- help
|
|
|
|
set -l
|
|
|
|
end
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Five args with two matching a flag
|
2017-07-13 13:12:41 +08:00
|
|
|
begin
|
|
|
|
argparse h/help -- help --help me -h 'a lot more'
|
|
|
|
set -l
|
|
|
|
end
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Required, optional, and multiple flags
|
2017-07-13 13:12:41 +08:00
|
|
|
begin
|
|
|
|
argparse 'h/help' 'a/abc=' 'd/def=?' 'g/ghk=+' -- help --help me --ghk=g1 --abc=ABC --ghk g2 --d -g g3
|
|
|
|
set -l
|
|
|
|
end
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg --stop-nonopt works
|
2017-07-13 13:12:41 +08:00
|
|
|
begin
|
|
|
|
argparse --stop-nonopt 'h/help' 'a/abc=' -- -a A1 -h --abc A2 non-opt 'second non-opt' --help
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-14 11:36:59 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Implicit int flags work
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse '#-val' -- abc -123 def
|
|
|
|
set -l
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
argparse 'v/verbose' '#-val' 't/token=' -- -123 a1 --token woohoo --234 -v a2 --verbose
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Should be set to 987
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'm#max' -- argle -987 bargle
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Should be set to 765
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'm#max' -- argle -987 bargle --max 765
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-17 12:10:22 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Bool short flag only
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'C' 'v' -- -C -v arg1 -v arg2
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Value taking short flag only
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'x=' 'v/verbose' -- --verbose arg1 -v -x arg2
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Implicit int short flag only
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'x#' 'v/verbose' -- -v -v argle -v -x 321 bargle
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Implicit int short flag only with custom validation passes
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'x#!_validate_int --max 500' 'v/verbose' -- -v -v -x 499 -v
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Implicit int short flag only with custom validation fails
|
2017-09-09 12:14:26 +08:00
|
|
|
begin
|
|
|
|
argparse 'x#!_validate_int --min 500' 'v/verbose' -- -v -v -x 499 -v
|
|
|
|
set -l
|
|
|
|
end
|
2017-07-22 06:55:52 +08:00
|
|
|
|
2017-07-17 12:10:22 +08:00
|
|
|
##########
|
|
|
|
# Verify that flag value validation works.
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Implicit int flag validation fails
|
2017-07-17 12:10:22 +08:00
|
|
|
argparse 'm#max' -- argle --max 765x bargle
|
|
|
|
and echo unxpected argparse return status >&2
|
|
|
|
argparse 'm#max' -- argle -ma1 bargle
|
|
|
|
and echo unxpected argparse return status >&2
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Check the exit status from argparse validation
|
2017-07-17 12:10:22 +08:00
|
|
|
argparse 'm#max!set | grep _flag_; function x; return 57; end; x' -- argle --max=83 bargle 2>&1
|
|
|
|
set -l saved_status $status
|
|
|
|
test $saved_status -eq 57
|
|
|
|
and echo expected argparse return status $saved_status
|
|
|
|
|
2017-08-05 02:39:43 +08:00
|
|
|
logmsg Explicit int flag validation
|
2017-07-17 12:10:22 +08:00
|
|
|
# These should fail
|
|
|
|
argparse 'm#max!_validate_int --min 1 --max 1' -- argle -m2 bargle
|
|
|
|
and echo unexpected argparse return status $status >&2
|
|
|
|
argparse 'm#max!_validate_int --min 0 --max 1' -- argle --max=-1 bargle
|
|
|
|
and echo unexpected argparse return status $status >&2
|
|
|
|
# These should succeed
|
|
|
|
argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=0 bargle
|
|
|
|
or echo unexpected argparse return status $status >&2
|
|
|
|
argparse 'm/max=!_validate_int --min 0 --max 1' -- argle --max=1 bargle
|
|
|
|
or echo unexpected argparse return status $status >&2
|