adhocore/php-cli

Negative argument values and optional variadic arguments / options.

shlomohass opened this issue ยท 5 comments

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

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.

btw could you please push your changes :)

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.

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.

thank you very much. can't wait to have this feature implemented ๐ŸŽ‰