Closed SamuelMarks closed 1 year ago
I'll defer to @mkideal to answer your question, but meanwhile
My use-case is I want all my subcommands to take all the config struct options + some new ones.
Take a look at https://github.com/suntong/lang/tree/master/lang/Go/src/sys/cli/027-global-redo#2016-06-01
for another way to implement subcommands with global options and options of its own.
The fields version
, port
... should all be exported, e.g. Version
, Port
.
Thanks mkideal that worked. Been a while since I tried Go!
@suntong Good idea. I've been trying to implement it, tried setting the Global: true
arg but it doesn't seem to call the subcommand also:
$ go build
$ ./postgres-version-manager-go --version latest install
Reached root Fn:func
var installCommand = &cli.Command{
Name: "install",
Desc: "install specified version of PostgreSQL",
Argv: func() interface{} { return new(InstallStruct) },
Fn: func(ctx *cli.Context) error {
ctx.String("Reached installCommand Fn:func")
…
var root = &cli.Command{
Argv: func() interface{} { return new(rootCli) },
Global: true,
Fn: func(ctx *cli.Context) error {
ctx.String("Reached root Fn:func")
…
func main() {
if err := cli.Root(root,
cli.Tree(installCommand),
cli.Tree(lsCommand),
).Run(os.Args[1:]); err != nil {
_, err := fmt.Fprintln(os.Stderr, err)
if err != nil {
panic(err)
}
os.Exit(1)
}
}
Full code: https://github.com/offscale/postgres-version-manager-go/blob/5d9ef30/main.go
What am I doing wrong?
PS: Also does this library support default env vars (PORT=5432 if not set by $PORT
or --port
) and positional arguments? - I was thinking to make --version
a global arg but also a positional arg, with the positional arg taking priority.
Thanks
Again, I'll defer to @mkideal to answer your question, but meanwhile for
Also does this library support default env vars (PORT=5432 if not set by
$PORT
or--port
) and positional arguments
Take a look at https://github.com/go-easygen/wireframe/wiki/Command-line-flag-handling-code-auto-generation#cli-parameter-priorities
and see if it could be of any help.
Which gives me this error:
My use-case is I want all my subcommands to take all the config struct options + some new ones.