Make adding subcommands a bit nicer
This commit is contained in:
parent
4396dede85
commit
5771bd0c01
@ -279,7 +279,7 @@ func (self *OptionGroup) FindOption(name_with_hyphens string) *Option {
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
type Command struct { // {{{
|
type Command struct { // {{{
|
||||||
Name string
|
Name, Group string
|
||||||
Usage, ShortDescription, HelpText string
|
Usage, ShortDescription, HelpText string
|
||||||
Hidden bool
|
Hidden bool
|
||||||
|
|
||||||
@ -287,13 +287,14 @@ type Command struct { // {{{
|
|||||||
AllowOptionsAfterArgs int
|
AllowOptionsAfterArgs int
|
||||||
// If true does not fail if the first non-option arg is not a sub-command
|
// If true does not fail if the first non-option arg is not a sub-command
|
||||||
SubCommandIsOptional bool
|
SubCommandIsOptional bool
|
||||||
|
// The entry point for this command
|
||||||
|
Run func(cmd *Command, args []string) (int, error)
|
||||||
|
|
||||||
SubCommandGroups []*CommandGroup
|
SubCommandGroups []*CommandGroup
|
||||||
OptionGroups []*OptionGroup
|
OptionGroups []*OptionGroup
|
||||||
Parent *Command
|
Parent *Command
|
||||||
|
|
||||||
Args []string
|
Args []string
|
||||||
Run func(cmd *Command, args []string) (int, error)
|
|
||||||
|
|
||||||
option_map map[string]*Option
|
option_map map[string]*Option
|
||||||
}
|
}
|
||||||
@ -317,17 +318,22 @@ func (self *Command) Clone(parent *Command) *Command {
|
|||||||
func (self *Command) AddClone(group string, src *Command) *Command {
|
func (self *Command) AddClone(group string, src *Command) *Command {
|
||||||
c := src.Clone(self)
|
c := src.Clone(self)
|
||||||
g := self.AddSubCommandGroup(group)
|
g := self.AddSubCommandGroup(group)
|
||||||
|
c.Group = g.Title
|
||||||
g.SubCommands = append(g.SubCommands, c)
|
g.SubCommands = append(g.SubCommands, c)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init_cmd(c *Command) {
|
||||||
|
c.SubCommandGroups = make([]*CommandGroup, 0, 8)
|
||||||
|
c.OptionGroups = make([]*OptionGroup, 0, 8)
|
||||||
|
c.Args = make([]string, 0, 8)
|
||||||
|
}
|
||||||
|
|
||||||
func NewRootCommand() *Command {
|
func NewRootCommand() *Command {
|
||||||
ans := Command{
|
ans := Command{
|
||||||
Name: filepath.Base(os.Args[0]),
|
Name: filepath.Base(os.Args[0]),
|
||||||
SubCommandGroups: make([]*CommandGroup, 0, 8),
|
|
||||||
OptionGroups: make([]*OptionGroup, 0, 8),
|
|
||||||
Args: make([]string, 0, 8),
|
|
||||||
}
|
}
|
||||||
|
init_cmd(&ans)
|
||||||
return &ans
|
return &ans
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -342,8 +348,12 @@ func (self *Command) AddSubCommandGroup(title string) *CommandGroup {
|
|||||||
return &ans
|
return &ans
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Command) AddSubCommand(group string, name string) *Command {
|
func (self *Command) AddSubCommand(ans *Command) *Command {
|
||||||
return self.AddSubCommandGroup(group).AddSubCommand(self, name)
|
g := self.AddSubCommandGroup(ans.Group)
|
||||||
|
g.SubCommands = append(g.SubCommands, ans)
|
||||||
|
init_cmd(ans)
|
||||||
|
ans.Parent = self
|
||||||
|
return ans
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Command) Validate() error {
|
func (self *Command) Validate() error {
|
||||||
|
|||||||
@ -68,7 +68,7 @@ func TestCLIParsing(t *testing.T) {
|
|||||||
|
|
||||||
root := NewRootCommand()
|
root := NewRootCommand()
|
||||||
root.Add(OptionSpec{Name: "--from-parent -p", Type: "count", Depth: 1})
|
root.Add(OptionSpec{Name: "--from-parent -p", Type: "count", Depth: 1})
|
||||||
child1 := root.AddSubCommand("", "child1")
|
child1 := root.AddSubCommand(&Command{Name: "child1"})
|
||||||
child1.Add(OptionSpec{Name: "--choices", Choices: "a b c"})
|
child1.Add(OptionSpec{Name: "--choices", Choices: "a b c"})
|
||||||
child1.Add(OptionSpec{Name: "--simple-string -s"})
|
child1.Add(OptionSpec{Name: "--simple-string -s"})
|
||||||
child1.Add(OptionSpec{Name: "--set-me", Type: "bool-set"})
|
child1.Add(OptionSpec{Name: "--set-me", Type: "bool-set"})
|
||||||
@ -76,7 +76,7 @@ func TestCLIParsing(t *testing.T) {
|
|||||||
child1.Add(OptionSpec{Name: "--float", Type: "float"})
|
child1.Add(OptionSpec{Name: "--float", Type: "float"})
|
||||||
child1.Add(OptionSpec{Name: "--list", Type: "list"})
|
child1.Add(OptionSpec{Name: "--list", Type: "list"})
|
||||||
child1.SubCommandIsOptional = true
|
child1.SubCommandIsOptional = true
|
||||||
gc1 := child1.AddSubCommand("", "gc1")
|
gc1 := child1.AddSubCommand(&Command{Name: "gc1"})
|
||||||
|
|
||||||
rt(
|
rt(
|
||||||
child1, "test --from-parent child1 -ps ss --choices b --from-parent one two",
|
child1, "test --from-parent child1 -ps ss --choices b --from-parent one two",
|
||||||
|
|||||||
@ -311,10 +311,12 @@ func setup_global_options(cmd *cli.Command) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EntryPoint(tool_root *cli.Command) *cli.Command {
|
func EntryPoint(tool_root *cli.Command) *cli.Command {
|
||||||
at_root_command := tool_root.AddSubCommand("", "@")
|
at_root_command := tool_root.AddSubCommand(&cli.Command{
|
||||||
at_root_command.Usage = "[global options] [sub-command] [sub-command options] [sub-command args]"
|
Name: "@",
|
||||||
at_root_command.ShortDescription = "Control kitty remotely"
|
Usage: "[global options] [sub-command] [sub-command options] [sub-command args]",
|
||||||
at_root_command.HelpText = "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` for this to work. When run without any sub-commands this will start an interactive shell to control kitty."
|
ShortDescription: "Control kitty remotely",
|
||||||
|
HelpText: "Control kitty by sending it commands. Set the allow_remote_control option in :file:`kitty.conf` for this to work. When run without any sub-commands this will start an interactive shell to control kitty.",
|
||||||
|
})
|
||||||
add_rc_global_opts(at_root_command)
|
add_rc_global_opts(at_root_command)
|
||||||
|
|
||||||
global_options_group := at_root_command.OptionGroups[0]
|
global_options_group := at_root_command.OptionGroups[0]
|
||||||
|
|||||||
@ -94,11 +94,13 @@ func run_CMD_NAME(cmd *cli.Command, args []string) (return_code int, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setup_CMD_NAME(parent *cli.Command) *cli.Command {
|
func setup_CMD_NAME(parent *cli.Command) *cli.Command {
|
||||||
ans := parent.AddSubCommand("", "CMD_NAME")
|
ans := parent.AddSubCommand(&cli.Command{
|
||||||
ans.Usage = "ARGSPEC"
|
Name: "CMD_NAME",
|
||||||
ans.ShortDescription = "SHORT_DESC"
|
Usage: "ARGSPEC",
|
||||||
ans.HelpText = "LONG_DESC"
|
ShortDescription: "SHORT_DESC",
|
||||||
ans.Run = run_CMD_NAME
|
HelpText: "LONG_DESC",
|
||||||
|
Run: run_CMD_NAME,
|
||||||
|
})
|
||||||
ADD_FLAGS_CODE
|
ADD_FLAGS_CODE
|
||||||
return ans
|
return ans
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,12 +94,13 @@ func main(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func EntryPoint(tool_root *cli.Command) {
|
func EntryPoint(tool_root *cli.Command) {
|
||||||
c := tool_root.AddSubCommand("", "__complete__")
|
tool_root.AddSubCommand(&cli.Command{
|
||||||
c.Usage = "output_type [shell state...]"
|
Name: "__complete__", Hidden: true,
|
||||||
c.Hidden = true
|
Usage: "output_type [shell state...]",
|
||||||
c.ShortDescription = "Generate completions for kitty commands"
|
ShortDescription: "Generate completions for kitty commands",
|
||||||
c.HelpText = "Generate completion candidates for kitty commands. The command line is read from STDIN. output_type can be one of the supported shells or 'json' for JSON output."
|
HelpText: "Generate completion candidates for kitty commands. The command line is read from STDIN. output_type can be one of the supported shells or 'json' for JSON output.",
|
||||||
c.Run = func(cmd *cli.Command, args []string) (ret int, err error) {
|
Run: func(cmd *cli.Command, args []string) (ret int, err error) {
|
||||||
return ret, main(args)
|
return ret, main(args)
|
||||||
}
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user