Skip to content

Commit 56745f7

Browse files
authored
Merge pull request urfave#2080 from dearchap/issue_1891
2 parents 809e4fb + ec68704 commit 56745f7

19 files changed

+874
-605
lines changed

docs/v3/examples/arguments.md

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
tags:
3+
- v3
4+
search:
5+
boost: 2
6+
---
7+
8+
Instead of the user having to look through all the arguments one by one we can also specify the argument types and destination
9+
fields so that the value can be directly retrieved by the user. Lets go back to the greeter app and specifying the types for arguments
10+
11+
<!-- {
12+
"args" : ["friend","1","bar","2.0"],
13+
"output": "friend-1-bar-2.0"
14+
} -->
15+
```go
16+
package main
17+
18+
import (
19+
"fmt"
20+
"log"
21+
"os"
22+
"context"
23+
24+
"github.com/urfave/cli/v3"
25+
)
26+
27+
func main() {
28+
var name, soap string
29+
var count int64
30+
var version float64
31+
cmd := &cli.Command{
32+
Arguments: []Argument{
33+
&StringArg{
34+
Name: "name",
35+
Min: 1,
36+
Max: 1,
37+
Destination: &name,
38+
},
39+
&IntArg{
40+
Name: "count",
41+
Min: 1,
42+
Max: 1,
43+
Destination: &count,
44+
},
45+
&StringArg{
46+
Name: "soap",
47+
Min: 1,
48+
Max: 1,
49+
Destination: &soap,
50+
},
51+
&FloatArg{
52+
Name: "version",
53+
Min: 1,
54+
Max: 1,
55+
Destination: &version,
56+
},
57+
},
58+
Action: func(ctx context.Context, cmd *cli.Command) error {
59+
fmt.Printf("%s-%d-%s-%f", name, count, soap, version)
60+
return nil
61+
},
62+
}
63+
64+
if err := cmd.Run(context.Background(), os.Args); err != nil {
65+
log.Fatal(err)
66+
}
67+
}
68+
```
69+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
tags:
3+
- v3
4+
search:
5+
boost: 2
6+
---
7+
8+
Lets add some arguments to our greeter app. This allows you to change the behaviour of
9+
the app depending on what argument has been passed. You can lookup arguments by calling
10+
the `Args` function on `cli.Command`, e.g.:
11+
12+
<!-- {
13+
"args" : ["Friend"],
14+
"output": "Hello \"Friend\""
15+
} -->
16+
```go
17+
package main
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"os"
23+
"context"
24+
25+
"github.com/urfave/cli/v3"
26+
)
27+
28+
func main() {
29+
cmd := &cli.Command{
30+
Action: func(ctx context.Context, cmd *cli.Command) error {
31+
fmt.Printf("Hello %q", cmd.Args().Get(0))
32+
return nil
33+
},
34+
}
35+
36+
if err := cmd.Run(context.Background(), os.Args); err != nil {
37+
log.Fatal(err)
38+
}
39+
}
40+
```
41+
42+
Running this program with an argument gives the following output
43+
44+
```sh-session
45+
$ greet friend
46+
Hello "Friend"
47+
```
48+
49+
Any number of arguments can be passed to the greeter app. We can get the number of arguments
50+
and each argument using the `Args`
51+
52+
<!-- {
53+
"args" : ["Friend", "1", "bar", "2.0"],
54+
"output": "Number of args : 4\nHello Friend 1 bar 2.0"
55+
} -->
56+
```go
57+
package main
58+
59+
import (
60+
"fmt"
61+
"log"
62+
"os"
63+
"context"
64+
65+
"github.com/urfave/cli/v3"
66+
)
67+
68+
func main() {
69+
cmd := &cli.Command{
70+
Action: func(ctx context.Context, cmd *cli.Command) error {
71+
fmt.Printf("Number of args : %d\n", cmd.Args().Len())
72+
var out string
73+
for i := 0; i < cmd.Args().Len(); i++ {
74+
out = out + fmt.Sprintf(" %v", cmd.Args().Get(i))
75+
}
76+
fmt.Printf("Hello%v", out)
77+
return nil
78+
},
79+
}
80+
81+
if err := cmd.Run(context.Background(), os.Args); err != nil {
82+
log.Fatal(err)
83+
}
84+
}
85+
```
86+
87+
Running this program with an argument gives the following output
88+
89+
```sh-session
90+
$ greet Friend 1 bar 2.0
91+
Number of args : 4
92+
Hello Friend 1 bar 2.0
93+
```

0 commit comments

Comments
 (0)