Default subcommand when program invoked without any #253
Unanswered
Chaitanyabsprip
asked this question in
Q&A
Replies: 2 comments 4 replies
-
The best way would be to define the arguments for the default subcommand in the top-level struct, and process them as follows: type dumpCommand struct {}
type someOtherCommand struct {
// subcommand-specific arguments here
}
var args struct {
File string `arg:"-f"`
Dump *dumpCommand `arg:"subcommand"`
Other *someOtherCommand `arg:"subcommand"`
}
func main() {
arg.MustParse(&args)
switch {
case args.Dump != nil {
doDump(args.File)
case args.Other != nil {
doOther(...)
default:
doDump(args.File)
}
} This of course means that the -f option is a "global" option available to all subcommands. If that is a concern then I could show you how to avoid that. |
Beta Was this translation helpful? Give feedback.
3 replies
-
One way that you could deal with this is to preprocess the raw os.Args slice like this: var args struct {
// normal arguments struct here
}
func main() {
cmdline := os.Args[1:]
if !slices.Contains(cmdline, "dump") && !slices.Contains(cmdline, "peek") && ... {
cmdline = append([]string{"dump"}, cmdline...)
}
p, err := arg.NewParser(&args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = p.ParseArgs(cmdline)
if err != nil {
p.Fail(err.Error())
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I require the following functionality, is there a way to achieve this with go-arg package?
for e.g., a program
note
Both of the above invocation of
note
should behave identically.Beta Was this translation helpful? Give feedback.
All reactions