Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding to Opts API to make adding sub-command more friendly #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

millergarym
Copy link
Contributor

Allows for the following

var (
	rflg    = &types.Root{}
	cliBldr = opts.New(rflg).
		Name("wx").
		EmbedGlobalFlagSet().
		Complete()
)

func main() {
	cli := cliBldr.Parse()
	err := cli.Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%[2]s\nError: %[1]v\n\n", err, cli.Selected().Help())
		os.Exit(1)
	}
}

func init() {
	// using End
	cliBldr.AddCommand(opts.New(&struct{}{}).Name("cli").
		AddCommand(opts.New(rename.NewRename(rflg)).Name("rename")).
		AddCommand(opts.New(newsubcmd.New(rflg)).Name("new_sub_command")).
		End())

	// using AddStruct and NewStruct
	cliBldr.AddStruct("a",
		opts.NewStruct("b",
			opts.NewStruct("c",
				opts.NewStruct("d",
					opts.New(xxx.NewXxx(rflg)),
					opts.New(xxy.NewXxy(rflg)).Name("3456789"),
				),
			),
		),
	)

	// Adding new sub-command to existing cmd structure
	cliBldr.AddStruct("a/b/c/d",
		opts.New(xxx.NewXxx(rflg)).Name("abcsdaf"),
	)

	// Replace a sub-command struct holding position with a runnable sub-command
	cliBldr.AddCommand(opts.New(&versionCmd{}).Name("a"))

	// Adding new sub-command to existing cmd structure
	cliBldr.AddStruct("a/bb/cc/dd",
		opts.New(xxy.NewXxy(rflg)),
	)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant