Skip to content
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

Feature request: support MatchValid(PositionalArgs) OR MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs #838

Closed
eine opened this issue Mar 18, 2019 · 5 comments · Fixed by #1643 · May be fixed by #841
Closed

Feature request: support MatchValid(PositionalArgs) OR MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs #838

eine opened this issue Mar 18, 2019 · 5 comments · Fixed by #1643 · May be fixed by #841
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users

Comments

@eine
Copy link

eine commented Mar 18, 2019

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)
@eine eine changed the title Feature request: support MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs Feature request: support MatchValid(p PositionalArgs) OR MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs Mar 18, 2019
@eine eine changed the title Feature request: support MatchValid(p PositionalArgs) OR MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs Feature request: support MatchValid(PositionalArgs) OR MinimumValidNArgs, MaximumValidNArgs, RangeValidArgs Mar 18, 2019
@github-actions
Copy link

github-actions bot commented Apr 6, 2020

This issue is being marked as stale due to a long period of inactivity

@eine
Copy link
Author

eine commented Apr 6, 2020

This is still an issue. It will be closed when #841 (or some other solution) is merged.

@johnSchnake johnSchnake added area/lib Methods and functions that exist in the cobra library and consumed by users and removed kind/stale labels Mar 5, 2022
@johnSchnake
Copy link
Collaborator

Took the stale label away since the PR referenced that would fix this is still active.

@marckhouzam
Copy link
Collaborator

The alternative solution proposed in the description of this issue is now available thanks to #896 and the new MatchAll(pargs ...PositionalArgs) function:

cobra/args.go

Lines 111 to 112 in 03c3eb7

// MatchAll allows combining several PositionalArgs to work in concert.
func MatchAll(pargs ...PositionalArgs) PositionalArgs {

I'm going to go ahead and close this as fixed.

@umarcor
Copy link
Contributor

umarcor commented Mar 23, 2022

IMHO, this issue should be kept open until either:

Using MatchAll does not solve the inconsistency in the API, which is increasing the maintenance burden because of the misunderstandings and related questions/requests. We should close this issue with a solution, rather than a workaround.

See #745 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/lib Methods and functions that exist in the cobra library and consumed by users
Projects
None yet
4 participants