github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/command/command.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package cmd
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/protocol"
     7  )
     8  
     9  type Command struct {
    10  	// parent is a parent command for this command.
    11  	parent protocol.Command
    12  	// Commands lists the available commands and help topics.
    13  	// The order here is the order in which they are printed by 'go help'.
    14  	// Note that subcommands are in general best avoided.
    15  	subCommands []protocol.Command
    16  }
    17  
    18  func (c *Command) Init(parent protocol.Command, cmd ...protocol.Command) {
    19  	c.parent = parent
    20  	// TODO::: check duplicate name usage
    21  	c.subCommands = append(c.subCommands, cmd...)
    22  }
    23  
    24  //libgo:impl protocol.Command
    25  func (c *Command) Name() string                    { panic("Dev must implement Name() method to overwrite this method") }
    26  func (c *Command) Aliases() []string               { return nil }
    27  func (c *Command) UsageLine() string               { return "" }
    28  func (c *Command) Runnable() bool                  { return false }
    29  func (c *Command) Parent() protocol.Command        { return c.parent }
    30  func (c *Command) SubCommands() []protocol.Command { return c.subCommands }
    31  func (c *Command) SubCommand(name string) protocol.Command {
    32  	// TODO::: intelligent suggestion or correction
    33  	for _, cmd := range c.subCommands {
    34  		if cmd.Name() == name {
    35  			return cmd
    36  		}
    37  		for _, alias := range cmd.Aliases() {
    38  			if alias == name {
    39  				return cmd
    40  			}
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  // ServeCLI read and write to os.Stdin, os.Stdout, and os.Stderr files
    47  func (c *Command) ServeCLI() (err protocol.Error) {
    48  	err = &ErrServiceNotAcceptCLI
    49  	return
    50  }