-
Notifications
You must be signed in to change notification settings - Fork 3k
Labels
area/libMethods and functions that exist in the cobra library and consumed by usersMethods and functions that exist in the cobra library and consumed by users
Description
For consistency, I think it would be useful to have MinimumValidNArgs(int)
, MaximumValidNArgs(int)
and RangeValidArgs(min, max)
added to the list of supported Args
formats. The complete list would then be:
- NoArgs
- ArbitraryArgs
- OnlyValidArgs
- MinimumNArgs(int)
- +MinimumValidNArgs(int)
- MaximumNArgs(int)
- +MaximumValidNArgs(int)
- ExactArgs(int)
- ExactValidArgs(int)
- RangeArgs(min, max)
- +RangeValidArgs(min, max)
This is because AFAIK it is not possible ATM to combine e.g. MinimumNArgs(1)
with OnlyValidArgs
.
An alternative solution is to provide a built-in function such as matchAll
by @Antolius in #745. For example:
func MatchValid(p cobra.PositionalArgs) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
return err
}
if err := p(cmd, args); err != nil {
return err
}
return nil
}
}
which would allow the following expressions:
Args: cobra.MatchValid(cobra.MinimumNArgs(1)), ValidArgs: []string{...}
Args: cobra.MatchValid(cobra.MaximumNArgs(5)), ValidArgs: []string{...}
Args: cobra.MatchValid(cobra.RangeArgs(1, 5)), ValidArgs: []string{...}
Args: cobra.MatchValid(cobra.ArbitraryArgs), ValidArgs: []string{...}
Args: cobra.MatchValid(cobra.ExactArgs(1)), ValidArgs: []string{...}
Certainly, MatchValid
can be used implicitly when ValidArgs
is defined, so the user is not required to type it. Therefore, OnlyValidArgs
and ExactValidArgs
can be deprecated from the public API. The list of formats would be:
- NoArgs
- ArbitraryArgs
- MinimumNArgs(int)
- MaximumNArgs(int)
- ExactArgs(int)
- RangeArgs(min, max)
Linuturk
Metadata
Metadata
Assignees
Labels
area/libMethods and functions that exist in the cobra library and consumed by usersMethods and functions that exist in the cobra library and consumed by users