Option types: bool int, int-array, string, string-array, string-map, file, dir
41 lines
823 B
Go
41 lines
823 B
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
type argSpec interface {
|
|
getBase() *argBase
|
|
parse(cli *CliParser, specIndex int, args []string, argIndex int) (consumedArgs int, err error)
|
|
}
|
|
|
|
type argBase struct {
|
|
name string
|
|
description string
|
|
required bool
|
|
repeat bool
|
|
}
|
|
|
|
// -------------- Argument Template functions ----------------
|
|
func (cli *CliParser) getArgTemplate(spec argSpec) (templ string) {
|
|
arg := spec.getBase()
|
|
templ = "<" + arg.name + ">"
|
|
if arg.repeat {
|
|
templ += " ..."
|
|
}
|
|
if !arg.required {
|
|
templ = "[" + templ + "]"
|
|
}
|
|
return
|
|
}
|
|
|
|
func (cli *CliParser) getArgsTemplate() (argsTemplate string) {
|
|
templates := make([]string, len(cli.argSpecs))
|
|
for i, argSpec := range cli.argSpecs {
|
|
templates[i] = cli.getArgTemplate(argSpec)
|
|
}
|
|
|
|
argsTemplate = strings.Join(templates, " ")
|
|
return
|
|
}
|