Getopt-Long-Bash

 view release on metacpan or  search on metacpan

GETOPTLONG.md  view on Meta::CPAN

    # |            | ||VALIDATION
    # |            | |||      DESCRIPTION                    INITIAL VALUE
    # |            | |||      |                              |
    [ verbose    | v          # Output verbose information  ]=
    [ sweep      | w | ~W     # Enable sweep (-W disables)  ]=1
    [ output     | o :        # Specify output file         ]=/dev/stdout
    [ mode       | m ?        # Operation mode (optional)   ]=
    [ include    | i @        # Include path (multiple ok)  ]=()
    [ define     | D %        # Definition (KEY=VALUE)      ]=()
    [ execute    | x  !       # Execute command             ]=my_execute_function
    [ count      | N : =i     # Number of iterations (int)  ]=1
    [ ratio      | r : =f     # Ratio (float)               ]=0.5
    [ id         | K : =(^[a-z0-9_]+$) # ID (alphanum & _)  ]=default_id
)
```

*   **`name`:** (Required) The long option name following `--` (e.g., `verbose`).  Can contain hyphens (e.g., `very-verbose`). This is also the default base for the variable name where the option's value is stored (hyphens replaced by underscores), u...

*   **`aliases`:** (Optional) One or more option aliases, each following a `-` (e.g., `v`).  Multiple aliases can be specified, separated by `|` (e.g., `long|s|t`).  If only a short name is defined (e.g., `[s:]`), it also serves as the base for the v...

*   **`type` (Type Specifier):** (Optional) A single character that specifies how the option handles arguments. If omitted, it defaults to `+` (flag/counter type).  Possible values are:

GETOPTLONG.md  view on Meta::CPAN

    *   Improving code readability by explicitly showing where an option's value is stored.

### 4.4. Value Validation

There is a feature to validate the values of arguments passed to options. Validation is specified by appending `=<validation_type>` to the end of the option definition.

#### 4.4.1. Integer Validation (`=i`)

Validates if the argument is an integer.

*   **Definition Example:** `[count|c:COUNT=i # Number of iterations]` (assuming COUNT is the destination variable) or `[count|c:=i # Number of iterations]` (if using default variable name).

*   **Behavior:** If the argument is not an integer, an error message is displayed, and the script exits (default behavior, if `EXIT_ON_ERROR=1`).

*   Applicable to array options (`@...=i`) and the value part of hash options (`%...=i`).

#### 4.4.2. Float Validation (`=f`)

Validates if the argument is a floating-point number.

*   **Definition Example:** `[ratio|r:RATIO=f # Ratio]` or `[ratio|r:=f # Ratio]`

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

    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]=
        )



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