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

Defining common parameters in several new commands #2311

Closed
petenelson opened this issue Dec 18, 2015 · 2 comments
Closed

Defining common parameters in several new commands #2311

petenelson opened this issue Dec 18, 2015 · 2 comments

Comments

@petenelson
Copy link

I'm working on a new set of commands for a project and each one has a set of common parameters that can be used. For example, I may or may not want a progress bar on each one of these commands, such as:

 * [--[no-]progress-bar]
 * : Show a progress bar (default) or show each individual post as it's generated

As of right now, I have to copy/paste this into the OPTIONS and synopsis section of the PHPDoc comments of each new command I'm creating. Is there a way to define a list of common command parameters and then be able to reference them when creating a new subcommand? I already built a parse_common_args() method in my code to process the common parameters, but I'm not sure if it's possible to define or import these in the PHPDoc comments.

@danielbachhuber
Copy link
Member

Is there a way to define a list of common command parameters and then be able to reference them when creating a new subcommand?

Not yet, but it will be much more viable with #2204

@danielbachhuber
Copy link
Member

Is there a way to define a list of common command parameters and then be able to reference them when creating a new subcommand?

In v0.23.0 (and already in the nightly), WP-CLI will support registering command synopsis in WP_CLI::add_command()'s third argument. Here's an example:

function foo( $args ) {
    $message = array_shift( $args );
    WP_CLI::log( 'Message is: ' . $message );
    WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo', array(
    'shortdesc'   => 'My awesome function command',
    'when'        => 'before_wp_load',
    'synopsis'    => array(
        array(
            'type'          => 'positional',
            'name'          => 'message',
            'description'   => 'An awesome message to display',
            'optional'      => false,
        ),
        array(
            'type'          => 'assoc',
            'name'          => 'apple',
            'description'   => 'A type of fruit.',
            'optional'      => false,
        ),
        array(
            'type'          => 'assoc',
            'name'          => 'meal',
            'description'   => 'A type of meal.',
            'optional'      => true,
        ),
    ),
) );

You can assign your arguments to a variable, and then reuse the variable between command registration.

Introduced in #2373

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

2 participants