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

Using catch_all with required: true #96

Closed
DannyBen opened this issue Aug 27, 2021 · 4 comments
Closed

Using catch_all with required: true #96

DannyBen opened this issue Aug 27, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@DannyBen
Copy link
Owner

Hi, @DannyBen!

I'm just wondering how you would recommenced using the catch_all approach when the additional arguments are required but of unknown length? I see that providing required: true to the catch_all hash changes nothing, which makes sense.

Thanks for the great tool,

  • Jakob

Originally posted by @JakobGM in #70 (comment)

@DannyBen
Copy link
Owner Author

DannyBen commented Aug 27, 2021

Alright. So you are right in observing that catch_all always means "zero or more additional arguments". I haven't considered a situation where it might need to be required.

For now, you can work around it like so:

# src/bashly.yml
name: update
help: Update one or more package
version: 0.1.0
catch_all: packages
# src/root_command.sh
packages=${other_args[*]}
count=${#other_args[@]}

if [[ $count -eq 0 ]]; then
  echo "Please provide at least one package"
  exit 1

else
  echo "updaging $count packages"

  for package in $packages; do
    echo "$package"
  done
fi

Of course, this is less ideal since the usage text implies that these extra params (PACKAGES in our case), is optional:

Usage:
  update [PACKAGES...]

Let me think about it a little, perhaps it can be easy to implement it in the hash notation of the catch_all.

Keep this issue open, I will post again if I can implement it without introducing undesired complexity.

@DannyBen
Copy link
Owner Author

DannyBen commented Aug 27, 2021

Found another option that might be suitable:

# src/bashly.yml
name: update
help: Update one or more package
version: 0.1.0
catch_all: true

args:
- name: package
  help: Package name
  required: true
# src/root_command.sh
packages=(${args[package]} ${other_args[*]})
count=${#packages[@]}

echo "updaging $count packages"

for package in ${packages[@]}; do
  echo "$package"
done
# output
$ ./update -h
update - Update one or more package

Usage:
  update PACKAGE [...]
...

With this we create one required argument, one nameless catch_all, so the usage text looks reasonable, and we merge them in the code.

I will still look for a way to support required catch_all, but it seems to be more involved than I hoped.

@DannyBen
Copy link
Owner Author

DannyBen commented Aug 27, 2021

So, I have implemented it. Using required: true in the hash form of the catch_all directive will now work as expected.

  1. The catch-all-advanced example was updated accordingly
  2. Version 0.6.4 was released to rubygems and dockerhub
  3. The documentation was updated

@DannyBen
Copy link
Owner Author

@JakobGM - can I close this?

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

1 participant