You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For future readers, the best way I found to do this was to use the Commander interface. It's not the most elegant solution, but it works fairly well.
typeOptionsstruct {
Sub1Subcommand1`command:"s1"`Sub2Subcommand2`command:"s2"`
}
typeSubcommand1struct {
Enabledbool`hidden:"true" no-ini:"true"`
}
// Detect when the subcommand is used.func (c*Subcommand1) Execute(args []string) error {
c.Enabled=truereturnnil
}
typeSubcommand2struct {
Enabledbool`hidden:"true" no-ini:"true"`
}
// Detect when the subcommand is used.func (c*Subcommand2) Execute(args []string) error {
c.Enabled=truereturnnil
}
funcmain() {
varopsOptionsargs, err:=flags.Parse(&ops)
ifflags.WroteHelp(err) {
return
}
iferr!=nil {
panic(err)
}
switch {
caseops.Sub1.Enabled:
// do thing 1caseops.Sub2.Enabled:
// do thing 2
}
}
@jessevdk this would be a good example to provide in your documentation. Additionally, it would be nice if the parser was able to recognize pointer struct types for subcommands; then it would be easy to check if a subcommand was provided with a simple nil check, as demonstrated below:
Say I have my app with nested subcommands
In order to check that I have run
app sub1 sub2 --someflag
I've been using.Is that the best way?
I couldn't find anything else in the docs.
The text was updated successfully, but these errors were encountered: