Skip to content

Conversation

@sgallagher
Copy link
Contributor

Fix uninitialized wrapped script arrays causing shellcheck failures

When ARGBASH_WRAP is used, the code generator creates combined arrays
that reference both _opt and _pos suffixed arrays for each wrapped
script group:

_args_test_onlyopt=("${_args_test_onlyopt_opt[@]}" "${_args_test_onlyopt_pos[@]}")

However, if a wrapped script only had positional arguments OR only
optional arguments, one of these arrays would never be initialized,
causing shellcheck SC2154 warnings ('variable referenced but not
assigned') and test failures.

This commit fixes the issue by:

  1. Initializing both _opt and _pos arrays as empty arrays for ALL
    wrapped script groups during the defaults initialization phase.
    This is done in both _MAKE_DEFAULTS_POSITIONAL and
    _MAKE_DEFAULTS_OPTIONAL to ensure coverage regardless of which
    argument types the script has.

  2. Adding a shellcheck disable directive (SC2154) in the generated
    assign_positional_args function where wrapped positional arrays
    reference variables like $_arg_pos_arg. These variables ARE
    assigned via eval, but shellcheck cannot trace dynamic assignments:

    eval "$_positional_name=\${1}"
    

    The disable directive prevents false positive warnings while
    keeping shellcheck validation for the rest of the generated code.

Fixes shellcheck errors in scripts using ARGBASH_WRAP such as:

  • test-wrapping-second_level.sh
  • test-wrapping-excl.sh

Assisted-by: Claude 4.5 Opus
Signed-off-by: Stephen Gallagher sgallagh@redhat.com

When ARGBASH_WRAP is used, the code generator creates combined arrays
that reference both _opt and _pos suffixed arrays for each wrapped
script group:

    _args_test_onlyopt=("${_args_test_onlyopt_opt[@]}" "${_args_test_onlyopt_pos[@]}")

However, if a wrapped script only had positional arguments OR only
optional arguments, one of these arrays would never be initialized,
causing shellcheck SC2154 warnings ('variable referenced but not
assigned') and test failures.

This commit fixes the issue by:

1. Initializing both _opt and _pos arrays as empty arrays for ALL
   wrapped script groups during the defaults initialization phase.
   This is done in both _MAKE_DEFAULTS_POSITIONAL and
   _MAKE_DEFAULTS_OPTIONAL to ensure coverage regardless of which
   argument types the script has.

2. Adding a shellcheck disable directive (SC2154) in the generated
   assign_positional_args function where wrapped positional arrays
   reference variables like $_arg_pos_arg. These variables ARE
   assigned via eval, but shellcheck cannot trace dynamic assignments:

       eval "$_positional_name=\${1}"

   The disable directive prevents false positive warnings while
   keeping shellcheck validation for the rest of the generated code.

Fixes shellcheck errors in scripts using ARGBASH_WRAP such as:
- test-wrapping-second_level.sh
- test-wrapping-excl.sh

Assisted-by: Claude 4.5 Opus
Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant