Is there a way to run main using flags but skipping sub command? #587
-
|
kong version: github.com/alecthomas/kong v1.14.0 parse code: // Parse constructs a new parser and parses the default command-line.
func Parse(cli any, options ...Option) *Context {
parser, err := New(cli, options...)
if err != nil {
panic(err)
}
ctx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err) <- stops here
return ctx
}Problem Code example:package main
import (
"fmt"
"github.com/alecthomas/kong"
)
type TestCmd struct {
}
type CLI struct {
Version bool `help:"check version"`
Test TestCmd `cmd:"" help:"just a test cmd"`
}
func main() {
cli := CLI{}
ctx := kong.Parse(&cli)
if ctx.Command() == "" {
// Code can't reach that part because kong.Parse Fatal error
fmt.Println("no subcommand")
return
}
if cli.Version {
fmt.Println("version")
return
}
fmt.Println("calling command: ", ctx.Command())
}Is there a way to just error: Thanks, |
Beta Was this translation helpful? Give feedback.
Answered by
alecthomas
Feb 26, 2026
Replies: 1 comment 1 reply
-
|
Yes you can use kong.VersionFlag |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fiwon123
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes you can use kong.VersionFlag