# Test the `argparse` builtin.

##########
# Start by verifying a bunch of error conditions.

logmsg No args is an error
argparse

logmsg Missing -- is an error
argparse h/help

logmsg Flags but no option specs is an error
argparse -s -- hello

logmsg Invalid option specs
argparse h-
argparse +help
argparse h/help:
argparse h-help::
argparse h-help=x

logmsg --max-args and --min-args work
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

logmsg Invalid \"#-val\" spec
begin
    argparse '#-val=' -- abc -x def
end

logmsg Invalid arg in the face of a \"#-val\" spec
begin
    argparse '#-val' -- abc -x def
end

logmsg Defining a short flag more than once
begin
    argparse 's/short' 'x/xray' 's/long' -- -s -x --long
end

logmsg Defining a long flag more than once
begin
    argparse 's/short' 'x/xray' 'l/short' -- -s -x --long
end

logmsg Defining an implicit int flag more than once
begin
    argparse '#-val' 'x/xray' 'v#val' -- -s -x --long
end

logmsg Defining an implicit int flag with modifiers
begin
    argparse 'v#val=' --
end

##########
# Now verify that validly formed invocations work as expected.

logmsg No args
begin
    argparse h/help --
end

logmsg One arg and no matching flags
begin
    argparse h/help -- help
    set -l
end

logmsg Five args with two matching a flag
begin
    argparse h/help -- help --help me -h 'a lot more'
    set -l
end

logmsg 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
end

logmsg --stop-nonopt works
begin
    argparse --stop-nonopt 'h/help' 'a/abc=' -- -a A1 -h --abc A2 non-opt 'second non-opt' --help
    set -l
end

logmsg Implicit int flags work
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

logmsg Should be set to 987
begin
    argparse 'm#max' -- argle -987 bargle
    set -l
end

logmsg Should be set to 765
begin
    argparse 'm#max' -- argle -987 bargle --max 765
    set -l
end

logmsg Bool short flag only
begin
    argparse 'C' 'v' -- -C -v arg1 -v arg2
    set -l
end

logmsg Value taking short flag only
begin
    argparse 'x=' 'v/verbose' -- --verbose arg1 -v -x arg2
    set -l
end

logmsg Implicit int short flag only
begin
    argparse 'x#' 'v/verbose' -- -v -v argle -v -x 321 bargle
    set -l
end

logmsg Implicit int short flag only with custom validation passes
begin
    argparse 'x#!_validate_int --max 500' 'v/verbose' -- -v -v -x 499 -v
    set -l
end

logmsg Implicit int short flag only with custom validation fails
begin
    argparse 'x#!_validate_int --min 500' 'v/verbose' -- -v -v -x 499 -v
    set -l
end

##########
# Verify that flag value validation works.

logmsg Implicit int flag validation fails
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

logmsg Check the exit status from argparse validation
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

logmsg Explicit int flag validation
# 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