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

Switching command based on os.Args[0] #369

Open
calmh opened this issue Jul 27, 2023 · 2 comments
Open

Switching command based on os.Args[0] #369

calmh opened this issue Jul 27, 2023 · 2 comments

Comments

@calmh
Copy link

calmh commented Jul 27, 2023

It's happened a few times now that I've migrated separate utility programs in as sub-commands of a larger program that uses Kong. In those cases, it would sometimes be neat to continue to provide the old binary name (via a symlink, hard link, or whatever) and have things continue to work.

As a concrete example, there is currently syncthing (a program) and strelaysrv (a different, supporting program). Migrating strelaysrv into syncthing so that it can be called as syncthing relaysrv ... is easy and clean. It would be nice if we could also say something like

struct CLI {
    Serve serveCmd `cmd:"" default:""`
    // ...
    Relaysrv relaysrv.CLI `cmd:"" default-when-argv0-is:"strelaysrv"`
}

and then calling the binary as strelaysrv would be equivalent to typing syncthing relaysrv.

Alternatively, there's perhaps some magic that can be done via variable interpolation to tweak what is the default command before calling kong.Parse()?

As it is, I'm setting up an args slice myself based on os.Args, tweaked to simulate that the longer form command was run, and then pass that to a kong parser.

@alecthomas
Copy link
Owner

That's an interesting idea. What about an option eg. kong.Parse(kong.Argv0AsFirstCommand()) that basically does what you're doing manually currently?

@calmh
Copy link
Author

calmh commented Jul 27, 2023

Sounds reasonable, and that would then use the "normal" default:"" if it didn't see a match?

Although, of course, in my specific example there isn't an exact match between the old binary name and the new subcommand name, because part of the old binary name is implied by the new program it's included in... (strelaysrv -> syncthing relaysrv)

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