Remaining args / -- argument separator?
I'd like to be able to grab the trailing args:
php foo.php -d val foo bar
php foo.php -d val -- foo bar
Then I want to get an array ["foo", "bar"] back.
(-- is common as an argument separator, like everything, including arguments with a leading -, following is passed literally to somewhere else.)
I've searched for it in the docs, but actually found no way how to grab every trailing arg. I've only found a way to grab a single arg...
Was this an oversight in the implementation or was there just some undocumented feature?
This was an oversight, but I will look into rectifying it. Thanks for bringing it to my attention, I'll keep you updated.
I understand your example above, but in the referenced issue afterwards (https://github.com/amphp/aerys/issues/58) how would you expect something like that to come back? Just so I know we're on the same page.
bin/aerys -c config.php -- --port 80 --host localhost
I'm not too sure about the API. Eventually, we could have the argument named "--" for that, or have an extra method for that.
I prefer a "--" argument as this means it'd be easily includible in the definition array.
@joetannenbaum any update on this?
Hey @bwoebi, I guess my question is, would you like it to work something like this:
bin/aerys -c config.php -- --port 80 --host localhost
$climate->arguments->trailing();
// ['port' => 80, 'host' => 'localhost']
// or
// '--port 80 --host localhost'
An unparsed variant. That's the point of the -- separator in general.
So basically: '--port 80 --host localhost'
@joetannenbaum bump ...?
Just put it into master, want to check it out before I tag it?
$trailing_args = $climate->arguments->trailing();
Looks nice, but usually trailing args should also work without explicit -- at the end for arguments without starting - ... if that is possible? :-)
Thanks!
(See also initial example: php foo.php -d val foo bar, trailing args are then "foo bar")
@joetannenbaum so, what do you think?
bump
I'm sorry for the delay, I've been swamped at work trying to launch a big project. I'll get this up this week.
bump
I hear you, I apologize. CLImate will get love this week.
This can be implemented manually by splitting the array if there's a -- in $argv and dispatching / parsing the two parts separately.
This week? bump.
I'm working on re-working the argument parser so that it's more readable and intuitive. The '--' version of the trailing arguments is in the current release (3.2.1), if you need the other part implemented prior to the re-write I'm happy to accept a pull request.
@joetannenbaum any progress on this?
@imbrish Hi Paul, I've taken over development of CLImate, I don't currently have this specific task scheduled, but I'll look at it as soon as I can. As Joe said, Pull Requests are always welcome :smile:
Hi Craig! I think https://github.com/thephpleague/climate/issues/85 is the best bet.
Currently it's not possible to get an array of remaining arguments, even with $climate->arguments->trailing() because spaces within and between arguments are mixed together.
Unfortunately rewriting whole parsing logic gonna be a big backward compatibility break :(
I needed to get the unregistered args following the main command and ended up doing this:
/**
* Match all the commands following the passed command.
*
* Example
* > `phy -d db pull main -x`
* Produces: [ 'pull', 'main' ]
*
* Example
* > `phy db pull-stuff --careful=true`
* Produces: [ 'pull-stuff' ]
*
* @param $argv
* @param $starting_command
*
* @return array
*/
function get_subsequent_commands( $argv, $starting_command ) {
$command_as_string = implode( ' ', $argv );
$pattern = "/{$starting_command}(\s(?!-)[\w\d-]+)+/";
preg_match( $pattern, $command_as_string, $matches );
if ( empty( $matches[0] ) ) {
return [];
}
$commands = $matches[0];
$commands = str_replace( $starting_command, '', $commands );
$commands = trim( $commands );
$command_chain = explode( ' ', $commands );
return $command_chain;
}
This seems to work well enough.