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

Remaining args / -- argument separator? #73

Open
bwoebi opened this issue Oct 23, 2015 · 21 comments
Open

Remaining args / -- argument separator? #73

bwoebi opened this issue Oct 23, 2015 · 21 comments

Comments

@bwoebi
Copy link
Contributor

bwoebi commented Oct 23, 2015

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?

@joetannenbaum
Copy link
Contributor

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

@joetannenbaum
Copy link
Contributor

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

@bwoebi
Copy link
Contributor Author

bwoebi commented Nov 19, 2015

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
Copy link
Contributor Author

bwoebi commented Jan 5, 2016

@joetannenbaum any update on this?

@joetannenbaum
Copy link
Contributor

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'

@bwoebi
Copy link
Contributor Author

bwoebi commented Jan 10, 2016

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

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

@bwoebi
Copy link
Contributor Author

bwoebi commented Jan 27, 2016

@joetannenbaum bump ...?

@joetannenbaum
Copy link
Contributor

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

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

@bwoebi
Copy link
Contributor Author

bwoebi commented Jan 31, 2016

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
Copy link
Contributor Author

bwoebi commented Feb 10, 2016

@joetannenbaum so, what do you think?

@kelunik
Copy link

kelunik commented Feb 23, 2016

bump

@joetannenbaum
Copy link
Contributor

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.

@bwoebi
Copy link
Contributor Author

bwoebi commented Jul 8, 2016

bump

@joetannenbaum
Copy link
Contributor

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

@kelunik
Copy link

kelunik commented Jul 12, 2016

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

@bwoebi
Copy link
Contributor Author

bwoebi commented Jul 17, 2016

This week? bump.

@joetannenbaum
Copy link
Contributor

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.

@imbrish
Copy link

imbrish commented Jun 20, 2018

@joetannenbaum any progress on this?

@duncan3dc
Copy link
Member

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

@imbrish
Copy link

imbrish commented Jun 20, 2018

Hi Craig! I think #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 :(

@pyronaur
Copy link

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.

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

No branches or pull requests

6 participants