-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
ExactArgs + ValidArgs + OnlyValidArgs #745
Comments
I would like to use OnlyValidArgs and MinimumNArgs(1) |
@patrick-motard for your exact use-case you can use More general solution would be something like this: var cmd = &cobra.Command{
Use: "cmd",
Short: "General solution",
ValidArgs: []string{"some", "acceptable", "values"},
Args: matchAll(cobra.MinimumNArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("cmd called")
},
}
func matchAll(checks ...cobra.PositionalArgs) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
for _, check := range checks {
if err := check(cmd, args); err != nil {
return err
}
}
return nil
}
} Here the If you think it would be a good idea to add this feature to the library itself I can submit a pull request. |
This issue is being marked as stale due to a long period of inactivity |
I would use the feature proposed by @Antolius |
@firelizzard18 you might want to try #841. |
@umarcor I want a function that composes multiple argument validators. Your MR does not provide this. Here is what I am using: func args(fns ...cobra.PositionalArgs) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
for _, fn := range fns {
if err := fn(cmd, args); err != nil {
return err
}
}
return nil
}
} |
@firelizzard18, my bad. The reference should have been #896. |
#841 is still active and other issues have mentioned it solves exactly this sort of problem. Leaving the issue open and unmarking as stale since the PR is in progress. |
The request and discussion here seem to be about composing multiple validators together. Lines 111 to 112 in 03c3eb7
|
We now even have Lines 89 to 92 in 03c3eb7
I'm going to go ahead and close this as fixed. |
@marckhouzam deprecating When #841 (or #1643) is merged, the default behaviour of Meanwhile, users can achieve the same result through |
I have a command called
i3
that must have at exactly one argument given to it, and the argument can either be "load" or "disable". By passing the command "ExactArgs(1)" I can make it do exactly one arg, but it doesn't respect "ValidArgs". I can pass "OnlyValidArgs" to make it respect "ValidArgs", but I can't pass it both. Something likeArgs: cobra.ExactArgs(1) && cobra.OnlyValidArgs(cobra, args)
would be awesome.PASS
go run main.go i3
--> Error: accepts 1 arg(s), received 0PASS
go run main.go i3 hello world
--> Error: accepts 1 arg(s), received 2FAIL
go run main.go i3 chicken
--> i3 calledPASS
go run main.go i3 load
--> i3 calledValidArgs is being ignored because
cobra.OnlyValidArgs(cmd, args)
isn't called. When i pass in chicken, I want it to error because 'load' is the only valid arg.Trying again with a custom Arg filter.
PASS
go run main.go i3 hello
--> Error: invalid argument "hello" for "dot i3"PASS
go run main.go i3 load
--> i3 calledPASS
go run main.go i3 hello world
--> Error: Requires exactly 1 argPASS
go run main.go i3
--> Error: Requires exactly 1 arg.The example above works... but it doesn't use
cobra.ExactArgs(1)
. I don't like re-inventing the wheel. How can I use bothOnlyValidArgs
andExactArgs
together? I tried:But
ExactArgs
returnsPositionalArgs
.. which looks to be a Type.. I'm not very familiar with Go, so this is confusing to me. I don't know how to combine the two. Any insight would be appreciated!Also.. If I set
ValidArgs
andOnlyValidArgs
, and an invalid command arg is provided, the user isn't notified of what the valid Args are. Is this the same feature request as #571? That seems like it would be very helpful.related #284
related #571
related #346
The text was updated successfully, but these errors were encountered: