cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Validation of nil args

Open rdgoite opened this issue 2 years ago • 4 comments

I'm not really sure if this is an issue, but I noticed that when calling ValidateArgs and the args are set to nil, the handler just effectively ignores it and returns nil and practically pass validation. Is this the right behaviour? The function ArbitraryArgs seems to suggest it ought to be doing something else, although I'm also a bit confused about it returning arbitrary args.

https://github.com/spf13/cobra/blob/2e8ba6f3087bda425da42af31f7373cd2c2c08c5/command.go#L1016

rdgoite avatar Sep 08 '22 09:09 rdgoite

Hi @rdgoite. I hope I understood your question properly.

Notice that the check for nil is made against c.Args which are not the actual arguments but the function the program can define to specify what kind of arguments the command accepts:

https://github.com/spf13/cobra/blob/2e8ba6f3087bda425da42af31f7373cd2c2c08c5/command.go#L74-L75

So if the program has not specified any function in the Args field, then ArbitraryArgs will be accepted.

marckhouzam avatar Sep 08 '22 10:09 marckhouzam

Thanks @marckhouzam. So to clarify, Args is a PositionalArgs which is a function for validating arguments. If that's not set, it means then there's no need to check the actual command-line args. Is that correct?

rdgoite avatar Sep 09 '22 00:09 rdgoite

Just to add a bit more context into this, I encountered this issue while trying to test my code:

cmd := CreateCmd()
cmd.SetArgs(nil)
//set stdout, stderr, etc
cmd.Execute()

I have set the command to check if there's at least one argument:

if len(args) < 1 {
    return errors.New("expected 1 argument")
}

However, when I run the test, I'm not getting the expected error. Instead the command proceeds doing what it's meant to do, because for some reason the args is not empty or nil. The args contains one element that looks like a regex containing the name of the test I'm running (e.g. ^\QTestDoStuff\E$).

It looks like it's somehow being set here:

https://github.com/spf13/cobra/blob/2e8ba6f3087bda425da42af31f7373cd2c2c08c5/command.go#L954

The cobra.test filepath base, where does that come from? I assume that's the mechanism to allow tests to run without issues, but for some reason that condition passes for my test.

rdgoite avatar Sep 09 '22 10:09 rdgoite

Thanks @marckhouzam. So to clarify, Args is a PositionalArgs which is a function for validating arguments. If that's not set, it means then there's no need to check the actual command-line args. Is that correct?

That's right

marckhouzam avatar Sep 09 '22 10:09 marckhouzam

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed. You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interseted in reopening

github-actions[bot] avatar Nov 14 '22 00:11 github-actions[bot]