Skip to content

[Console] Support binary / negatable options #39642

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

Merged
merged 2 commits into from
Jan 5, 2021

Conversation

jderusse
Copy link
Member

@jderusse jderusse commented Dec 28, 2020

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #24314
License MIT
Doc PR TODO

This PR take over #26292 to introduce a new option mode NEGATABLE that ease creation of option and their negation (ie. --foo, --no-foo)

Negated options applies only to flag options (VALUE_NONE)

./cli.php --foo
var_dump(getOption('foo')); // true

./cli.php --no-foo
var_dump(getOption('foo')); // false

@fabpot
Copy link
Member

fabpot commented Dec 29, 2020

I would only allow the --no- prefix for options that act as flags (the ones that do not take a value).

@jderusse jderusse force-pushed the binary-options branch 2 times, most recently from 497f03c to 82d2a9f Compare December 29, 2020 16:56
@OskarStark
Copy link
Contributor

OskarStark commented Dec 30, 2020

Can you please make fabbot happy? 😊

@jderusse
Copy link
Member Author

Can you please make fabbot happy? 😊

I think, fabbot is a false positive here.
Fixing it will break the test

@OskarStark
Copy link
Contributor

Indeed it looks weird 🧐

@javiereguiluz
Copy link
Member

Jérémy thanks for this contribution.

As a super minor comment: is "negatable" the best word to describe this? Could we use "boolean" like the Golang boolean flags?

@OskarStark
Copy link
Contributor

I like the Boolean naming a lot! Thank you for the proposal Javier 😃

@chalasr
Copy link
Member

chalasr commented Dec 30, 2020

Boolean looks confusing IMO.
Actually, Go' boolean flags are not about options that have a --no- alternative, but just options whose value can either be true or false.
Negatable options can be found in Perl and node and other languages. I would stick with it.

@jderusse
Copy link
Member Author

jderusse commented Dec 30, 2020

renamed VALUE_NEGATABLE into VALUE_BOOLEAN

I also Replaced --[no-]foo notation by --foo|no-foo for consistency with shortcut -v|vv|vvv and respects docopt (| means either X or X).
But it lloks like we are not 100% compliant with the spec:

  • -v|vv|vvv should be -v | -vv | -vvv,
  • -e, --env=ENV should be -e --env=ENV or -e, --env <env>

@wouterj
Copy link
Member

wouterj commented Dec 30, 2020

You're correct about -v|vv|vvv (let's fix that in another PR), but -e, --env=ENV is correct afaics: "Follow either <angular-brackets> or UPPER-CASE convention for options' arguments. You can use a comma if you want to separate options." Seems like all variations of comma/no-comma, and <angular-brackets>/UPPER-CASE are valid.

(and sorry for being nitpicky about this, I just remember this as it was my first big PR to Symfony: #13220)

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

Small nitpick before merging

@fabpot
Copy link
Member

fabpot commented Jan 5, 2021

Thank you @greg-1-anderson.

@fabpot fabpot merged commit a0bbf53 into symfony:5.x Jan 5, 2021
@jderusse jderusse deleted the binary-options branch January 5, 2021 08:04
@chalasr
Copy link
Member

chalasr commented Jan 5, 2021

Thanks @jderusse also :)

@greg-1-anderson
Copy link
Contributor

Is a backport to Symfony 4.4.x permissible?

@stof
Copy link
Member

stof commented Jan 5, 2021

@greg-1-anderson no. New features are never backported in patch releases.

@fabpot fabpot mentioned this pull request Apr 18, 2021
@@ -1033,8 +1033,7 @@ protected function getDefaultInputDefinition()
new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this flag removed?

@driesvints
Copy link
Contributor

@jderusse @fabpot the no-ansi flag was removed in this PR and broke the Laravel installer: laravel/installer#202. That seems like a breaking change to me on a minor release? Although it seems like we can work around it for now easily.

@stof
Copy link
Member

stof commented Jun 1, 2021

Well, was Lavaral checking the no-ansi option directly instead of checking whether the output was decorated ? If yes, it was not dealing with the smart default when neither --no-ansi or --ansi was passed.
The BC promise for application-level options is for the caller (and there, --no-ansi still works as --ansi is now a negatable option).

@driesvints
Copy link
Contributor

We switched from getOption to hasOption:

Screen Shot 2021-06-01 at 10 42 56

@stof
Copy link
Member

stof commented Jun 1, 2021

@driesvints this is not equivalent. hasOption tells you whether the input has a definition for this option. It won't tell you whether the option was used when calling the command.

@stof
Copy link
Member

stof commented Jun 1, 2021

what you really need to check is $output->isDecorated() to pass either --ansi or --no-ansi to force the subcommand to have the same decoration.

@driesvints
Copy link
Contributor

@stof never have had to do that before. This has always worked like this. I still feel this is a breaking change, sorry.

@driesvints
Copy link
Contributor

@stof I can't seem to find any docs about isDecorated on symfony.com. How would one use something like that?

@stof
Copy link
Member

stof commented Jun 1, 2021

@driesvints there is no documentation about accessing --no-ansi directly in your own command either. and as I said, passing an explicit --no-ansi is not the only way to get a decorated output. The default is a smart one (which is why we also have a --ansi to force colors).
And the case of executing a command in a subprocess like you do is precisely the case where the smart default will probably be different in the subprocess (probably never enabling colors).

How would one use something like that?

call $output->isDecorated() (which returns a boolean) to know whether messages will be decorated (i.e. whether they support being formatted with color, bold, etc...)

@driesvints
Copy link
Contributor

Thanks @stof, that was clear 👍

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.

[Console] Support for --foo / --no-foo binary options
10 participants