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

Access default value #562

Open
zeroSteiner opened this issue Oct 21, 2024 · 11 comments
Open

Access default value #562

zeroSteiner opened this issue Oct 21, 2024 · 11 comments
Labels
enhancement New feature or request

Comments

@zeroSteiner
Copy link

Description

First, thanks so much for your work on bashly, it's a fantastic project.

I'm hoping to request a feature to access the default value from the YAML definition to prevent the need for repeating it in the bash code. For my use case, I have this option definition:

  - long: --option
    arg: option
    help: Options to add to the certificate
    repeatable: true
    validate: is_valid_signing_option
    default:
      - permit-agent-forwarding
      - permit-port-forwarding
      - permit-pty

When the user specifies the --option flag, the defaults are all removed. My use case is probably uncommon, but I'd like to maintain the array to allow the user to pass multiple keys prefixed with permit or no and they'll be merged into the result as a set of enabled options. I have all of that working, but as it is now I have to have my defaults defined both in the YAML so they can be shown to the user and in the bash code, so that if only one flag is disabled the others will still be set. Basically, I'd like to be able to merge them myself by detecting the value of args[--option] isn't the default like this (assuming the function named arg_option_default returns the default value):

options="${args[--option]}"

# Get the default value from arg_option_default
default_value=$(arg_option_default)

# Check if the string equals the default value
if [[ "$options" != "$default_value" ]]; then
    # Prepend the default value to the string if they are not equal
    options="${default_value} ${options}"
fi

I was thinking there would probably be other use cases for checking if a value is non-default as well but I guess an alternative solution could be a flag to allow repeatable options to be appended to the specified default.

@zeroSteiner zeroSteiner added the enhancement New feature or request label Oct 21, 2024
@DannyBen
Copy link
Owner

What is the reason for cramming multiple options inside a single flag? Why not a separate flag for each option? Wouldn't that make it both simpler to the user and the developer?

@zeroSteiner
Copy link
Author

zeroSteiner commented Oct 21, 2024

I was trying to keep a degree of parity with the tool I'm wrapping and how they do it. In this case the tool is ssh-keygen and I'm passing the arguments for the certificate signing. By keeping the parity, I can just refer to the upstream documentation and hopefully keep usage intuitive for users that are already familiar with it.

@DannyBen
Copy link
Owner

DannyBen commented Oct 21, 2024

I see.

The use case is quite irregular, so I am not sure the value outweighs the introduced complexity and potential confusion.
There is probably more than one way to avoid writing these values more than once, one such option is to use a combination of private bashly flag and YAML aliases:

flags:
- long: --default-option
  arg: option
  repeatable: true
  private: true
  default: &options
  - permit-agent-forwarding
  - permit-port-forwarding
  - permit-pty

- long: --option
  arg: option
  help: Options to add to the certificate
  repeatable: true
  default: *options

Running it will provide:

$ ./cli
args:
- ${args[--default-option]} = permit-agent-forwarding permit-port-forwarding permit-pty
- ${args[--option]} = permit-agent-forwarding permit-port-forwarding permit-pty

Is this acceptable?

@DannyBen
Copy link
Owner

DannyBen commented Oct 21, 2024

... or perhaps with better show of intent:

flags:
- &options
  long: --option
  arg: option
  help: Options to add to the certificate
  repeatable: true
  default:
  - permit-agent-forwarding
  - permit-port-forwarding
  - permit-pty

- <<: *options
  long: --default-option
  private: true

(same result)

@zeroSteiner
Copy link
Author

Yeah, that's a clever solution. Thanks! I'll go ahead and do it that way. Feel free to close this ticket out if you'd like.

@DannyBen
Copy link
Owner

Good.

I am keeping this open, as I am still considering if there is a place for a feature that makes this more straight forward.

@DannyBen
Copy link
Owner

DannyBen commented Oct 22, 2024

@zeroSteiner how do you feel about this:

# bashly.yml
name: cli
help: Sample application
version: 0.1.0

# This is the new section
variables:
- name: default_cert_options
  value: &default_cert_options
  - permit-agent-forwarding
  - permit-port-forwarding
  - permit-pty

flags:
- long: --option
  arg: option
  repeatable: true
  default: *default_cert_options

which will simply add a section as the first thing in the command's function:

root_command() {
  # :command.variables
  declare -a default_cert_options=(   # <= This (can be anything, including array or dictionary)
    "permit-agent-forwarding"
    "permit-port-forwarding"
    "permit-pty"
  )

  # src/root_command.sh
  for key in "${!default_cert_options[@]}"; do
    echo "$key: ${default_cert_options[$key]}"
  done
}

@zeroSteiner
Copy link
Author

I like that solution a lot and think it'd fit my use case very nicely. I imagine it'd be even more useful, as you could have things like URI endpoints and path defaults defined for subcommands instead of placing them in before.sh.

@DannyBen
Copy link
Owner

Excellent. I have already created a pull request with this feature - I can merge it to master, and then you will have an edge version to use if you want. Will you be able to test it?

@zeroSteiner
Copy link
Author

Yes, I'd be very happy to help test it but it may take me a day or two. Thank you for working on it.

@DannyBen
Copy link
Owner

No pressure. Instructions for installing unreleased version are here:
https://github.com/DannyBen/bashly/tree/master/edge#readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants