climate icon indicating copy to clipboard operation
climate copied to clipboard

Remaining args / -- argument separator?

Open bwoebi opened this issue 10 years ago • 21 comments

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?

bwoebi avatar Oct 23 '15 13:10 bwoebi

This was an oversight, but I will look into rectifying it. Thanks for bringing it to my attention, I'll keep you updated.

joetannenbaum avatar Nov 11 '15 02:11 joetannenbaum

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

joetannenbaum avatar Nov 11 '15 03:11 joetannenbaum

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.

bwoebi avatar Nov 19 '15 22:11 bwoebi

@joetannenbaum any update on this?

bwoebi avatar Jan 05 '16 17:01 bwoebi

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'

joetannenbaum avatar Jan 10 '16 19:01 joetannenbaum

An unparsed variant. That's the point of the -- separator in general.

So basically: '--port 80 --host localhost'

bwoebi avatar Jan 10 '16 19:01 bwoebi

@joetannenbaum bump ...?

bwoebi avatar Jan 27 '16 07:01 bwoebi

Just put it into master, want to check it out before I tag it?

$trailing_args = $climate->arguments->trailing();

joetannenbaum avatar Jan 31 '16 22:01 joetannenbaum

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")

bwoebi avatar Jan 31 '16 22:01 bwoebi

@joetannenbaum so, what do you think?

bwoebi avatar Feb 10 '16 15:02 bwoebi

bump

kelunik avatar Feb 23 '16 11:02 kelunik

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.

joetannenbaum avatar Feb 23 '16 14:02 joetannenbaum

bump

bwoebi avatar Jul 08 '16 15:07 bwoebi

I hear you, I apologize. CLImate will get love this week.

joetannenbaum avatar Jul 11 '16 15:07 joetannenbaum

This can be implemented manually by splitting the array if there's a -- in $argv and dispatching / parsing the two parts separately.

kelunik avatar Jul 12 '16 19:07 kelunik

This week? bump.

bwoebi avatar Jul 17 '16 23:07 bwoebi

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 avatar Jul 18 '16 20:07 joetannenbaum

@joetannenbaum any progress on this?

imbrish avatar Jun 20 '18 09:06 imbrish

@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:

duncan3dc avatar Jun 20 '18 09:06 duncan3dc

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 :(

imbrish avatar Jun 20 '18 13:06 imbrish

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.

pyronaur avatar Jan 26 '20 23:01 pyronaur