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

Allowing multiple valued @options via 'new' annotation method? #269

Open
JacobSanford opened this issue May 3, 2022 · 5 comments
Open

Comments

@JacobSanford
Copy link

JacobSanford commented May 3, 2022

consolidation/annotated-command: 4.5.2 
consolidation/robo: 3.0.10
PHP 8.1.2

I am unsure if this issue belongs here, or in the consolidation/robo repository. Apologies if I have chosen incorrectly.

Following the 'legacy' hint in the documentation, we are attempting to migrate our @commands in Robo from the 'Legacy Annotated Command Methods' to the current method.

We are struggling with how to port @option items that allow multiple values. Legacy method:

/**
[...]
   * @option branch
   *   The branch to restart the build in. Defaults to 'dev'.
[...]
**/
public function doSomething(array $options = [
      'branch' => ['dev'],
    ]
) {

This allowed the user to specify as many branches as desired (while also defining a default option) via multiple switches. It also added '(multiple values allowed)' to the --help text.

One initial effort to port this was to 'type' the function argument in the method signature as array:

/**
[...]
   * @param array $branch
   *   The 'branch' option.
   * @option branch
   *   The branch to restart the build in. Defaults to 'dev'.
[...]
**/
public function doSomething(array $branch = ['dev']) {

this only resulted in a type validation error, as the framework passes a non-array.

How should this desired behavior be defined using the new annotation method? Is it possible, or In the 'new' method are multiple valued options only achievable using separators (--branch=dev,prod) and manual parsing (e.g. explode) within the command?

Warm regards, and many thanks for your efforts.

@JacobSanford JacobSanford changed the title Specifying multiple valued @options in 'new' annotation method? Allowing multiple valued @options via 'new' annotation method? May 3, 2022
@greg-1-anderson
Copy link
Member

Your second example should work, or I should say your second example is intended to work. Try with one argument; I believe there may be a bug with zero-argument commands that have options. I haven't had a chance to isolate and fix yet.

@greg-1-anderson
Copy link
Member

I did some brief tests and could only get binary / valueless options to work. This is a bug.

@JacobSanford
Copy link
Author

JacobSanford commented May 5, 2022

Hi Greg,

Thanks for the investigation and time. Would you prefer I open a new, more targeted issue addressing this?

@Sweetchuck
Copy link
Contributor

It is not a good idea to have an argument and an option with the same name.

@greg-1-anderson
Copy link
Member

@param is for php; @option is for Robo / Annotated Commands. Sorry if this is confusing; some folks / linters prefer to strictly have one @param for every PHP method parameter. If you're using PHP 8+, then you may use PHP annotations instead of @option, in which case the @param isn't quite as confusing.

I don't recall if this bug has been fixed; I did some work on options in the Annotated Command library some time back. It would be better to track issues with that library in its own queue.

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

No branches or pull requests

3 participants