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

Multiple commands sharing the same positional arguments #408

Open
ylacancellera opened this issue Jan 24, 2024 · 3 comments
Open

Multiple commands sharing the same positional arguments #408

ylacancellera opened this issue Jan 24, 2024 · 3 comments

Comments

@ylacancellera
Copy link

ylacancellera commented Jan 24, 2024

Hi

Asking because my workaround now brings me too many issues

I currently have

var  CLI struct {
    CommonOption bool

    Cmd1 cmd1 `cmd`
    Cmd2 cmd2 `cmd`
}

type Cmd1 struct {
    Paths []string `arg`
    SpecificOption bool
}

type Cmd2 struct {
    Paths []string `arg`
    OtherSpecificOption bool
}

"Paths" is a required arguments shared by almost all subcommands

Commands:
  list <paths> ...

  ctx <paths> ...

  regex-list

  conflicts <paths> ...

I have tried but never succeeded in putting Paths under "CLI", instead of duplicating it on every subcommand.
It would always give panic: can't mix positional arguments and branching arguments on *struct

Is there a solution to put "Paths" under CLI somehow, so that I don't have to do CLI.List.Paths or CLI.Ctx.Path, but instead do something more generic like CLI.Paths ?

Maybe the only subcommands not using Paths could be a blocker ?

Any pointer would be greatly appreciated!
Excellent library btw, simple and effective

@alecthomas
Copy link
Owner

No that's not possible. What would that look like in practice?

@ylacancellera
Copy link
Author

ylacancellera commented Jan 25, 2024

Here is the actual code
That's the main: https://github.com/percona/percona-toolkit/blob/v3.5.7/src/go/pt-galera-log-explainer/main.go
That's a subcommand: https://github.com/percona/percona-toolkit/blob/v3.5.7/src/go/pt-galera-log-explainer/list.go
Other subcommand will have "Paths" as well

And in usage it look like pt-galera-log-explainer list --all *.log, pt-galera-log-explainer list --all $(find . -name *.log) or pt-galera-log-explainer conflicts *.log, where --all is only an option for list subcommand, with paths acting as a variadic argument to accept any number of files (that's the goal of this tool)

I guess that's pretty specific, so I understand I can't just do a simple CLI.Paths, but I wonder if there could be a trick by putting "Paths" in its own structure and somehow sharing it between subcommand
I just want to avoid a switch on commands to handle every possible paths from every subcommands

That's what I would avoid if possible

	var paths []string
	switch kongcli.Command() {
	case "list":
		paths = CLI.List.Paths
	case "ctx":
		paths = CLI.Ctx.Paths
	case "conflicts":
		paths = CLI.Conflicts.Paths
	}
        // use paths for a common tasks before subcommand execute

@ylacancellera
Copy link
Author

For the record: not sure if that's a better solution, though it's possible to use

	for _, path := range kongcli.Path {
		if path.Positional != nil && path.Positional.Name == "paths" {
			paths, ok := path.Positional.Target.Interface().([]string)
                        ....
                }
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants