cli

package
v1.0.0-pre Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// define valid flag type in command file.
	FLAG_TYPE_INT    = "int"
	FLAG_TYPE_FLOAT  = "float"
	FLAG_TYPE_STRING = "string"
	FLAG_TYPE_BOOL   = "bool"
)

Variables

This section is empty.

Functions

func DefaultCommandPath

func DefaultCommandPath(path string)

DefaultCommandPath set default command path

func DefaultDelimiter

func DefaultDelimiter(delimiter string)

func Execute

func Execute(mountFuncs ...func(*cobra.Command))

Instruction execution entry. mountFunc represents that the root command is passed into this method, which is used to expand the mounting custom command set. There are some default internal mount method, like [mountVersion] etc. Perhaps we could obtain the context here to control which commands can be added to the command tree, but the simplest way is not to set a default command to add, ensuring that the command tree is always empty. This may be useful for some commands. Therefore, we can only add commands that are not related to business logic to the command tree as tools, and the commands related to writing later can be set in the actual project.

func GetCommand

func GetCommand(verb string, delimiter string) *cobra.Command

GetCommand return the cobra's Command from command dict and will panic if not exists the specific verb. It will set internal command dict cache in package cli.

func MountCmd

func MountCmd(verb string, cmd *Command, dictkey string) *cobra.Command

MountCmd performs the same operation as the built-in mountCmd, except that it mounts the commands to the specified dictionary. If the specified dictionary does not exist, a panic is triggered.

func MountServer

func MountServer(rootCmd *cobra.Command)

MountServer mount the verb-server to the instruction tree. For details, see the command.json file in the project structure to find the composition structure of the corresponding instruction.

func RootLong

func RootLong(long string)

func RootShort

func RootShort(short string)

func RootVerb

func RootVerb(verb string)

Types

type Cli

type Cli struct {
	App      string    `json:"app"`      // cli name used for root command
	Entry    []string  `json:"entry"`    // execute entry, like Package main.
	Version  string    `json:"version"`  // cli version
	Intro    string    `json:"intro"`    // cli introduction
	Commands []Command `json:"commands"` // all cli commands
}

Cli is the standard format after parsing the command file.

func GetCLI

func GetCLI() *Cli

GetCmd return the pointer to Cli from config dict and will panic if not exists Cli.

func LoadCmd

func LoadCmd(path string) *Cli

LoadCmd parse the specified file to the cli instance and build the instruction dictionary.

type Command

type Command struct {
	Verb            string    `json:"verb"`            // called verb
	ShortDesc       string    `json:"shortDesc"`       // short description for verb
	LongDesc        string    `json:"longDesc"`        // long description for verb
	PersistentFlags []Flag    `json:"persistentFlags"` // persistent flags for current command
	LocalFlags      []Flag    `json:"localFlags"`      // local flags for current command
	SubCommand      []Command `json:"subCommands"`     // subcommands for current command
}

Command store command information.

type Flag

type Flag struct {
	FullName  string `json:"fullName"`  //  full name
	ShortName string `json:"shortName"` //  short name
	Type      string `json:"type"`      //  value type
	Desc      string `json:"desc"`      // description
	Default   string `json:"default"`   // default value
}

Flag store flag information.

func ParseLocalFlags

func ParseLocalFlags(verb string, delimiter string, entry *Cli) []Flag

ParseLocalFlags return the list for local flag of specific verb. No panic here.

func ParsePersistenFlags

func ParsePersistenFlags(verb string, delimiter string, entry *Cli) []Flag

The idea here is to try to enter from any top-level instruction in the already successfully parsed CLI structure, but obviously this can be automatically obtained during the recursion process, so it is retained here, but there is a fast flag loading version. ParsePersistenFlags return the list for persistent flag of specific verb. No panic here.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL