Skip to content

Unable to register option with optional value using array synopsis syntax #5617

@ssnepenthe

Description

@ssnepenthe

Bug Report

Describe the current, buggy behavior

When registering a command using the $args array with an array for the synopsis arg, I am unable to add an optional option with an optional value.

Describe how other contributors can replicate this bug

For example, the following works as expected:

WP_CLI::add_command('a', function() {}, [
	'synopsis' => '[--name[=<name>]]',
]);

We can run wp a, wp a --name or wp a --name=joe successfully.

But the equivalent array syntax does not work (array syntax created using WP_CLI\SynopsisParser::parse('[--name[=<name>]]')):

WP_CLI::add_command('a', function() {}, [
	'synopsis' => [
		[
			'optional' => true,
			'repeating' => false,
			'name' => 'name',
			'type' => 'assoc',
			'value' => [
				'optional' => true,
				'name' => 'name',
			],
			'token' => '[--name[=<name>]]',
		]
	],
]);

wp a works, wp a --name=joe works, but wp a --name displays a warning that "--name parameter needs a value".

Describe what you expect as the correct outcome

Using the array syntax should provide the same outcome as using the string syntax.

Let us know what environment you are running this on

OS:     Linux 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php7.4
PHP version:    7.4.27
php.ini used:   /etc/php/7.4/cli/php.ini
MySQL binary:   /usr/bin/mysql
MySQL version:  mysql  Ver 15.1 Distrib 10.5.13-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
SQL modes:      STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /srv/www/code/plugins/wp-cli-example
WP-CLI packages dir:    /home/vagrant/.wp-cli/packages/
WP-CLI global config:
WP-CLI project config:  /srv/www/code/plugins/wp-cli-example/wp-cli.local.yml
WP-CLI version: 2.6.0

Provide a possible solution

I am not familiar enough with internals of WP-CLI to say for sure but I assume this is caused by WP_CLI\SynopsisParser::render() not correctly rendering the array syntax used above:

>>> $synopsis = WP_CLI\SynopsisParser::parse('[--name[=<name>]]')
=> [
     [
       "optional" => true,
       "repeating" => false,
       "name" => "name",
       "type" => "assoc",
       "value" => [
         "optional" => true,
         "name" => "name",
       ],
       "token" => "[--name[=<name>]]",
     ],
   ]
>>> WP_CLI\SynopsisParser::render($synopsis)
=> "[--name=<name>]"

I can try to dig into this a little more in a bit to be sure this is the cause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions