-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Clarification regarding Command.Arguments property #2056
Comments
@kudla The Argument instances should have either a Destination or Values set for example
In the first case since you need only a single string positional argument the dest will have the single value. In the second case since you could have 2 values the dest(string slice) can contain both. |
Hi @dearchap thanks for the reply.
And what the purpose for Thnx |
To me, this seems quite strange. Both Wouldn't id be more intuitive and consistent to have a getter method to get this argument values by name? |
Yes we should probably add a getter method for Argument. |
I've run into this behavior unexpectedly. PoC: var rootCmd = &cli.Command{
Name: "prtools",
Commands: []*cli.Command{
{
Name: "validate",
Action: validate,
Arguments: []cli.Argument{
&cli.StringArg{Name: "filename", Max: -1},
},
},
},
}
func Run(ctx context.Context, args []string) error {
return rootCmd.Run(context.Background(), os.Args)
} I expected to be able to access the args using func validate(ctx context.Context, cmd *cli.Command) error {
var (
results []validationResult
targets = cmd.Args().Slice()
) However, not only does an This is confusing behavior when you consider how flags operate. I prefer being able to define my command structure in a tree at the root of the application with all flags, args, etc, so I don't use |
Correct me if I'm wrong, but didn't you name the string argument "filename", not "name"? |
Yes, typo; the actual label is immaterial because there's no way to retrieve the value of an argument using a method like this. I've corrected it :) |
Hi
Guys please could you clarify the idea behind
Command.Arguments
property.Actually my use case is a command with one required positional argument
So without
Command.Arguments
declarationcmd.Args().Get(0)
works well. Yet in this case there's no validation performed for argument was providedWith adding
it works the very same way (without args number validation)
While trying to extend arg declaration with
it actually starts validating against arguments number. Yet the argument value is not accessible neither through
cmd.Args().Get(0)
norcmd.String("required-position-argument")
. Instead it could be reached over something likecmd.Arguments[0].Values[0]
So as the examples seem not covered such a deep cases it is not clear (for
v3
specifically)Argument.Name
though. Are such argument values could be accessed by name somehow or is it just for help referring matter onlyCommand.Arguments
declarationThank you.
The text was updated successfully, but these errors were encountered: