8000 [Console] Add method to know parsed option by blanchonvincent · Pull Request #12773 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@blanchonvincent
Copy link
Contributor
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #12769, #11572
License MIT
Doc PR -

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe faster with isset ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@samsonasik array_key_exists is different from isset. Here, a value can be null

@fabpot fabpot added the Console label Dec 7, 2014
@jakzal
Copy link
Contributor
jakzal commented Dec 12, 2014

What's the use case? If the option is not defined why would you like to check if it was passed?

@xabbuh
Copy link
Member
xabbuh commented Dec 12, 2014

@jakzal The issue is that you can't determine if an option was passed by the user when the value is optional (see #11572 and symfony/symfony-docs#4110).

@javiereguiluz
Copy link
Member

@xabbuh do you like the hasParsedOption() method name?

@fabpot
Copy link
Member
fabpot commented Oct 2, 2015

I think the implementation should be actually different. There is no need to add another method with a weird name nobody will understand. I was more thinking about adding an additional argument to the existing getOption() method.

@bishopb
Copy link
bishopb commented Nov 9, 2015

@fabpot I think the challenge with an additional argument to getOption is that wouldn't address getOptions, which I would also expect to support tri-state optionals:

command.php
$this->getOptions() => [ 'foo' => 'value when not given' ]

command.php --foo
$this->getOptions() => [ 'foo' => 'value when given but blank' ]

command.php --foo=bar
$this->getOptions() => [ 'foo' => 'bar' ];

Also, I just noticed changing getOption contradicts what @stof indicated on a related issue:

Changing getOption() is not possible as it would be a BC break (and would make many valid use cases harder to implement).
However, we could add a new method to check whether the option is present or no (still difficult in term of BC though as we cannot change the InputInterface itself for BC reasons)

@tomzx
Copy link
tomzx commented Jan 25, 2016

While you guys are at it, it might also be useful to be able to determine if an argument has been set.

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
fabpot added a commit that referenced this pull request Mar 1, 2017
…empty) should remain empty (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Console] Explicitly passed options without value (or empty) should remain empty

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21215 #11572 #12773
| License       | MIT
| Doc PR        | n/a (maybe look at updating the existing one)

This conserves empty values for options instead of returning their default values.

Code:
```php
// cli.php
$application = new Application();
$application
    ->register('echo')
    ->addOption('prefix', null, InputOption::VALUE_OPTIONAL, null, 'my-default')
    ->addArgument('value', InputArgument::REQUIRED)
    ->setCode(function ($input, $output) {
        var_dump($input->getOption('prefix'));
    });
$application->run();
```

Before:
![before](http://image.prntscr.com/image/157d9c6c054240da8b0dce54c9ce24d6.png)

After:
![after](http://image.prntscr.com/image/4aeded77f8084d3c985687fc8cc7b54e.png)

Commits
-------

8086742 [Console] Explicitly passed options without value (or empty) should remain empty
@fabpot fabpot closed this Mar 1, 2017
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

0