Skip to content

Allow Root commands when there are subcommands #561

@F21

Description

@F21

If I have a CLI with sub commands, it is not possible to call the root comand, even if Run() is defined at the root.

For example:

type CLI struct {
	SubCmd1 SubCmd1 `cmd:""`
	SubCmd2 SubCmd2  `cmd:""`
}

func (cli *CLI) Run() error {
	fmt.Println("This is the root command")
	return nil
}

type SubCmd1 struct {}

func (c *SubCmd1) Run() error {
	fmt.Println("This is sub command 1")
	return nil
}

type SubCmd2 struct {}

func (c *SubCmd3) Run() error {
	fmt.Println("This is sub command 3")
	return nil
}

func main() {
	var cli CLI
	ctx := kong.Parse(&cli)
	err := ctx.Run()
	ctx.FatalIfErrorf(err)
}

Upon compiling, it is not possible to call ./myapp as it will return the following error: myapp: error: expected one of "sub-cmd1", "sub-cmd2". I would love to have the ability to call Run() on the root: ./myapp as well as the sub commands: ./myapp sub-cmd1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions