-
-
Notifications
You must be signed in to change notification settings - Fork 32
Open
Description
============ STEP1: Issue Description ============
Issue Details
- When using
setup -in parser definition (to omit restargs variable as documented),gengetoptions embedcommand fails with error:/path/to/gengetoptions: 69: _rest: parameter not set or null - According to References.md documentation, specifying
-should allow omitting the restargs variable, but the implementation does not handle this case correctly.
Expected Behavior
- When
setup -is specified in parser definition,gengetoptions embed --overwrite <file>should successfully generate and embed the parser code without errors. - The documentation states: "Specify
-if you want to omit the restargs"
Steps to Reproduce
- Reproduction rate: 100% (tested 3/3 times)
- Reproduction steps:
- Create a shell script with parser definition using
setup -(see test-minimal.sh below) - Run command:
gengetoptions embed --overwrite test-minimal.sh - Observe the error:
/path/to/gengetoptions: 69: _rest: parameter not set or null
- Create a shell script with parser definition using
Environment
Environment Information
- Software
- getoptions version: 3.3.2
- gengetoptions version: 3.3.2
- System
- OS: Ubuntu 22.04.5 LTS(Linux 5.15.0-164-generic)
- Shell: GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Minimal Test Case
test-minimal.sh:
#!/bin/sh
# Minimal test case to reproduce the issue with setup -
# @getoptions
parser_definition() {
setup - help:usage -- "Usage: test-minimal.sh [OPTIONS]" ''
msg -- 'Options:'
param MODE --mode init:=default -- "Operation mode"
disp :usage -h --help
}
# @end
# @gengetoptions parser -i parser_definition parse
# (Empty - code should be generated here by gengetoptions embed)
# @end
eval "$(getoptions parser_definition parse)"
parse "$@"
eval "set -- $REST"
echo "MODE: $MODE"Command to reproduce:
$ gengetoptions embed --overwrite test-minimal.sh
/home/user/.local/bin/gengetoptions: 69: _rest: parameter not set or nullAdditional Notes
- The same script works correctly when using
setup RESTinstead ofsetup - - This appears to be a discrepancy between documentation and implementation
============ STEP2: Root Cause Analysis ============
Root Cause
- In
/path/to/getoptionsline 68, when the first argument to_setup()is-, the condition[ "${1#-}" ]evaluates to false (empty string), so_rest=$1is never executed, leaving_restunset. - However, line 80 uses
${_rest:?}which requires_restto be set, causing the "parameter not set or null" error. - This is triggered by line 5:
set -eu(specifically the-uoption which treats unset variables as errors).
Relevant code in getoptions (lines 67-80):
_setup() {
[ "${1#-}" ] && _rest=$1 # Line 68: Does NOT set _rest when $1 is "-"
while loop "$@" && shift; do kv "$1" _; done
}
# ... (lines 71-79)
_0 "${_rest:?}=''" # Line 80: Requires _rest to be setConditions for Issue Occurrence
- Using
setup -in parser definition (attempting to omit restargs as documented) - Running
gengetoptions embedor any command that invokes the getoptions code generation
Workaround (How to avoid the issue)
- Workaround procedure:
- Always specify a restargs variable name (e.g.,
REST) instead of using- - Change
setup -tosetup RESTin the parser definition - Example:
setup REST help:usage -- "Usage: example.sh [OPTIONS]" ''
- Always specify a restargs variable name (e.g.,
Recovery Method (How to fix after encountering the issue)
- Recovery procedure:
- Open the affected shell script file
- Locate the
parser_definition()function - Change the
setupline fromsetup -tosetup REST(or any other valid variable name) - Re-run the
gengetoptions embed --overwritecommand - The command should now complete successfully
Additional Investigation Notes
- This appears to be an implementation bug in getoptions v3.3.2
- The documentation (References.md) clearly states that
-can be used to omit restargs, but the implementation does not properly support this - A potential fix would be to modify line 80 in getoptions to conditionally generate the restargs initialization:
# Current (buggy): _0 "${_rest:?}=''" # Suggested fix: [ "$_rest" ] && _0 "$_rest=''"
- This would require updating the subsequent code that references
$_restto handle the case where it may be unset
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels