Compare commits
4 Commits
622d47071a
...
arghing
Author | SHA1 | Date | |
---|---|---|---|
3de96b813f
|
|||
a73acedc6d
|
|||
92e3d6fe5b
|
|||
395006cdb8
|
@@ -15,7 +15,16 @@ func main() {
|
|||||||
|
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
pt, err := argh.ParseArgs(os.Args, nil)
|
pt, err := argh.ParseArgs(os.Args, argh.NewParserConfig(
|
||||||
|
&argh.CommandConfig{
|
||||||
|
NValue: argh.OneOrMoreValue,
|
||||||
|
ValueNames: []string{"topping"},
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Automatic: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
@@ -11,7 +11,7 @@ type parser struct {
|
|||||||
|
|
||||||
cfg *ParserConfig
|
cfg *ParserConfig
|
||||||
|
|
||||||
errors ScannerErrorList
|
errors ParserErrorList
|
||||||
|
|
||||||
tok Token
|
tok Token
|
||||||
lit string
|
lit string
|
||||||
@@ -36,8 +36,12 @@ func ParseArgs(args []string, pCfg *ParserConfig) (*ParseTree, error) {
|
|||||||
return p.parseArgs()
|
return p.parseArgs()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *parser) addError(msg string) {
|
||||||
|
p.errors.Add(Position{Column: int(p.pos)}, msg)
|
||||||
|
}
|
||||||
|
|
||||||
func (p *parser) init(r io.Reader, pCfg *ParserConfig) {
|
func (p *parser) init(r io.Reader, pCfg *ParserConfig) {
|
||||||
p.errors = ScannerErrorList{}
|
p.errors = ParserErrorList{}
|
||||||
|
|
||||||
if pCfg == nil {
|
if pCfg == nil {
|
||||||
pCfg = POSIXyParserConfig
|
pCfg = POSIXyParserConfig
|
||||||
@@ -103,7 +107,9 @@ func (p *parser) parseCommand(cCfg *CommandConfig) Node {
|
|||||||
tracef("parseCommand(...) for=%d nodes=%+#v", i, nodes)
|
tracef("parseCommand(...) for=%d nodes=%+#v", i, nodes)
|
||||||
tracef("parseCommand(...) for=%d tok=%s lit=%q pos=%v", i, p.tok, p.lit, p.pos)
|
tracef("parseCommand(...) for=%d tok=%s lit=%q pos=%v", i, p.tok, p.lit, p.pos)
|
||||||
|
|
||||||
if subCfg, ok := cCfg.Commands[p.lit]; ok {
|
tracef("parseCommand(...) cCfg=%+#v", cCfg)
|
||||||
|
|
||||||
|
if subCfg, ok := cCfg.GetCommandConfig(p.lit); ok {
|
||||||
subCommand := p.lit
|
subCommand := p.lit
|
||||||
|
|
||||||
nodes = append(nodes, p.parseCommand(&subCfg))
|
nodes = append(nodes, p.parseCommand(&subCfg))
|
||||||
@@ -156,7 +162,7 @@ func (p *parser) parseCommand(cCfg *CommandConfig) Node {
|
|||||||
case ASSIGN:
|
case ASSIGN:
|
||||||
tracef("parseCommand(...) error on bare %s", p.tok)
|
tracef("parseCommand(...) error on bare %s", p.tok)
|
||||||
|
|
||||||
p.errors.Add(Position{Column: int(p.pos)}, "invalid bare assignment")
|
p.addError("invalid bare assignment")
|
||||||
|
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
@@ -182,45 +188,49 @@ func (p *parser) parseIdent() Node {
|
|||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseFlag(flCfgMap map[string]FlagConfig) Node {
|
func (p *parser) parseFlag(flags *Flags) Node {
|
||||||
switch p.tok {
|
switch p.tok {
|
||||||
case SHORT_FLAG:
|
case SHORT_FLAG:
|
||||||
tracef("parseFlag(...) parsing short flag with config=%+#v", flCfgMap)
|
tracef("parseFlag(...) parsing short flag with config=%+#v", flags)
|
||||||
return p.parseShortFlag(flCfgMap)
|
return p.parseShortFlag(flags)
|
||||||
case LONG_FLAG:
|
case LONG_FLAG:
|
||||||
tracef("parseFlag(...) parsing long flag with config=%+#v", flCfgMap)
|
tracef("parseFlag(...) parsing long flag with config=%+#v", flags)
|
||||||
return p.parseLongFlag(flCfgMap)
|
return p.parseLongFlag(flags)
|
||||||
case COMPOUND_SHORT_FLAG:
|
case COMPOUND_SHORT_FLAG:
|
||||||
tracef("parseFlag(...) parsing compound short flag with config=%+#v", flCfgMap)
|
tracef("parseFlag(...) parsing compound short flag with config=%+#v", flags)
|
||||||
return p.parseCompoundShortFlag(flCfgMap)
|
return p.parseCompoundShortFlag(flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(fmt.Sprintf("token %v cannot be parsed as flag", p.tok))
|
panic(fmt.Sprintf("token %v cannot be parsed as flag", p.tok))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseShortFlag(flCfgMap map[string]FlagConfig) Node {
|
func (p *parser) parseShortFlag(flags *Flags) Node {
|
||||||
node := &Flag{Name: string(p.lit[1])}
|
node := &Flag{Name: string(p.lit[1])}
|
||||||
|
|
||||||
flCfg, ok := flCfgMap[node.Name]
|
flCfg, ok := flags.Get(node.Name)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
p.addError(fmt.Sprintf("unknown flag %q", node.Name))
|
||||||
|
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.parseConfiguredFlag(node, flCfg)
|
return p.parseConfiguredFlag(node, flCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseLongFlag(flCfgMap map[string]FlagConfig) Node {
|
func (p *parser) parseLongFlag(flags *Flags) Node {
|
||||||
node := &Flag{Name: string(p.lit[2:])}
|
node := &Flag{Name: string(p.lit[2:])}
|
||||||
|
|
||||||
flCfg, ok := flCfgMap[node.Name]
|
flCfg, ok := flags.Get(node.Name)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
p.addError(fmt.Sprintf("unknown flag %q", node.Name))
|
||||||
|
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.parseConfiguredFlag(node, flCfg)
|
return p.parseConfiguredFlag(node, flCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) parseCompoundShortFlag(flCfgMap map[string]FlagConfig) Node {
|
func (p *parser) parseCompoundShortFlag(flags *Flags) Node {
|
||||||
flagNodes := []Node{}
|
flagNodes := []Node{}
|
||||||
|
|
||||||
withoutFlagPrefix := p.lit[1:]
|
withoutFlagPrefix := p.lit[1:]
|
||||||
@@ -229,12 +239,16 @@ func (p *parser) parseCompoundShortFlag(flCfgMap map[string]FlagConfig) Node {
|
|||||||
node := &Flag{Name: string(r)}
|
node := &Flag{Name: string(r)}
|
||||||
|
|
||||||
if i == len(withoutFlagPrefix)-1 {
|
if i == len(withoutFlagPrefix)-1 {
|
||||||
flCfg, ok := flCfgMap[node.Name]
|
flCfg, ok := flags.Get(node.Name)
|
||||||
if ok {
|
if !ok {
|
||||||
flagNodes = append(flagNodes, p.parseConfiguredFlag(node, flCfg))
|
p.addError(fmt.Sprintf("unknown flag %q", node.Name))
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flagNodes = append(flagNodes, p.parseConfiguredFlag(node, flCfg))
|
||||||
|
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
flagNodes = append(flagNodes, node)
|
flagNodes = append(flagNodes, node)
|
||||||
|
@@ -7,15 +7,17 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
POSIXyParserConfig = &ParserConfig{
|
POSIXyParserConfig = NewParserConfig(
|
||||||
Prog: CommandConfig{},
|
nil,
|
||||||
ScannerConfig: POSIXyScannerConfig,
|
POSIXyScannerConfig,
|
||||||
}
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
type NValue int
|
type NValue int
|
||||||
|
|
||||||
func (nv NValue) Contains(i int) bool {
|
func (nv NValue) Contains(i int) bool {
|
||||||
|
tracef("NValue.Contains(%v)", i)
|
||||||
|
|
||||||
if i < int(ZeroValue) {
|
if i < int(ZeroValue) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -33,14 +35,112 @@ type ParserConfig struct {
|
|||||||
ScannerConfig *ScannerConfig
|
ScannerConfig *ScannerConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewParserConfig(prog *CommandConfig, sCfg *ScannerConfig) *ParserConfig {
|
||||||
|
if sCfg == nil {
|
||||||
|
sCfg = POSIXyScannerConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
if prog == nil {
|
||||||
|
prog = &CommandConfig{}
|
||||||
|
}
|
||||||
|
|
||||||
|
prog.init()
|
||||||
|
|
||||||
|
pCfg := &ParserConfig{
|
||||||
|
Prog: *prog,
|
||||||
|
ScannerConfig: sCfg,
|
||||||
|
}
|
||||||
|
|
||||||
|
return pCfg
|
||||||
|
}
|
||||||
|
|
||||||
type CommandConfig struct {
|
type CommandConfig struct {
|
||||||
NValue NValue
|
NValue NValue
|
||||||
ValueNames []string
|
ValueNames []string
|
||||||
Flags map[string]FlagConfig
|
Flags *Flags
|
||||||
Commands map[string]CommandConfig
|
Commands *Commands
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cCfg *CommandConfig) init() {
|
||||||
|
if cCfg.ValueNames == nil {
|
||||||
|
cCfg.ValueNames = []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cCfg.Flags == nil {
|
||||||
|
cCfg.Flags = &Flags{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cCfg.Commands == nil {
|
||||||
|
cCfg.Commands = &Commands{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cCfg *CommandConfig) GetCommandConfig(name string) (CommandConfig, bool) {
|
||||||
|
tracef("CommandConfig.GetCommandConfig(%q)", name)
|
||||||
|
|
||||||
|
if cCfg.Commands == nil {
|
||||||
|
cCfg.Commands = &Commands{Map: map[string]CommandConfig{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cCfg.Commands.Get(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cCfg *CommandConfig) GetFlagConfig(name string) (FlagConfig, bool) {
|
||||||
|
tracef("CommandConfig.GetFlagConfig(%q)", name)
|
||||||
|
|
||||||
|
if cCfg.Flags == nil {
|
||||||
|
cCfg.Flags = &Flags{Map: map[string]FlagConfig{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cCfg.Flags.Get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FlagConfig struct {
|
type FlagConfig struct {
|
||||||
NValue NValue
|
NValue NValue
|
||||||
|
Persist bool
|
||||||
ValueNames []string
|
ValueNames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Flags struct {
|
||||||
|
Parent *Flags
|
||||||
|
Map map[string]FlagConfig
|
||||||
|
|
||||||
|
Automatic bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fl *Flags) Get(name string) (FlagConfig, bool) {
|
||||||
|
tracef("Flags.Get(%q)", name)
|
||||||
|
|
||||||
|
if fl.Map == nil {
|
||||||
|
fl.Map = map[string]FlagConfig{}
|
||||||
|
}
|
||||||
|
|
||||||
|
flCfg, ok := fl.Map[name]
|
||||||
|
if !ok {
|
||||||
|
if fl.Automatic {
|
||||||
|
return FlagConfig{}, true
|
||||||
|
}
|
||||||
|
|
||||||
|
if fl.Parent != nil {
|
||||||
|
flCfg, ok = fl.Parent.Get(name)
|
||||||
|
return flCfg, ok && flCfg.Persist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return flCfg, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
type Commands struct {
|
||||||
|
Map map[string]CommandConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmd *Commands) Get(name string) (CommandConfig, bool) {
|
||||||
|
tracef("Commands.Get(%q)", name)
|
||||||
|
|
||||||
|
if cmd.Map == nil {
|
||||||
|
cmd.Map = map[string]CommandConfig{}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdCfg, ok := cmd.Map[name]
|
||||||
|
return cmdCfg, ok
|
||||||
|
}
|
||||||
|
88
argh/parser_error.go
Normal file
88
argh/parser_error.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package argh
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ParserError is largely borrowed from go/scanner.Error
|
||||||
|
type ParserError struct {
|
||||||
|
Pos Position
|
||||||
|
Msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e ParserError) Error() string {
|
||||||
|
if e.Pos.IsValid() {
|
||||||
|
return e.Pos.String() + ":" + e.Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParserErrorList is largely borrowed from go/scanner.ErrorList
|
||||||
|
type ParserErrorList []*ParserError
|
||||||
|
|
||||||
|
func (el *ParserErrorList) Add(pos Position, msg string) {
|
||||||
|
*el = append(*el, &ParserError{Pos: pos, Msg: msg})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el *ParserErrorList) Reset() { *el = (*el)[0:0] }
|
||||||
|
|
||||||
|
func (el ParserErrorList) Len() int { return len(el) }
|
||||||
|
|
||||||
|
func (el ParserErrorList) Swap(i, j int) { el[i], el[j] = el[j], el[i] }
|
||||||
|
|
||||||
|
func (el ParserErrorList) Less(i, j int) bool {
|
||||||
|
e := &el[i].Pos
|
||||||
|
f := &el[j].Pos
|
||||||
|
|
||||||
|
if e.Column != f.Column {
|
||||||
|
return e.Column < f.Column
|
||||||
|
}
|
||||||
|
|
||||||
|
return el[i].Msg < el[j].Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el ParserErrorList) Sort() {
|
||||||
|
sort.Sort(el)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el ParserErrorList) Error() string {
|
||||||
|
switch len(el) {
|
||||||
|
case 0:
|
||||||
|
return "no errors"
|
||||||
|
case 1:
|
||||||
|
return el[0].Error()
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s (and %d more errors)", el[0], len(el)-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el ParserErrorList) Err() error {
|
||||||
|
if len(el) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return el
|
||||||
|
}
|
||||||
|
|
||||||
|
func (el ParserErrorList) Is(other error) bool {
|
||||||
|
if _, ok := other.(ParserErrorList); ok {
|
||||||
|
return el.Error() == other.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := other.(*ParserErrorList); ok {
|
||||||
|
return el.Error() == (*v).Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintParserError(w io.Writer, err error) {
|
||||||
|
if list, ok := err.(ParserErrorList); ok {
|
||||||
|
for _, e := range list {
|
||||||
|
fmt.Fprintf(w, "%s\n", e)
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Fprintf(w, "%s\n", err)
|
||||||
|
}
|
||||||
|
}
|
@@ -25,10 +25,20 @@ func TestParser(t *testing.T) {
|
|||||||
},
|
},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
Flags: &argh.Flags{
|
||||||
"hello": argh.CommandConfig{
|
Map: map[string]argh.FlagConfig{
|
||||||
NValue: 1,
|
"e": {},
|
||||||
ValueNames: []string{"name"},
|
"a": {},
|
||||||
|
"t": {},
|
||||||
|
"wat": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Commands: &argh.Commands{
|
||||||
|
Map: map[string]argh.CommandConfig{
|
||||||
|
"hello": argh.CommandConfig{
|
||||||
|
NValue: 1,
|
||||||
|
ValueNames: []string{"name"},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -82,6 +92,89 @@ func TestParser(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "persistent flags",
|
||||||
|
args: []string{
|
||||||
|
"pies", "--wat", "hello", "mario", "-eat",
|
||||||
|
},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: func() argh.CommandConfig {
|
||||||
|
cmdCfg := argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"e": {Persist: true},
|
||||||
|
"a": {Persist: true},
|
||||||
|
"t": {Persist: true},
|
||||||
|
"wat": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdCfg.Commands = &argh.Commands{
|
||||||
|
Map: map[string]argh.CommandConfig{
|
||||||
|
"hello": argh.CommandConfig{
|
||||||
|
NValue: 1,
|
||||||
|
ValueNames: []string{"name"},
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Parent: cmdCfg.Flags,
|
||||||
|
Map: map[string]argh.FlagConfig{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdCfg
|
||||||
|
}(),
|
||||||
|
},
|
||||||
|
expPT: []argh.Node{
|
||||||
|
&argh.Command{
|
||||||
|
Name: "pies",
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Flag{Name: "wat"},
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Command{
|
||||||
|
Name: "hello",
|
||||||
|
Values: map[string]string{
|
||||||
|
"name": "mario",
|
||||||
|
},
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Ident{Literal: "mario"},
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.CompoundShortFlag{
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.Flag{Name: "e"},
|
||||||
|
&argh.Flag{Name: "a"},
|
||||||
|
&argh.Flag{Name: "t"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expAST: []argh.Node{
|
||||||
|
&argh.Command{
|
||||||
|
Name: "pies",
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.Flag{Name: "wat"},
|
||||||
|
&argh.Command{
|
||||||
|
Name: "hello",
|
||||||
|
Values: map[string]string{
|
||||||
|
"name": "mario",
|
||||||
|
},
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.Ident{Literal: "mario"},
|
||||||
|
&argh.Flag{Name: "e"},
|
||||||
|
&argh.Flag{Name: "a"},
|
||||||
|
&argh.Flag{Name: "t"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "bare",
|
name: "bare",
|
||||||
args: []string{"pizzas"},
|
args: []string{"pizzas"},
|
||||||
@@ -173,6 +266,17 @@ func TestParser(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "long value-less flags",
|
name: "long value-less flags",
|
||||||
args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"},
|
args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"tasty": {},
|
||||||
|
"fresh": {},
|
||||||
|
"super-hot-right-now": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
&argh.Command{
|
&argh.Command{
|
||||||
Name: "pizzas",
|
Name: "pizzas",
|
||||||
@@ -209,10 +313,15 @@ func TestParser(t *testing.T) {
|
|||||||
},
|
},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{},
|
Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}},
|
||||||
Flags: map[string]argh.FlagConfig{
|
Flags: &argh.Flags{
|
||||||
"fresh": argh.FlagConfig{NValue: 1},
|
Map: map[string]argh.FlagConfig{
|
||||||
"box": argh.FlagConfig{NValue: argh.OneOrMoreValue},
|
"tasty": {},
|
||||||
|
"fresh": argh.FlagConfig{NValue: 1},
|
||||||
|
"super-hot-right-now": {},
|
||||||
|
"box": argh.FlagConfig{NValue: argh.OneOrMoreValue},
|
||||||
|
"please": {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -281,6 +390,17 @@ func TestParser(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "short value-less flags",
|
name: "short value-less flags",
|
||||||
args: []string{"pizzas", "-t", "-f", "-s"},
|
args: []string{"pizzas", "-t", "-f", "-s"},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"t": {},
|
||||||
|
"f": {},
|
||||||
|
"s": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
&argh.Command{
|
&argh.Command{
|
||||||
Name: "pizzas",
|
Name: "pizzas",
|
||||||
@@ -308,6 +428,19 @@ func TestParser(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "compound short flags",
|
name: "compound short flags",
|
||||||
args: []string{"pizzas", "-aca", "-blol"},
|
args: []string{"pizzas", "-aca", "-blol"},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"a": {},
|
||||||
|
"b": {},
|
||||||
|
"c": {},
|
||||||
|
"l": {},
|
||||||
|
"o": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
&argh.Command{
|
&argh.Command{
|
||||||
Name: "pizzas",
|
Name: "pizzas",
|
||||||
@@ -352,9 +485,15 @@ func TestParser(t *testing.T) {
|
|||||||
args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"},
|
args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{},
|
Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}},
|
||||||
Flags: map[string]argh.FlagConfig{
|
Flags: &argh.Flags{
|
||||||
"b": argh.FlagConfig{NValue: 1},
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"a": {},
|
||||||
|
"b": argh.FlagConfig{NValue: 1},
|
||||||
|
"ca": {},
|
||||||
|
"l": {},
|
||||||
|
"o": {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -411,14 +550,24 @@ func TestParser(t *testing.T) {
|
|||||||
args: []string{"pizzas", "fly", "freely", "sometimes", "and", "other", "times", "fry", "deeply", "--forever"},
|
args: []string{"pizzas", "fly", "freely", "sometimes", "and", "other", "times", "fry", "deeply", "--forever"},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
Commands: &argh.Commands{
|
||||||
"fly": argh.CommandConfig{
|
Map: map[string]argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
"fly": argh.CommandConfig{
|
||||||
"fry": argh.CommandConfig{},
|
Commands: &argh.Commands{
|
||||||
|
Map: map[string]argh.CommandConfig{
|
||||||
|
"fry": argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"forever": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Flags: map[string]argh.FlagConfig{},
|
Flags: &argh.Flags{Map: map[string]argh.FlagConfig{}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
@@ -484,14 +633,16 @@ func TestParser(t *testing.T) {
|
|||||||
args: []string{"pizzas", "-need", "sauce", "heat", "love", "-also", "over9000"},
|
args: []string{"pizzas", "-need", "sauce", "heat", "love", "-also", "over9000"},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Flags: map[string]argh.FlagConfig{
|
Flags: &argh.Flags{
|
||||||
"a": {NValue: argh.ZeroOrMoreValue},
|
Map: map[string]argh.FlagConfig{
|
||||||
"d": {NValue: argh.OneOrMoreValue},
|
"a": {NValue: argh.ZeroOrMoreValue},
|
||||||
"e": {},
|
"d": {NValue: argh.OneOrMoreValue},
|
||||||
"l": {},
|
"e": {},
|
||||||
"n": {},
|
"l": {},
|
||||||
"o": {NValue: 1, ValueNames: []string{"level"}},
|
"n": {},
|
||||||
"s": {NValue: argh.ZeroOrMoreValue},
|
"o": {NValue: 1, ValueNames: []string{"level"}},
|
||||||
|
"s": {NValue: argh.ZeroOrMoreValue},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -585,24 +736,32 @@ func TestParser(t *testing.T) {
|
|||||||
args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"},
|
args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
Commands: &argh.Commands{
|
||||||
"fly": argh.CommandConfig{
|
Map: map[string]argh.CommandConfig{
|
||||||
Flags: map[string]argh.FlagConfig{
|
"fly": argh.CommandConfig{
|
||||||
"freely": {},
|
Flags: &argh.Flags{
|
||||||
},
|
Map: map[string]argh.FlagConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
"freely": {},
|
||||||
"fry": argh.CommandConfig{
|
},
|
||||||
Flags: map[string]argh.FlagConfig{
|
},
|
||||||
"deeply": {},
|
Commands: &argh.Commands{
|
||||||
"w": {},
|
Map: map[string]argh.CommandConfig{
|
||||||
"A": {},
|
"fry": argh.CommandConfig{
|
||||||
"t": argh.FlagConfig{NValue: 1},
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"deeply": {},
|
||||||
|
"w": {},
|
||||||
|
"A": {},
|
||||||
|
"t": argh.FlagConfig{NValue: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Flags: map[string]argh.FlagConfig{},
|
Flags: &argh.Flags{Map: map[string]argh.FlagConfig{}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
@@ -677,19 +836,25 @@ func TestParser(t *testing.T) {
|
|||||||
args: []string{"PIZZAs", "^wAT@golf", "^^hecKing", "goose", "bonk", "^^FIERCENESS@-2"},
|
args: []string{"PIZZAs", "^wAT@golf", "^^hecKing", "goose", "bonk", "^^FIERCENESS@-2"},
|
||||||
cfg: &argh.ParserConfig{
|
cfg: &argh.ParserConfig{
|
||||||
Prog: argh.CommandConfig{
|
Prog: argh.CommandConfig{
|
||||||
Commands: map[string]argh.CommandConfig{
|
Commands: &argh.Commands{
|
||||||
"goose": argh.CommandConfig{
|
Map: map[string]argh.CommandConfig{
|
||||||
NValue: 1,
|
"goose": argh.CommandConfig{
|
||||||
Flags: map[string]argh.FlagConfig{
|
NValue: 1,
|
||||||
"FIERCENESS": argh.FlagConfig{NValue: 1},
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"FIERCENESS": argh.FlagConfig{NValue: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Flags: map[string]argh.FlagConfig{
|
Flags: &argh.Flags{
|
||||||
"w": argh.FlagConfig{},
|
Map: map[string]argh.FlagConfig{
|
||||||
"A": argh.FlagConfig{},
|
"w": argh.FlagConfig{},
|
||||||
"T": argh.FlagConfig{NValue: 1},
|
"A": argh.FlagConfig{},
|
||||||
"hecking": argh.FlagConfig{},
|
"T": argh.FlagConfig{NValue: 1},
|
||||||
|
"hecKing": argh.FlagConfig{},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ScannerConfig: &argh.ScannerConfig{
|
ScannerConfig: &argh.ScannerConfig{
|
||||||
@@ -741,11 +906,67 @@ func TestParser(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "windows like",
|
||||||
|
args: []string{"hotdog", "/f", "/L", "/o:ppy", "hats"},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"f": {},
|
||||||
|
"L": {},
|
||||||
|
"o": argh.FlagConfig{NValue: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Commands: &argh.Commands{
|
||||||
|
Map: map[string]argh.CommandConfig{
|
||||||
|
"hats": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ScannerConfig: &argh.ScannerConfig{
|
||||||
|
AssignmentOperator: ':',
|
||||||
|
FlagPrefix: '/',
|
||||||
|
MultiValueDelim: ',',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expPT: []argh.Node{
|
||||||
|
&argh.Command{
|
||||||
|
Name: "hotdog",
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Flag{Name: "f"},
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Flag{Name: "L"},
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Flag{
|
||||||
|
Name: "o",
|
||||||
|
Values: map[string]string{"0": "ppy"},
|
||||||
|
Nodes: []argh.Node{
|
||||||
|
&argh.Assign{},
|
||||||
|
&argh.Ident{Literal: "ppy"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&argh.ArgDelimiter{},
|
||||||
|
&argh.Command{Name: "hats"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "invalid bare assignment",
|
name: "invalid bare assignment",
|
||||||
args: []string{"pizzas", "=", "--wat"},
|
args: []string{"pizzas", "=", "--wat"},
|
||||||
expErr: argh.ScannerErrorList{
|
cfg: &argh.ParserConfig{
|
||||||
&argh.ScannerError{Pos: argh.Position{Column: 8}, Msg: "invalid bare assignment"},
|
Prog: argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"wat": {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expErr: argh.ParserErrorList{
|
||||||
|
&argh.ParserError{Pos: argh.Position{Column: 8}, Msg: "invalid bare assignment"},
|
||||||
},
|
},
|
||||||
expPT: []argh.Node{
|
expPT: []argh.Node{
|
||||||
&argh.Command{
|
&argh.Command{
|
||||||
|
@@ -16,8 +16,23 @@ func TestQuerier_Program(t *testing.T) {
|
|||||||
expOK bool
|
expOK bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "typical",
|
name: "typical",
|
||||||
args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
|
args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
|
||||||
|
cfg: &argh.ParserConfig{
|
||||||
|
Prog: argh.CommandConfig{
|
||||||
|
Commands: &argh.Commands{
|
||||||
|
Map: map[string]argh.CommandConfig{
|
||||||
|
"ahoy": argh.CommandConfig{
|
||||||
|
Flags: &argh.Flags{
|
||||||
|
Map: map[string]argh.FlagConfig{
|
||||||
|
"treatsa": argh.FlagConfig{NValue: 1},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
exp: "pizzas",
|
exp: "pizzas",
|
||||||
expOK: true,
|
expOK: true,
|
||||||
},
|
},
|
||||||
|
@@ -1,88 +0,0 @@
|
|||||||
package argh
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ScannerError is largely borrowed from go/scanner.Error
|
|
||||||
type ScannerError struct {
|
|
||||||
Pos Position
|
|
||||||
Msg string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e ScannerError) Error() string {
|
|
||||||
if e.Pos.IsValid() {
|
|
||||||
return e.Pos.String() + ":" + e.Msg
|
|
||||||
}
|
|
||||||
|
|
||||||
return e.Msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// ScannerErrorList is largely borrowed from go/scanner.ErrorList
|
|
||||||
type ScannerErrorList []*ScannerError
|
|
||||||
|
|
||||||
func (el *ScannerErrorList) Add(pos Position, msg string) {
|
|
||||||
*el = append(*el, &ScannerError{Pos: pos, Msg: msg})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (el *ScannerErrorList) Reset() { *el = (*el)[0:0] }
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Len() int { return len(el) }
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Swap(i, j int) { el[i], el[j] = el[j], el[i] }
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Less(i, j int) bool {
|
|
||||||
e := &el[i].Pos
|
|
||||||
f := &el[j].Pos
|
|
||||||
|
|
||||||
if e.Column != f.Column {
|
|
||||||
return e.Column < f.Column
|
|
||||||
}
|
|
||||||
|
|
||||||
return el[i].Msg < el[j].Msg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Sort() {
|
|
||||||
sort.Sort(el)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Error() string {
|
|
||||||
switch len(el) {
|
|
||||||
case 0:
|
|
||||||
return "no errors"
|
|
||||||
case 1:
|
|
||||||
return el[0].Error()
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s (and %d more errors)", el[0], len(el)-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Err() error {
|
|
||||||
if len(el) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return el
|
|
||||||
}
|
|
||||||
|
|
||||||
func (el ScannerErrorList) Is(other error) bool {
|
|
||||||
if _, ok := other.(ScannerErrorList); ok {
|
|
||||||
return el.Error() == other.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if v, ok := other.(*ScannerErrorList); ok {
|
|
||||||
return el.Error() == (*v).Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintScannerError(w io.Writer, err error) {
|
|
||||||
if list, ok := err.(ScannerErrorList); ok {
|
|
||||||
for _, e := range list {
|
|
||||||
fmt.Fprintf(w, "%s\n", e)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
fmt.Fprintf(w, "%s\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user