Coder Social home page Coder Social logo

How to group subcommands about go-flags HOT 3 OPEN

xuezhaokun avatar xuezhaokun commented on July 17, 2024
How to group subcommands

from go-flags.

Comments (3)

nrobinson12 avatar nrobinson12 commented on July 17, 2024

I'm not quite sure the reason why you are using groups in this example here. Why do you want to group subcommands?
In my opinion, the commands themselves act like groups, especially in this example. You can achieve what you're looking for if you instead just use the commands and not involve groups:

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 {
	CmdA Cmd1 `command:"cmd1"`
	CmdB Cmd2 `command:"cmd2"`
}

Even simpler:

type MainCmd struct {
	Cmd1 struct {
		Opt1 string `long:"opt1" description:"first opt" required:"true"`
		Opt2 int    `long:"opt2" description:"second opt" default:"10"`
	} `command:"cmd1"`
	
	Cmd2 struct {
		OptA string `long:"optA" description:"opt a" default:":8080"`
		OptB string `long:"optB" description:"opt b" default:"debug"`
	} `command:"cmd2"`
}

Let me know if this helps.

from go-flags.

xuezhaokun avatar xuezhaokun commented on July 17, 2024

Thanks for the help. I tried the suggested approach; however, it did not give me the expected output. The help message I have is :

Available commands:
cmd1
cmd2

The example I provided is just a simple piece of workable code to demonstrate my basic need. I think there are many reasons people want to group or categorize subcommands, especially for organizing the help message better. For example, in some system setting related commands, we may want the help message organize the commands as follows:

Troubleshooting and Debugging Commands:
describe Show details
logs Print the logs
exec Execute a command

Settings Commands:
label Update the labels
annotate Update the annotations

Hope this can answer your concerns.

from go-flags.

maxlandon avatar maxlandon commented on July 17, 2024

Hello !
I'm allowing myself to redirect you to the last comment I posted in this issue: #380 .
It advertises a project I did work on a lot, and by which you might be interested.
The problem you have here is solved by the advertised project, and much more.

from go-flags.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.