Skip to content

Using flag.conflicts causes issues with set -u #559

@thendricks0

Description

@thendricks0

Bashly Version

Latest Docker Image

Description

Using flag.conflicts causes issues with set -u.
If I use strict mode or rather set -u and then try to use conflicting variables I get an unbound variable error since $key is unbound.

Contents of bashly.yml

flags:
  - long: --verbose
    short: -v
    help: Show verbose output
    conflicts: [--quiet]
  - long: --debug
    short: -d
    help: Show debug output
    conflicts: [--quiet]
  - long: --quiet
    short: -q
    help: Suppress output, this implies --force so all prompts are confirmed
    conflicts: [--verbose, --debug]

Reproduction Steps

Use the provided bashly config + settings.yml below, generate a script and then execute the script with -qd or -qv or -qdv.

# settings.yml

strict: true
# or use
strict: set -o nounset

Actual Behavior

parse_requirements will be generated like this:

parse_requirements() {
  # :command.fixed_flags_filter
  while [[ $# -gt 0 ]]; do
    case "${1:-}" in
      --version)
        version_command
        exit
        ;;

      --help | -h)
        long_usage=yes
        awps_usage
        exit
        ;;

      # :flag.case
      --verbose | -v)
        # :flag.conflicts
        if [[ -n "${args['--quiet']:-}" ]]; then
          printf "conflicting options: %s cannot be used with %s\n" "$key" "--quiet" >&2
          exit 1
        fi

        # :flag.case_no_arg
        args['--verbose']=1
        shift
        ;;

      # :flag.case
      --debug | -d)
        # :flag.conflicts
        if [[ -n "${args['--quiet']:-}" ]]; then
          printf "conflicting options: %s cannot be used with %s\n" "$key" "--quiet" >&2
          exit 1
        fi

        # :flag.case_no_arg
        args['--debug']=1
        shift
        ;;

      # :flag.case
      --quiet | -q)
        # :flag.conflicts
        for conflict in --verbose --debug; do
          if [[ -n "${args[$conflict]:-}" ]]; then
            printf "conflicting options: %s cannot be used with %s\n" "$key" "$conflict" >&2
            exit 1
          fi
        done

        # :flag.case_no_arg
        args['--quiet']=1
        shift
        ;;
...

Expected Behavior

But it should be generated like this:

parse_requirements() {
  # :command.fixed_flags_filter
  while [[ $# -gt 0 ]]; do
    key="$1"   # <----------- this!
    case "${key:-}" in
      --version)
        version_command
        exit
        ;;

      --help | -h)
        long_usage=yes
        awps_usage
        exit
        ;;

      # :flag.case
      --verbose | -v)
        # :flag.conflicts
        if [[ -n "${args['--quiet']:-}" ]]; then
          printf "conflicting options: %s cannot be used with %s\n" "$key" "--quiet" >&2
          exit 1
        fi

        # :flag.case_no_arg
        args['--verbose']=1
        shift
        ;;

      # :flag.case
      --debug | -d)
        # :flag.conflicts
        if [[ -n "${args['--quiet']:-}" ]]; then
          printf "conflicting options: %s cannot be used with %s\n" "$key" "--quiet" >&2
          exit 1
        fi

        # :flag.case_no_arg
        args['--debug']=1
        shift
        ;;

      # :flag.case
      --quiet | -q)
        # :flag.conflicts
        for conflict in --verbose --debug; do
          if [[ -n "${args[$conflict]:-}" ]]; then
            printf "conflicting options: %s cannot be used with %s\n" "$key" "$conflict" >&2
            exit 1
          fi
        done

        # :flag.case_no_arg
        args['--quiet']=1
        shift
        ;;
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions