Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed values of optional flags and arguments fail when set -u mode is set #445

Closed
chovanecadam opened this issue Nov 12, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@chovanecadam
Copy link

chovanecadam commented Nov 12, 2023

Bashly Version

Latest Ruby Gem

Description

I have a command with optional arguments and flags. Only some values are allowed and none of the flags or arguments are required. However, when no value is passed, the script fails with unbound variable error.

The check for the allowed values is done like this:

  # :command.whitelist_filter
  if [[ -n ${args['ARG']} ]] && [[ ! ${args['ARG']} =~ ^(a|b)$ ]]; then
    printf "%s\n" "ARG must be one of: a, b" >&2
    exit 1
  fi
  if [[ ${args['--option']} ]] && [[ ! ${args['--option']} =~ ^(a|b)$ ]]; then
    printf "%s\n" "--option must be one of: a, b" >&2
    exit 1
  fi

This fails with unbound variable error if the argument or the flag are not set:

$ ./cli test
./cli: line 298: args['ARG']: unbound variable

$ ./cli test a
./cli: line 302: args['--option']: unbound variable

The :command.whitelist_filter should use [[ -n ${args['arg']:-} ]] instead of [[ -n ${args['arg']} ]] to check the default values.

Contents of bashly.yml

name: cli
commands:
  - name: test
    args:
      - name: ARG
        allowed:
          - a
          - b

    flags:
      - long: --option
        arg: OPTION
        allowed:
          - a
          - b

Reproduction Steps

bashly init
bashly add settings

# set `strict: set -u`
sed -i 's/strict: false/strict: set -u/' settings.yml

# copy over bashly.yml

# generate new script
bashly generate

./cli test

Actual Behavior

$ ./cli test
./cli: line 298: args['ARG']: unbound variable

Expected Behavior

No error should be shown, because the argument (or flag) are not required.

@chovanecadam chovanecadam added the bug Something isn't working label Nov 12, 2023
@chovanecadam chovanecadam changed the title Allowed values of optional flags and arguments break set -u mode is set. Allowed values of optional flags and arguments fail when set -u mode is set Nov 12, 2023
@DannyBen DannyBen self-assigned this Nov 13, 2023
@DannyBen
Copy link
Owner

Confirmed, thank you for the easily reproducible report.

I was sure that all tests (and examples) are tested under the struct: true setting, but I see now that examples are not.

My plan is:

  • Change ${args['ARG']} to ${args['ARG']:-} in the whitelist_filter view to fix this particular violation
  • Ensure that all examples are generated and tested with strict true
  • Fix any other failing example in strict mode, which I suspect I will find

BTW - you should use lowercase when naming args (arg: option instead of arg: OPTION).

@DannyBen
Copy link
Owner

This is fixed now in master branch, and in the edge docker image.

I will hold off on releasing for a day or two, allowing you some time to test it if you can.

@chovanecadam
Copy link
Author

Thank you for the quick response, I will check it this evening.

@chovanecadam
Copy link
Author

chovanecadam commented Nov 14, 2023

I have tested the docker and the issue seems to be fixed with args as well as with flags. By the way, I use podman instead of docker, and I had to add :U flag to the volume settings to allow it read-write access to the volume. The alias then looked like this:

alias bashly='docker run --rm -it --user $(id -u):$(id -g) --volume "$PWD:/app:U" docker.io/dannyben/bashly'

(Another difference is the docker.io on the end)

@DannyBen
Copy link
Owner

Alright, thanks for confirming.
I will release tomorrow.

@DannyBen
Copy link
Owner

This is now fixed in version 1.1.2.
Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants