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][RFC] Add named arguments (aka required options) #48244
Conversation
Hey! Thanks for your PR. You are targeting branch "6.2" but it seems your PR description refers to branch "6.3 for features". Cheers! Carsonbot |
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
Python supports required options but recommends not using them:
For this reason and the ones given in the related issue (tl;dr: not standard-compliant), I'm 👎 on this. Also this can be achieved by throwing some exception. |
@chalasr the name required options is misleading, it is better named "named arguments". as i started with symfony (v1) most of the classes used getter and setter. the constructor was not used most of the time. calling each setter one after the other really gave a good readability what was set, one line could simply be commented out or switched.
switching to immutable objects, with no setter and only a constructor to set the initial state of the object, made a lot of sense but really made the creation more error-prone. having multiple string parameters in a constructor was quite dangerous and it was easy to mix up the order of the parameters.
the really great solution from php was the introduction of named parameters. explicit naming the parameter prevents errors.
the same is with associative config arrays. we are using them instead accesing values from an array by index.
vs
so why exactly is it a bad practice to have named arguments in the cli? it is ok to have unnamed arguments if you have only one or two arguments but having 3+ requires you checking the right order most of the time.
vs
with php now having named parameters i thought it would be good thing to also have the option (nobody has to use it if they don't want to) of named arguments would be a good thing. maybe naming the constant |
just stumbled over another usecase: add multiple users to multiple projects. at least one username and one project is required. quote from the symfony docs:
so doing it with two
we need to set users to
as workaround i can only switch to 2 string arguments and do a coma-separation and explode.
or i can add the required option code myself. i think it would really make sense to have the feature "named arguments" in the core. real world use cases are there. holding onto the semantic of the word "option" instead of going with the words "named argument" is just withholding the desired path.
i found one interesting article Desire paths: the unofficial footpaths that frustrate, captivate campus planners about this topic and a collection of images 50 Times City Architects Failed To Understand People’s Needs, And It Resulted In These ‘Desire Paths’ Appearing Around The City . |
I'm still not convinced we need this given that the above use cases can be achieved with some custom code in userland and this is not supported by most relevant standards. Calling @symfony/mergers for additional opinion(s) |
To me, an option should always be optional. So I'm against required options. I agree with @chalasr, it can still be achieve in userland. |
Closing, thanks for pushing the discussion. |
Doesn't VALUE_REQUIRED imply that the option must be set? If the option is not set, there's no value, thus the required value is missing. Right? |
No. It means that if you set the option, it is required to provide a value (that's why it is named |
I know it's an old discussion, but i stumbled across this myself as i wanted to use a named argument (as it's required it's an argument) or a required option (as its key is prefixed with either one or two hyphens). And i do think a named arguments are a valid use-case. Since i didn't find the arguments made here completely convincing, i had a look. To spare anyone the time to do it themselves, here's what i think i found: There were basically two arguments made against it:
I'm not familiar with writing console commands in linux, but if i am not mistaken, argument 2 is wrong: It is actually part of the only relevant standard: POSIX 12.1 under list number 9:
The only command i found that has "required options"/"named arguments" was "tar", but i wouldnt consider it an essential part of linux and even if it was, it could've and should've been done another way imo. "Required options" being part of POSIX of course doesn't mean, that it isn't confusing. But the problem is not that it is not part of any standard, but that the implementation of named arguments in the POSIX standard is done in a confusing way: It includes "named arguments" (functionality-wise) in a misleading way as "required options", because of its hyphen prefixed syntax, which makes it a mix of an argument and an option. My best guess is that they wanted to include named arguments, but defining a new syntax was more complex than just ignoring semantics a bit (probably by saying: this is a required option, because it has "options to choose from"):
I think that's also the reason why python includes and discourages it at the same time. Of course i can be completely wrong about all this, but it makes perfect sense to me. |
A thumbs up from me too. 👍 Almost everyone knows it, especially on stressful days, the parameters don't quite work the way you're used to. Support like this is very helpful! |
added mode
InputOption::REQUIRED
to allow required "options" aka "named arguments"edit:
should a required option also require a value? in the sense of named arguments it would speak for having a required value but maybe i miss something.
in combination with negatable it would make sense again to not require a value, so you are forced to provide
--delete
or--no-delete
.should it be allowed to require array options?
from the logic point of view a required option should not have defaults, should it?
new tests have to be fixed/adapted
edit2:
renamed named parameters to named arguments