Skip to content

Commit 936824c

Browse files
vsachsverburgsvsachs
authored
Arg can return the result pointer (akamensky#84)
* Arg interface can return result pointer * Arg interface can return result pointer * Add examples & documentation * add docstring Co-authored-by: verburgs <joshua.verburg-sachs@hpe.com> Co-authored-by: vsachs <vsachs@hpe.com>
1 parent 231d348 commit 936824c

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ You can implement sub-commands in your CLI using `parser.NewCommand()` or go eve
137137
Since parser inherits from command, every command supports exactly same options as parser itself,
138138
thus allowing to add arguments specific to that command or more global arguments added on parser itself!
139139

140+
You can also dynamically retrieve argument values:
141+
```
142+
var myInteger *int = parser.Int("i", "integer", ...)
143+
parser.Parse()
144+
fmt.Printf("%d", *parser.GetArgs()[0].GetResult().(*int))
145+
```
146+
140147
#### Basic Option Structure
141148

142149
The `Option` structure is declared at `argparse.go`:

argparse_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,33 @@ func TestFlagSimple1(t *testing.T) {
147147
t.Errorf("Test %s failed with flag2 being true", t.Name())
148148
return
149149
}
150+
151+
if args := p.GetArgs(); args == nil {
152+
t.Errorf("Test %s failed with args empty", t.Name())
153+
} else if len(args) != 3 { // our two and -h
154+
t.Errorf("Test %s failed with wrong len", t.Name())
155+
} else {
156+
got := 0
157+
for _, arg := range args {
158+
switch arg.GetLname() {
159+
case "flag-arg1":
160+
if *arg.GetResult().(*bool) != *flag1 {
161+
t.Errorf("Test %s failed with wrong arg value", t.Name())
162+
}
163+
got += 3
164+
case "flag-arg2":
165+
if *arg.GetResult().(*bool) != *flag2 {
166+
t.Errorf("Test %s failed with wrong arg value", t.Name())
167+
}
168+
got += 5
169+
case "help":
170+
got += 11
171+
}
172+
}
173+
if got != 19 {
174+
t.Errorf("Test %s failed with wrong args found", t.Name())
175+
}
176+
}
150177
}
151178

152179
func TestFlagSimple2(t *testing.T) {
@@ -192,6 +219,7 @@ func TestFlagSimple2(t *testing.T) {
192219
t.Errorf("Test %s failed with flag3 being false", t.Name())
193220
return
194221
}
222+
195223
}
196224

197225
func TestLongFlagEqualChar(t *testing.T) {
@@ -697,6 +725,33 @@ func TestStringSimple1(t *testing.T) {
697725
t.Errorf("Test %s failed. Want: [%s], got: [%s]", t.Name(), "\"\"", *s1)
698726
return
699727
}
728+
729+
if args := p.GetArgs(); args == nil {
730+
t.Errorf("Test %s failed with args empty", t.Name())
731+
} else if len(args) != 3 { // our two + help
732+
t.Errorf("Test %s failed with wrong len", t.Name())
733+
} else {
734+
got := 0
735+
for _, arg := range args {
736+
switch arg.GetLname() {
737+
case "flag-arg1":
738+
if *arg.GetResult().(*string) != *s1 {
739+
t.Errorf("Test %s failed with wrong arg value", t.Name())
740+
}
741+
got += 3
742+
case "flag-arg2":
743+
if *arg.GetResult().(*string) != "" {
744+
t.Errorf("Test %s failed with non-nil result", t.Name())
745+
}
746+
got += 5
747+
case "help":
748+
got += 11
749+
}
750+
}
751+
if got != 19 {
752+
t.Errorf("Test %s failed with wrong args found", t.Name())
753+
}
754+
}
700755
}
701756

702757
func TestStringSimple2(t *testing.T) {

argument.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Arg interface {
2828
GetOpts() *Options
2929
GetSname() string
3030
GetLname() string
31+
GetResult() interface{}
3132
}
3233

3334
func (o arg) GetOpts() *Options {
@@ -42,6 +43,12 @@ func (o arg) GetLname() string {
4243
return o.lname
4344
}
4445

46+
// getResult returns the interface{} to the *(type) containing the argument's result value
47+
// Will contain the empty/default value if argument value was not given
48+
func (o arg) GetResult() interface{} {
49+
return o.result
50+
}
51+
4552
type help struct{}
4653

4754
// checkLongName if long argumet present.

examples/commands/commands.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func main() {
1616

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

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

0 commit comments

Comments
 (0)