Repository navigation
Be strict about required arguments; fix ordering bug - #177
Conversation
|
Thanks for this quick feedback, @parndt! I think I might actually change tack a bit here. I'm going to address your bug about redefining args (this is important!) but instead of automagically turning an arg with |
9302bfd to
57a1127
Compare
57a1127 to
efa89a3
Compare
| - Improved error messages to be more precise and helpful (@katafrakt in #158) | ||
| - Accepted values are now listed the same way in help output as in error messages, separated by commas rather than slashes. (@timriley in #163) | ||
| - Declaring a required argument after an optional one now raises `Dry::CLI::CommandDefinitionError`. Arguments are matched by position, so such a command could never be called correctly: the required argument could not be given without also giving the optional one before it. (@timriley in #177) | ||
| - Declaring an argument with both `required: true` and a `default:` now raises `Dry::CLI::CommandDefinitionError`. Arguments are matched by position, so the default would make the argument optional when it comes last, and would never be used anywhere else. Remove either `required: true` or the default. (@timriley in #177) |
There was a problem hiding this comment.
Declaring an argument with required: true and a default used to load fine, and the default was just ignored. Now the command fails as soon as it's defined, so people with existing commands like that will see it after upgrading. Should we call this out as a breaking change?
There was a problem hiding this comment.
Fair point, I'll add this before merging :)
efa89a3 to
a76e43c
Compare
4e05e06 to
53da0be
Compare
Since arguments are given by position, a required argument makes a previous optional argument non-optional.
Argument handling is quite fragile due to them being given positionally, so handle these strictly (unlike options, where we allow the default to make a required option non-required).
Now that we have this broader error class, let’s just use it for all definition errors.
53da0be to
432fc2e
Compare
While working on merging #122 (focusing on options with
required: true), it became clear that we needed stricter handling of arguments withrequired: true— we were allowingrequired: truearguments to also have adefault:, but didn't do anything with that default value.Arguments are more fragile than options, because they're given positionally, versus being able to be supplied by name and in any order. So we handle conflicting options on arguments more strictly. Now we raise a
Dry::CLI::CommandDefinitionErrorin a few cases:required: trueis defined after one that is not requiredrequired: trueand adefault:.We also fix a bug from when an argument is redefined. If someone e.g. changes an existing argument's description, it was previously moved to the end of this list, and would result in values arriving in an unexpected order. Now we allow the argument to be modified while keeping its place in the sequence.
Finally, now that we've introduced the broader
CommandDefinitionError, we swapIncompatibleOptionErrorto use this one, just to keep things simple :)