zenstruck / console-extra

A modular set of features to reduce configuration boilerplate for your Symfony commands.
MIT License
78 stars 3 forks source link

show default value with nullable bool options #54

Closed tacman closed 1 year ago

tacman commented 1 year ago

I have 3 nullable (negatable) boolean options defined:

        #[Option(description: "create meili index")] ?bool            $index = true,
        #[Option(description: "process the assets")] ?bool            $process = false,
        #[Option(description: "translate the data")] ?bool            $trans = false,

I like having the ability to prefix with --no-

bin/console (command) --help
Options:
      --index|--no-index                    create meili index
      --process|--no-process                process the assets
      --trans|--no-trans                    translate the data

How I can configure this so that the default value is displayed in the help?

kbond commented 1 year ago

When using the standard symfony syntax, is there a way to display the default? If not, we should add upstream.

kbond commented 1 year ago

I also don't think negatable options are intended to have a non null default. I believe the intention is if it hasn't been set, it's null, detect the default from another source. In your example above, there's no way for the value of those options to be null. I think you'd be better off not using the negatable options.

tacman commented 1 year ago

Hmm. I have lots of these options, and depending on where I am in the development cycle, I turn on and off the defaults. And these scripts (processing specific sets of data) are in turned called by another script, which also turns on and off the options (download, refresh-cache, process, validate, translate, etc.), so it's nice to simply prepend the "no-" when I want to explicitly turn off an option, rather than not passing the option if it's to be turned off.

It seems like I should be able to have it all -- indeed, it works while setting a default, it just doesn't display the default value in the help.

kbond commented 1 year ago

Ok, fair enough. Then let's confirm symfony console does indeed show these defaults. If so, I think we need to change something here: https://github.com/zenstruck/console-extra/blob/87c43ae871436ff93123434ae3d7619b9d0ff3c8/src/Attribute/Option.php#L69

kbond commented 1 year ago

For instance, if you remove $value->mode ^ InputOption::VALUE_NONE from that if statement, does the default show?

tacman commented 1 year ago

Alas, no. :-(

I got this error:

Cannot set a default value when using InputOption::VALUE_NONE mode.

kbond commented 1 year ago

A quick check tells me it isn't possible for a negatable option to display the default. You could try and suggest this upstream in Symfony.

tacman commented 1 year ago

Thanks.