github.com/aergoio/aergo@v1.3.1/cmd/brick/exec/help.go (about)

     1  package exec
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/aergoio/aergo/cmd/brick/context"
     8  )
     9  
    10  func init() {
    11  	registerExec(&help{})
    12  }
    13  
    14  type help struct{}
    15  
    16  func (c *help) Command() string {
    17  	return "help"
    18  }
    19  
    20  func (c *help) Syntax() string {
    21  	return context.CommandSymbol
    22  }
    23  
    24  func (c *help) Usage() string {
    25  	return "help [command]"
    26  }
    27  
    28  func (c *help) Describe() string {
    29  	return "print usages and descriptions of commands"
    30  }
    31  
    32  func (c *help) Validate(args string) error {
    33  	if args == "" {
    34  		return nil
    35  	}
    36  
    37  	executor := GetExecutor(args)
    38  	if executor == nil {
    39  		return fmt.Errorf("command not found")
    40  	}
    41  	return nil
    42  }
    43  
    44  func (c *help) Run(args string) (string, error) {
    45  	var result strings.Builder
    46  
    47  	if args == "" {
    48  		// print whole usage guide
    49  		result.WriteString(fmt.Sprintf("Aergo Brick, Toy for Developing Contracts, version %s\n", context.GitHash))
    50  
    51  		result.WriteString(fmt.Sprintf("\n%-12s%s\n", "Command", "Usage"))
    52  		result.WriteString(fmt.Sprintf("=====================================================\n"))
    53  
    54  		for _, executor := range AllExecutors() {
    55  			result.WriteString(fmt.Sprintf("%-12s%s\n", executor.Command(), executor.Usage()))
    56  		}
    57  
    58  		return result.String(), nil
    59  	}
    60  
    61  	// print details of a specific command
    62  	executor := GetExecutor(args)
    63  	if executor != nil {
    64  		result.WriteString(fmt.Sprintf("%s\tusage: %s\tdescr: %s", executor.Command(), executor.Usage(), executor.Describe()))
    65  	}
    66  
    67  	return result.String(), nil
    68  }