Skip to content

Commit

Permalink
added ability to use equals from the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jhughes1153 committed May 16, 2020
1 parent 82bef92 commit bc2f505
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
90 changes: 90 additions & 0 deletions argparse_test.go
Expand Up @@ -194,6 +194,96 @@ func TestFlagSimple2(t *testing.T) {
}
}

func TestLongFlagEqualChar(t *testing.T) {
testArgs := []string{"progname", "--flag1=test1", "--flag2=test2", "--flag3", "test3"}

p := NewParser("", "description")
flag1 := p.String("", "flag1", nil)
flag2 := p.String("", "flag2", nil)
flag3 := p.String("", "flag3", nil)

err := p.Parse(testArgs)
if err != nil {
t.Errorf("Test %s failed with error: %s", t.Name(), err.Error())
return
}

if flag1 == nil {
t.Errorf("Test %s failed with flag1 being nil pointer", t.Name())
return
}

if flag2 == nil {
t.Errorf("Test %s failed with flag2 being nil pointer", t.Name())
return
}

if flag3 == nil {
t.Errorf("Test %s failed with flag3 being nil pointer", t.Name())
return
}

if *flag1 != "test1" {
t.Errorf("Test %s failed with flag1 being false", t.Name())
return
}

if *flag2 != "test2" {
t.Errorf("Test %s failed with flag2 being true", t.Name())
return
}

if *flag3 != "test3" {
t.Errorf("Test %s failed with flag3 being true", t.Name())
return
}
}

func TestShortFlagEqualChar(t *testing.T) {
testArgs := []string{"progname", "-a=test1", "-b=test2", "-c", "test3"}

p := NewParser("", "description")
flag1 := p.String("a", "flag1", nil)
flag2 := p.String("b", "flag2", nil)
flag3 := p.String("c", "flag3", nil)

err := p.Parse(testArgs)
if err != nil {
t.Errorf("Test %s failed with error: %s", t.Name(), err.Error())
return
}

if flag1 == nil {
t.Errorf("Test %s failed with flag1 being nil pointer", t.Name())
return
}

if flag2 == nil {
t.Errorf("Test %s failed with flag2 being nil pointer", t.Name())
return
}

if flag3 == nil {
t.Errorf("Test %s failed with flag3 being nil pointer", t.Name())
return
}

if *flag1 != "test1" {
t.Errorf("Test %s failed with flag1 being false", t.Name())
return
}

if *flag2 != "test2" {
t.Errorf("Test %s failed with flag2 being true", t.Name())
return
}

if *flag3 != "test3" {
t.Errorf("Test %s failed with flag3 being true", t.Name())
return
}
}

func TestFlagMultiShorthandWithParam1(t *testing.T) {
testArgs := []string{"progname", "-ab", "10", "-c", "-de", "11", "--ee", "12"}

Expand Down
10 changes: 10 additions & 0 deletions argument.go
Expand Up @@ -20,6 +20,7 @@ type arg struct {
filePerm os.FileMode // File permissions to set a file
selector *[]string // Used in Selector type to allow to choose only one from list of options
parent *Command // Used to get access to specific Command
eqChar bool // This is used if the command is passed in with an equals char as a seperator
}

// Arg interface provides exporting of arg structure, while exposing it
Expand Down Expand Up @@ -67,6 +68,7 @@ func (o *arg) checkLongName(argument string) int {
func (o *arg) checkShortName(argument string) (int, error) {
// Check for short name only if not empty
if o.sname != "" {

// If argument begins with "-" and next is not "-" then it is a short name
if len(argument) > 1 && strings.HasPrefix(argument, "-") && argument[1] != '-' {
count := strings.Count(argument[1:], o.sname)
Expand Down Expand Up @@ -112,6 +114,11 @@ func (o *arg) reduceLongName(position int, args *[]string) {
if o.lname != "" {
// If argument begins with "--" and next is not "-" then it is a long name
if len(argument) > 2 && strings.HasPrefix(argument, "--") && argument[2] != '-' {
if o.eqChar {
splitInd := strings.LastIndex(argument, "=")
equalArg := []string{argument[:splitInd], argument[splitInd+1:]}
argument = equalArg[0]
}
if argument[2:] == o.lname {
for i := position; i < position+o.size; i++ {
(*args)[i] = ""
Expand All @@ -134,6 +141,9 @@ func (o *arg) reduceShortName(position int, args *[]string) {
if (*args)[position] == "-" {
(*args)[position] = ""
}
if o.eqChar {
(*args)[position] = ""
}
}
// For all other types it must be separate argument
} else {
Expand Down
21 changes: 21 additions & 0 deletions command.go
Expand Up @@ -2,6 +2,7 @@ package argparse

import (
"fmt"
"strings"
)

func (o *Command) help(sname, lname string) {
Expand Down Expand Up @@ -80,6 +81,26 @@ func (o *Command) parseArguments(args *[]string) error {
if arg == "" {
continue
}
if strings.Contains(arg, "=") {
splitInd := strings.LastIndex(arg, "=")
equalArg := []string{arg[:splitInd], arg[splitInd+1:]}
if cnt, err := oarg.check(equalArg[0]); err != nil {
return err
} else if cnt > 0 {
if len(equalArg) < 2 {
return fmt.Errorf("not enough arguments for %s", oarg.name())
}
oarg.eqChar = true
oarg.size = 1
currArg := []string{equalArg[1]}
err := oarg.parse(currArg, cnt)
if err != nil {
return err
}
oarg.reduce(j, args)
continue
}
}
if cnt, err := oarg.check(arg); err != nil {
return err
} else if cnt > 0 {
Expand Down

0 comments on commit bc2f505

Please sign in to comment.