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

Negative argument values and optional variadic arguments / options. #93

Open
shlomohass opened this issue Sep 24, 2023 · 5 comments
Open

Comments

@shlomohass
Copy link
Contributor

Hi,
First and foremost, thank you for this fantastic project! While there are others out there, I really like this one since it's small, light, simple, and very flexible.

I am having trouble with negative values:

# cmd <offset> --reduce 
$ cmd  -20 --reduce -4 # fails as it parses -20 to -2 -0 and -4 as an option name.

Usually, in some CLI programs, something like this would be evaluated correctly:

$ cmd  "-20" --reduce "-4" # same issue
$ cmd "-20" --reduce=-4 # this one partially works 

It seems that currently, php-cli doesn't support direct negative values unless I'm missing something (please correct me if I'm wrong).

I modified the Normalizer and the Parser to support that if this is truly an issue let me know, and I can create a pull request.

Also, as an artifact, now a syntax as follows is easily implemented:

# cmd <name> [counted...] --variadic [values...] --another <values...>
$ cmd john [12 45 87] --variadic [ 15 20 65 ] --another 98 65 45
# Square brackets act like group boundaries for "in the middle" variadic arguments.
# Still the last variadic argument, without square brackets, functions as normal.

I think it's Cool... and very useful.

Let me know if that's an acceptable improvement

@adhocore
Copy link
Owner

i see, thanks for using it and also opening issues to make it better. regarding the main issue of negative value, my guess would be it is because - is considered as a short option. a potential fix would be if the preceding option is still expecting values we can push the current -ve arg as value to the preceding option.

and i like your proposal of variadic variadics, that would be awesome and welcome feature - however we need to double check if the braces [ ( may have special meaning in the bash/shell environment that causes us a problem to use the brace as variadic values boundary.

@adhocore
Copy link
Owner

btw could you please push your changes :)

@shlomohass
Copy link
Contributor Author

Great, I will push them shortly. Just need to add some tests and tidy my implementation.
Meanwhile, I have some thoughts about the Option parser:

$this->newCommand()->option("--names [items...]")
// We fail miserably since we are not setting the short name of this option.

Anyway it seems that any lack of a "short" definition will cause some troubles - I will fix that in the parse method of Option
And publish my suggestions.

@shlomohass
Copy link
Contributor Author

i see, thanks for using it and also opening issues to make it better. regarding the main issue of negative value, my guess would be it is because - is considered as a short option. a potential fix would be if the preceding option is still expecting values we can push the current -ve arg as value to the preceding option.

and i like your proposal of variadic variadics, that would be awesome and welcome feature - however we need to double check if the braces [ ( may have special meaning in the bash/shell environment that causes us a problem to use the brace as variadic values boundary.

As far as I know, ( ) can cause some issues since it is evaluated as a sub-shell group, so in some wild cases (when php-cli) is called in combination with another command or passthrough some other program, this may cause some issues.
For [] it seems safe, in the old days this was evaluated as a "conditional command" which was replaced by [[ ]] anyhow we can always change the special character or escape it with \.
I will test it of course.

@adhocore
Copy link
Owner

thank you very much. can't wait to have this feature implemented 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants