Getopt-Long-Bash

 view release on metacpan or  search on metacpan

t/03_oneliner.bats  view on Meta::CPAN

            [include|i@]=
            [exclude|e@]=
            [config|c%]=
            [dry-run|n]=
        )
        . ../script/getoptlong.sh OPTS \
            --verbose \
            --output result.txt \
            --include "*.txt" \
            --include "*.md" \
            --exclude temp \
            --config key1=val1 \
            --config key2=val2 \
            --dry-run \
            file1.txt file2.txt
        
        echo "verbose:$verbose"
        echo "output:$output"
        echo "format:$format"
        echo "include_count:${#include[@]}"
        echo "include_0:${include[0]}"
        echo "include_1:${include[1]}"
        echo "exclude_count:${#exclude[@]}"
        echo "exclude_0:${exclude[0]}"
        echo "config_key1:${config[key1]}"
        echo "config_key2:${config[key2]}"
        echo "dry_run:$dry_run"
        echo "remaining_args:$@"
    '
    assert_success
    assert_line --index 0 "verbose:1"
    assert_line --index 1 "output:result.txt"
    assert_line --index 2 "format:json"
    assert_line --index 3 "include_count:2"
    assert_line --index 4 "include_0:*.txt"
    assert_line --index 5 "include_1:*.md"
    assert_line --index 6 "exclude_count:1"
    assert_line --index 7 "exclude_0:temp"
    assert_line --index 8 "config_key1:val1"
    assert_line --index 9 "config_key2:val2"
    assert_line --index 10 "dry_run:1"
    assert_line --index 11 "remaining_args:file1.txt file2.txt"
}

# Test: One-liner - error handling for unknown option
@test "getoptlong: one-liner - unknown option error" {
    run $BASH -c '
        declare -A OPTS=([verbose|v]=)
        . ../script/getoptlong.sh OPTS --unknown-option
    '
    assert_failure
    assert_output "no such option -- --unknown-option"
}

# Test: One-liner - help generation
@test "getoptlong: one-liner - help with comments" {
    run $BASH -c '
        declare -A OPTS=(
            [verbose|v # Enable verbose output]=
            [file|f: # Input file path]=
            [count|c: # Number of iterations]=5
        )
        . ../script/getoptlong.sh OPTS --help 2>/dev/null || true
    '
    assert_success
    assert_output --partial "Enable verbose output"
    assert_output --partial "Input file path"
    assert_output --partial "Number of iterations"
}

# Test: One-liner with PERMUTE= (stop at first non-option)
@test "getoptlong: one-liner - PERMUTE= stops at first non-option" {
    run $BASH -c '
        declare -A OPTS=(
            [&PERMUTE]=
            [debug|d]=
            [verbose|v]=
        )
        . ../script/getoptlong.sh OPTS --debug arg1 --verbose arg2
        echo "debug:$debug"
        echo "verbose:$verbose"
        echo "args:$@"
    '
    assert_success
    assert_line --index 0 "debug:1"
    assert_line --index 1 "verbose:"
    assert_line --index 2 "args:arg1 --verbose arg2"
}

# Test: One-liner with PERMUTE= - remaining args with options
@test "getoptlong: one-liner - PERMUTE= remaining args preserved" {
    run $BASH -c '
        declare -A OPTS=(
            [&PERMUTE]=
            [image|I:]=
        )
        . ../script/getoptlong.sh OPTS -I myimage ls -la
        echo "image:$image"
        echo "args:$@"
        echo "argc:$#"
    '
    assert_success
    assert_line --index 0 "image:myimage"
    assert_line --index 1 "args:ls -la"
    assert_line --index 2 "argc:2"
}

# Test: One-liner with PERMUTE= - no remaining args
@test "getoptlong: one-liner - PERMUTE= no remaining args" {
    run $BASH -c '
        declare -A OPTS=(
            [&PERMUTE]=
            [debug|d]=
            [file|f:]=
        )
        . ../script/getoptlong.sh OPTS -d --file test.txt
        echo "debug:$debug"
        echo "file:$file"
        echo "args:$@"
        echo "argc:$#"
    '
    assert_success
    assert_line --index 0 "debug:1"
    assert_line --index 1 "file:test.txt"
    assert_line --index 2 "args:"
    assert_line --index 3 "argc:0"
}



( run in 0.424 second using v1.01-cache-2.11-cpan-71847e10f99 )