Skip to content

Commit

Permalink
Utilize two-stage parsing with positionals subsequent to flags
Browse files Browse the repository at this point in the history
  • Loading branch information
vsachs committed Aug 3, 2022
1 parent 1e2e679 commit c1c45f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
10 changes: 5 additions & 5 deletions argparse_test.go
Expand Up @@ -3087,10 +3087,10 @@ func TestCommandSubcommandPositionals(t *testing.T) {
testArgs7 := []string{"pos", "subcommand2", "-i=1", "abc"}
testArgs8 := []string{"pos", "subcommand2", "--integer=1", "abc"}
testArgs9 := []string{"pos", "subcommand3", "second"}
testArgs10 := []string{"pos", "subcommand2", "-i", "1", "abc"}
// Error cases
errArgs1 := []string{"pos", "subcommand2", "-i", "1"}
errArgs2 := []string{"pos", "subcommand2", "-i", "1", "abc"}
errArgs3 := []string{"pos", "subcommand3", "abc"}
errArgs2 := []string{"pos", "subcommand3", "abc"}

newParser := func() *Parser {
parser := NewParser("pos", "")
Expand Down Expand Up @@ -3131,14 +3131,14 @@ func TestCommandSubcommandPositionals(t *testing.T) {
if err := newParser().Parse(testArgs9); err != nil {
t.Error(err.Error())
}
if err := newParser().Parse(testArgs10); err != nil {
t.Error(err.Error())
}

if err := newParser().Parse(errArgs1); err == nil {
t.Error("Expected error")
}
if err := newParser().Parse(errArgs2); err == nil {
t.Error("Expected error")
}
if err := newParser().Parse(errArgs3); err == nil {
t.Error("Expected error")
}
}
40 changes: 32 additions & 8 deletions command.go
Expand Up @@ -91,22 +91,46 @@ func (o *Command) parseSubCommands(args *[]string) error {
return nil
}

// All flags must have been parsed and reduced prior to calling this
// This will cause positionals to consume any remainig values,
// whether they have dashes or equals signs or whatever.
func (o *Command) parsePositionals(inputArgs *[]string) error {
for _, oarg := range o.args {
// Two-stage parsing, this is the second stage
if !oarg.GetPositional() || oarg.parsed {
continue
}
for j := 0; j < len(*inputArgs); j++ {
arg := (*inputArgs)[j]
if arg == "" {
continue
}
if err := oarg.parsePositional(arg); err != nil {
return err
}
oarg.reduce(j, inputArgs)
break // Positionals can only occur once
}
// Positionals are implicitly required, if unsatisfied error out
if oarg.opts.Required && !oarg.parsed {
return fmt.Errorf("[%s] is required", oarg.name())
}
}
return nil
}

//parseArguments - Parses arguments
func (o *Command) parseArguments(inputArgs *[]string) error {
// Iterate over the args
for _, oarg := range o.args {
if oarg.GetPositional() { // Two-stage parsing, this is the first stage
continue
}
for j := 0; j < len(*inputArgs); j++ {
arg := (*inputArgs)[j]
if arg == "" {
continue
}
if !strings.HasPrefix(arg, "-") && oarg.GetPositional() {
if err := oarg.parsePositional(arg); err != nil {
return err
}
oarg.reduce(j, inputArgs)
break // Positionals can only occur once
}
if strings.Contains(arg, "=") {
splitInd := strings.LastIndex(arg, "=")
equalArg := []string{arg[:splitInd], arg[splitInd+1:]}
Expand Down Expand Up @@ -153,7 +177,7 @@ func (o *Command) parseArguments(inputArgs *[]string) error {
}
}
}
return nil
return o.parsePositionals(inputArgs)
}

// Will parse provided list of arguments
Expand Down

0 comments on commit c1c45f5

Please sign in to comment.