Skip to content

Introduce wp cli has-command to detect whether a command is registered#4349

Merged
schlessera merged 11 commits intowp-cli:masterfrom
Sidsector9:feature/GH#4283
Sep 26, 2017
Merged

Introduce wp cli has-command to detect whether a command is registered#4349
schlessera merged 11 commits intowp-cli:masterfrom
Sidsector9:feature/GH#4283

Conversation

@Sidsector9
Copy link
Member

@Sidsector9 Sidsector9 commented Sep 13, 2017

I've created a recursive function that traverses the array of the dump output by self::command_to_array( WP_CLI::get_root_command() )

See #4283

  • Tested locally
  • Functional tests

*
* @subcommand has-command
*
* @when before_wp_load
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having before_wp_load is an issue as it won't detect commands added in plugins/themes. Actually, this is an issue for cmd-dump as well as the whole CLI_Command class is marked as before_wp_load. So I think the before_wp_load needs to be removed from the CLI_Command class and added to the individual commands, except for this command and cmd-dump, where after_wp_load should be used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is an issue for cmd-dump as well

Historically, cmd-dump has only been used for exporting bundled command definition to documentation, so it hasn't actually been an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok - I'll open an issue re individual function whens not being honoured if there's a class when as it probably needs discussion.

/**
* Get dump as array.
*/
$json_decoded = self::command_to_array( WP_CLI::get_root_command() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below.

/**
* If command is input as a string, then explode it into array.
*/
$command = ( 1 === count( $_ ) ) ? explode( ' ', $_[0] ) : $_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be simpler just to do $args = explode( ' ', implode( ' ', $_ ) ); which allows any old arg format.

/**
* If command doesn't start with WP, then halt script execution and exit with status 1.
*/
if ( 'wp' !== $command[0] ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nicer to make prefixing with wp optional by doing if ( 'wp' === $args[0] ) { array_shift( $args ); }

WP_CLI::halt( 1 );
}

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-using Runner::find_command_to_run() would make sense here:

WP_CLI::halt( is_array( WP_CLI::get_runner()->find_command_to_run( $args ) ) ? 0 : 1 );

making the recursive_search() function below and the command_to_array() call above unnecessary.

Copy link
Member Author

@Sidsector9 Sidsector9 Sep 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just such a beautiful way to do this, I did not know about this!

*/
public function has_command( $_, $assoc_args ) {

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general in-function comments shouldn't be docblocks and if they're one liners should use // I think.

@schlessera
Copy link
Member

Looks good so far.

A general note:
wp is not actually part of the command name, it is the name of the executable that was used to start up the WP-CLI framework. So, something like wp post list could just as well be php ~/wp-cli.phar post list.

As regards to testing, you could go through the following steps:

  • check whether the command scaffold package can be found - this should return an error, as the command is not part of the bundled commands
  • install the wp-cli/scaffold-package-command
  • check again whether the command scaffold package can be found - this time it should be positive

@schlessera schlessera added the command:cli-has-command Related to 'cli has-command' command label Sep 14, 2017
@schlessera schlessera added this to the 1.4.0 milestone Sep 14, 2017
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looking good, some minor nits.

* @when after_wp_load
*
* @param array $_ Array of positional arguments.
* @param array $assoc_args Array of associative arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove this argument definition, as it's not a pattern we have elsewhere.

* <command_name>...
* : The command
*
* ## EXAMPLES
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs another space after this heading.

*
* ## EXAMPLES
* wp cli has-command "wp site delete"
* wp cli has-command wp site delete
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of these examples should have some explanation:

# The "site delete" command is registered
$ wp cli has-command "site delete"
$ echo $?
0

# The "foo bar" command is not registered
$ wp cli has-command "foo bar"
$ echo $?
1

// If command is input as a string, then explode it into array.
$command = explode( ' ', implode( ' ', $_ ) );

if ( 'wp' === $command[0] ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should accept "wp" to this command.

@Sidsector9
Copy link
Member Author

Made changes as per review

When I try `{PHAR_PATH} cli has-command scaffold package`
Then the return code should be 1

When I run `{PHAR_PATH} package install git@github.com:wp-cli/scaffold-package-command.git`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a short package identifier here:
When I run {PHAR_PATH} package install wp-cli/scaffold-package-command`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schlessera, Why didn't git clone work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just because Travis hit the git rate limiting.

With the short package identifier, in this case, you fetch the ZIP through the old package index, instead of the GH versioned source.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see 👀

*
* ## EXAMPLES
*
* The "site delete" command is registered.
Copy link
Member

@schlessera schlessera Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the examples, you should follow the notation convention, as @danielbachhuber had provided:

Comments start with #, and the command starts with the "basic" prompt $. Lines without either are output.

Here's one example:

/**
 * ## EXAMPLES
 *
 * # The "site delete" command is registered.
 * $ wp cli has-command "site delete"
 * $ echo $?
 * 0
 */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scenario: Ability to detect a WP-CLI registered command
Given an empty directory
And save the {SRC_DIR}/VERSION file as {TRUE_VERSION}
And a new Phar with version "1.3.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to generate a new Phar here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, there wasn't a need to generate a phar. I replaced it with wp @danielbachhuber

@danielbachhuber danielbachhuber changed the title GH#4283 Function to detect a registered WP-CLI command Introduce wp cli has-command to detect whether a command is registered Sep 19, 2017
@danielbachhuber danielbachhuber self-requested a review September 26, 2017 00:18
@schlessera schlessera merged commit 4738303 into wp-cli:master Sep 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:cli-has-command Related to 'cli has-command' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants