How to group subcommands using go_flags package?

453 Views Asked by At

I am using https://github.com/jessevdk/go-flags and trying to group the subcommands using the top-level option "group" in the struct field. But instead of grouping the subcommands, it actually groups the options in the subcommands. Here is my code:

type Cmd1 struct {
    Opt1         string `long:"opt1" description:"first opt" required:"true"`
    Opt2         int    `long:"opt2" description:"second opt" default:"10"`
}

type Cmd2 struct {
    OptA string `long:"optA" description:"opt a" default:":8080"`
    OptB string `long:"optB" description:"opt b" default:"debug"`
}

type MainCmd struct {
    Cmd1      Cmd1      `group:"group1" namespace:"group1" env-namespace:"group1"`
    Cmd2     Cmd2     `group:"group2" namespace:"group2" env-namespace:"group2"`
}

func main() {
    var mainCmd MainCmd
    parser := flags.NewParser(&mainCmd, flags.Default) 
    if _, err := parser.Parse(); err != nil {
        if err, ok := err.(*flags.Error); ok {
            if err.Type == flags.ErrHelp {
                os.Exit(0)
            }
            parser.WriteHelp(os.Stdout)
        }
        os.Exit(1)
    }
}

What I am looking for is when I run the main function, it will print the help message with the grouped subcommands like:

group1:

--Cmd1

group2:

--Cmd2

However it groups the subcommands' options like:

group1:

--group1.opt1=

--group1.opt2=

group2:

--group2.optA=

--group2.optB=

Any ideas or help? Thanks!

1

There are 1 best solutions below

4
giyuu On

You can using function AddCommand and also could separate the package group1 and group2 to make it well classified.

Code:

package main

import (
    "fmt"
    "os"

    "github.com/jessevdk/go-flags"
)

// package group 1
type Group1 struct {
    Opt1 string `long:"opt1" description:"first opt" required:"true"`
    Opt2 int    `long:"opt2" description:"second opt" default:"10"`
}

var group1Command Group1
var group1 = "group1"

func (g *Group1) Execute(args []string) error {
    fmt.Printf("Option (opt1=%v, opt2=%v): \n", g.Opt1, g.Opt2)
    return nil
}

// package group 2
type Group2 struct {
    OptA string `long:"optA" description:"opt a" default:":8080"`
    OptB string `long:"optB" description:"opt b" default:"debug"`
}

var group2Command Group2
var group2 = "group2"

func (g *Group2) Execute(args []string) error {
    fmt.Printf("Option (opt1=%v, opt2=%v): \n", g.OptA, g.OptB)
    return nil
}

var parser = flags.NewParser(nil, flags.Default)

func init() {
    parser.AddCommand(group1,
        "Group 1 summary",
        "Long description of group 1",
        &group1Command)

    parser.AddCommand(group2,
        "Group 2 summary",
        "Long description of group 2",
        &group2Command)
}

func main() {
    if _, err := parser.Parse(); err != nil {
        switch flagsErr := err.(type) {
        case flags.ErrorType:
            if flagsErr == flags.ErrHelp {
                os.Exit(0)
            }
            os.Exit(1)
        default:
            os.Exit(1)
        }
    }
}

Result enter image description here

Further Reading

Docs