Skip to content

Commit

Permalink
Merge ed0730a into 231d348
Browse files Browse the repository at this point in the history
  • Loading branch information
vsachs authored May 28, 2021
2 parents 231d348 + ed0730a commit 302a271
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ You can implement sub-commands in your CLI using `parser.NewCommand()` or go eve
Since parser inherits from command, every command supports exactly same options as parser itself,
thus allowing to add arguments specific to that command or more global arguments added on parser itself!

You can also dynamically retrieve argument values:
```
var myInteger *int = parser.Int("i", "integer", ...)
parser.Parse()
fmt.Printf("%d", *parser.GetArgs()[0].GetResult().(*int))
```

#### Basic Option Structure

The `Option` structure is declared at `argparse.go`:
Expand Down
55 changes: 55 additions & 0 deletions argparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ func TestFlagSimple1(t *testing.T) {
t.Errorf("Test %s failed with flag2 being true", t.Name())
return
}

if args := p.GetArgs(); args == nil {
t.Errorf("Test %s failed with args empty", t.Name())
} else if len(args) != 3 { // our two and -h
t.Errorf("Test %s failed with wrong len", t.Name())
} else {
got := 0
for _, arg := range args {
switch arg.GetLname() {
case "flag-arg1":
if *arg.GetResult().(*bool) != *flag1 {
t.Errorf("Test %s failed with wrong arg value", t.Name())
}
got += 3
case "flag-arg2":
if *arg.GetResult().(*bool) != *flag2 {
t.Errorf("Test %s failed with wrong arg value", t.Name())
}
got += 5
case "help":
got += 11
}
}
if got != 19 {
t.Errorf("Test %s failed with wrong args found", t.Name())
}
}
}

func TestFlagSimple2(t *testing.T) {
Expand Down Expand Up @@ -192,6 +219,7 @@ func TestFlagSimple2(t *testing.T) {
t.Errorf("Test %s failed with flag3 being false", t.Name())
return
}

}

func TestLongFlagEqualChar(t *testing.T) {
Expand Down Expand Up @@ -697,6 +725,33 @@ func TestStringSimple1(t *testing.T) {
t.Errorf("Test %s failed. Want: [%s], got: [%s]", t.Name(), "\"\"", *s1)
return
}

if args := p.GetArgs(); args == nil {
t.Errorf("Test %s failed with args empty", t.Name())
} else if len(args) != 3 { // our two + help
t.Errorf("Test %s failed with wrong len", t.Name())
} else {
got := 0
for _, arg := range args {
switch arg.GetLname() {
case "flag-arg1":
if *arg.GetResult().(*string) != *s1 {
t.Errorf("Test %s failed with wrong arg value", t.Name())
}
got += 3
case "flag-arg2":
if *arg.GetResult().(*string) != "" {
t.Errorf("Test %s failed with non-nil result", t.Name())
}
got += 5
case "help":
got += 11
}
}
if got != 19 {
t.Errorf("Test %s failed with wrong args found", t.Name())
}
}
}

func TestStringSimple2(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Arg interface {
GetOpts() *Options
GetSname() string
GetLname() string
GetResult() interface{}
}

func (o arg) GetOpts() *Options {
Expand All @@ -42,6 +43,12 @@ func (o arg) GetLname() string {
return o.lname
}

// getResult returns the interface{} to the *(type) containing the argument's result value
// Will contain the empty/default value if argument value was not given
func (o arg) GetResult() interface{} {
return o.result
}

type help struct{}

// checkLongName if long argumet present.
Expand Down
7 changes: 7 additions & 0 deletions examples/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {

// Add top level commands `stop`
stopCmd := parser.NewCommand("stop", "Will stop a process")
stopCmd.Int("-t", "--time", nil)

// Parse command line arguments and in case of any error print error and help information
err := parser.Parse(os.Args)
Expand All @@ -30,6 +31,12 @@ func main() {
fmt.Println("Started process")
} else if stopCmd.Happened() { // Check if `stop` command was given
// Stopping a process
for _, arg := range stopCmd.GetArgs() {
switch val := arg.GetResult().(type) {
case int:
fmt.Printf("Arg: %s = %d\n", arg.GetLname(), val)
}
}
fmt.Println("Stopped process")
} else {
// In fact we will never hit this one
Expand Down

0 comments on commit 302a271

Please sign in to comment.