command

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 9 Imported by: 0

README

command

utils for command line app

Provide two style for command line args parse:

  • use struct field tag
  • use command

Usage

use struct field tag

type targs struct {
	Test     string `flag_default:"test" flag_usage:"this is test"`
	TestBool bool   `flag_short:"b"`
	TestInt  int
}
	sargs := &targs{}
	v, _ := NewFVSet(sargs)
	v.Usage()
	err := Parse([]string{"--test", "test", "--test-bool", "--test-int", "1"}, v)

use command style

package main

import (
	"fmt"
	"os"

	"git.pyer.club/kingecg/command"
)

func main() {
	var cmd = command.NewCommand("test", "test command")
	cmd.AddArg("arg1", "a", "arg1 description", "default value")
	cmd.AddArg("arg2", "b", "arg2 description", "default value")
	cmd.AddSubCommand("sub1", "sub1 description")
	cmd.Parse(os.Args[1:])
	fmt.Println(cmd.GetGlobalOptions())
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UsageTemplate, _ = template.New("Usage").Parse(`Usage:
 {{if .ParentCommand }} {{.ParentCommand.Name}} {{end}} {{.Name}} [OPTIONS] [ARGS]
{{ if .Description }}
Description:
  {{.Description}}
{{ end }}
{{ if .Args }}
Options:
  {{range .Args}}
  -{{.ShortName}}|--{{.LongName}} {{.OType}} {{.Description}} default:{{.DefaultValue}}
  {{end}}
{{ end }}
{{ if .SubCommands }}
 Sub Commands:
  {{range .SubCommands}}
  {{.Name}} {{.Description}}
  {{end}}
{{end}}
`)

Functions

func Parse

func Parse(args []string, flagSet ...*FVSet) error

Parse is used to parse command line arguments based on one or more flag sets. args represents the list of command line arguments. flagSet is a variable-length parameter that contains the flag sets to be parsed. If no flag set is provided, it throws a panic exception. If only one flag set is provided, it directly parses the arguments with that flag set. If multiple flag sets are provided, it attempts to match and parse based on the first argument in args. If no matching flag set is found, it returns an error.

func ToKebabCase

func ToKebabCase(input string) string

Types

type Command

type Command struct {
	Name          string
	Description   string
	Args          []*Option
	SubCommands   []*Command
	ParentCommand *Command
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(name string, desc string) *Command

func (*Command) AddArg

func (c *Command) AddArg(name string, shortName string, description string, defaultValue interface{})

func (*Command) AddSubCommand

func (c *Command) AddSubCommand(name string, description string) *Command

func (*Command) GetGlobalOptions

func (c *Command) GetGlobalOptions() map[string]interface{}

func (*Command) GetOption

func (c *Command) GetOption(name string) *Option

func (*Command) GetSubCommand

func (c *Command) GetSubCommand() string

func (*Command) GetSubCommandOptions

func (c *Command) GetSubCommandOptions() map[string]interface{}

func (*Command) Parse

func (c *Command) Parse(args []string) ([]string, error)

func (*Command) Usage

func (c *Command) Usage()

type FVSet

type FVSet struct {
	Name  string
	Value *any
	// contains filtered or unexported fields
}

func NewFVSet

func NewFVSet(v interface{}) (*FVSet, error)

NewFVSet 创建一个新的FVSet实例,用于处理命令行标志。 参数v是一个指向结构体的指针,该结构体的字段将被用作命令行标志。 返回值是一个指向FVSet的指针和一个错误对象,如果操作成功,错误对象为nil。 如果v不是指向结构体的指针,函数将返回错误。

func (*FVSet) Args

func (f *FVSet) Args() interface{}

func (*FVSet) Parse

func (f *FVSet) Parse(args []string) error

func (*FVSet) Usage

func (f *FVSet) Usage()

type FVSets

type FVSets struct {
	// contains filtered or unexported fields
}

func (*FVSets) Add

func (f *FVSets) Add(v interface{}) error

func (*FVSets) Parse

func (f *FVSets) Parse(args []string) error

func (*FVSets) Usage

func (f *FVSets) Usage()

type FlagUsage

type FlagUsage struct {
	LongName  string
	ShortName string
	Usage     string
}

type Option

type Option struct {
	Name         string
	LongName     string
	ShortName    string
	Description  string
	OType        string
	DefaultValue interface{}
}

type Usage

type Usage struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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