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

[Console] Add ability to select values interactively #6343

Merged
merged 2 commits into from Dec 14, 2012

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Dec 14, 2012

This is a slightly modified version of #5417

Adds ability to select from value list:

$values = array('One', 'Two', 'Three');
$pick = $this->getHelper('dialog')->select($output, 'Pick a bundle:', $values, 2);
var_dump($pick);

... would give the following CLI output:

Pick a bundle:
  0   One
  1   Two
  2   Three
> 1
1

* @param integer $attempts Max number of times to ask before giving up (false by default, which means infinite)
* @param string $errorMessage Message which will be shown if invalid value from choice list would be picked
*
* @return mixed The selected value
Copy link
Member

Choose a reason for hiding this comment

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

you should make it more clear that the selected value is a a key of the $choices array

Copy link
Member

Choose a reason for hiding this comment

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

Since its the array key that is returned, the return type can only be string|integer.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@Seldaek
Copy link
Member

Seldaek commented Dec 14, 2012

Maybe the default value should be shown somehow?

@fabpot
Copy link
Member Author

fabpot commented Dec 14, 2012

@Seldaek: It's up to you to display it (probably in the question). That's how the other ask* methods work.

*
* @return integer|string The selected value (the key of the choices array)
*/
public function select(OutputInterface $output, $question, $choices, $default = null, $attempts = false, $errorMessage = 'Value "%s" is invalid')
Copy link
Member

Choose a reason for hiding this comment

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

I think you should typehint the choices as array

Copy link
Member

Choose a reason for hiding this comment

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

I agree

@Tobion
Copy link
Member

Tobion commented Dec 14, 2012

Would be nice to also add the ability to select multiple values (probably other PR).
So one can select each option individually to toggle it's selection. And one can select multiple with a range like 2-7 (as in the git console with git add -i).

… consistent with other method in the same class
@fabpot
Copy link
Member Author

fabpot commented Dec 14, 2012

@Tobion That's indeed something that should be done in another PR.

fabpot added a commit that referenced this pull request Dec 14, 2012
This PR was merged into the master branch.

Commits
-------

008dc8f [Console] tweaked the select() method in the dialog helper to be more consistent with other method in the same class
692b0f4 [Console] added select() method for DialogHelper

Discussion
----------

[Console] Add ability to select values interactively

This is a slightly modified version of #5417

Adds ability to select from value list:

    $values = array('One', 'Two', 'Three');
    $pick = $this->getHelper('dialog')->select($output, 'Pick a bundle:', $values, 2);
    var_dump($pick);

... would give the following CLI output:

    Pick a bundle:
      0   One
      1   Two
      2   Three
    > 1
    1

---------------------------------------------------------------------------

by Seldaek at 2012-12-14T09:07:40Z

Maybe the default value should be shown somehow?

---------------------------------------------------------------------------

by fabpot at 2012-12-14T09:28:42Z

@Seldaek: It's up to you to display it (probably in the question). That's how the other `ask*` methods work.

---------------------------------------------------------------------------

by Tobion at 2012-12-14T09:38:40Z

Would be nice to also add the ability to select multiple values (probably other PR).
So one can select each option individually to toggle it's selection. And one can select multiple with a range like `2-7` (as in the git console with `git add -i`).

---------------------------------------------------------------------------

by fabpot at 2012-12-14T09:45:10Z

@Tobion That's indeed something that should be done in another PR.
@fabpot fabpot merged commit 008dc8f into symfony:master Dec 14, 2012
@nfx
Copy link
Contributor

nfx commented Dec 16, 2012

@fabpot i think you've missed "return => key" handling while tweaking the code.

@fabpot
Copy link
Member Author

fabpot commented Dec 16, 2012

I removed this on purpose.The list of options is already quite long, and getting the value from the original array is simple enough to not add more options.

@nfx
Copy link
Contributor

nfx commented Dec 16, 2012

one way to go could be introducing some kind of DialogSelectPromptBuilder, but not sure

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

Successfully merging this pull request may close these issues.

None yet

5 participants