4

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!

MC X
  • 337
  • 4
  • 16

1 Answers1

0

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

giyuu
  • 99
  • 4
  • example2 is my folder name due to I refer to example of the package (https://github.com/jessevdk/go-flags) when creating final code. – giyuu Aug 17 '22 at 15:57
  • It looks like it is making group1 and group2 as commands. What I am looking for is that when you run ./exmaple2 --help it will list all the subcommands inside each group. For example: --group1: Group1; --group2: Group2;. Now it can only lists all the available groups, which seems like commands. I do not want groups to be commands. I just want it to be a convenient way to organize all the subcommands. – MC X Aug 17 '22 at 17:13
  • I got it. I think, based on my exploration and found some comment in code, We can only add options to a group. got you concern, it's nice to have a command group like `kubectl` – giyuu Aug 20 '22 at 04:56
  • @MCX You must have seen some reference implementation to arrive at this requirement right? If not I'm not sure, from what I've seen the package can do, your requirement cannot be achieved. – Inian Aug 22 '22 at 09:52