Creating a subcommand for custom wp-cli command

I'm toying around with writing custom wp-cli commands. I have successfully created a simple command, wp-cli theme save which runs a script to backup the site's theme files:

?php

class Save_Command extends WP_CLI_Command {

     /**
     * Create a tarball of current active theme and save it to wp_themes directory in the user's config directory.
     *
     * @synopsis
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( 'wpst' );
    }

}

WP_CLI::add_command( 'theme save', 'Save_Command' );

This code exists in ~/.wp-cli/commands/custom/save/theme-save.php and works.

Now I want to extend this feature with a new command:

wp theme save colors scheme_name

This feature spits out the customized colors to a file. I used the following code to try to get this to work:

?php

class Save_Theme_Colors_Command extends WP_CLI_Command {

     /**
     * Save color scheme.
     *
     * @synopsis name of color scheme
     */
    public function __invoke($args = array(), $assoc_args = array())
    {
    exec ( "go save_theme_colors " . implode(" ", $args));
    }

}

WP_CLI::add_command( 'theme save colors', 'Save_Theme_Colors_Command' );

However, when I try to execute the command I get an error:

Warning: Failed to load autoloader 'phar://wp-cli.phar/vendor/autoload.php'. Reason: 'wp theme save' can't have subcommands.

I've tried various ways to get the subcommand working including using the @subcommand directive but that just seems to get ignored.

Topic wp-cli Wordpress

Category Web


OK, looks like I need to change the commands so I have:

wp theme save theme and wp theme save colors

This works.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.