Debian Codegraph @

docs/v3/examples/subcommands/basics.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 Subcommands can be defined for a more git-like command line app.
9
10 <!-- {
11 "args": ["template", "add"],
12 "output": "new task template: .+"
13 } -->
14 ```go
15 package main
16
17 import (
18 "fmt"
19 "log"
20 "os"
21 "context"
22
23 "github.com/urfave/cli/v3"
24 )
25
26 func main() {
27 cmd := &cli.Command{
28 Commands: []*cli.Command{
29 {
30 Name: "add",
31 Aliases: []string{"a"},
32 Usage: "add a task to the list",
33 Action: func(ctx context.Context, cmd *cli.Command) error {
34 fmt.Println("added task: ", cmd.Args().First())
35 return nil
36 },
37 },
38 {
39 Name: "complete",
40 Aliases: []string{"c"},
41 Usage: "complete a task on the list",
42 Action: func(ctx context.Context, cmd *cli.Command) error {
43 fmt.Println("completed task: ", cmd.Args().First())
44 return nil
45 },
46 },
47 {
48 Name: "template",
49 Aliases: []string{"t"},
50 Usage: "options for task templates",
51 Commands: []*cli.Command{
52 {
53 Name: "add",
54 Usage: "add a new template",
55 Action: func(ctx context.Context, cmd *cli.Command) error {
56 fmt.Println("new task template: ", cmd.Args().First())
57 return nil
58 },
59 },
60 {
61 Name: "remove",
62 Usage: "remove an existing template",
63 Action: func(ctx context.Context, cmd *cli.Command) error {
64 fmt.Println("removed task template: ", cmd.Args().First())
65 return nil
66 },
67 },
68 },
69 },
70 },
71 }
72
73 if err := cmd.Run(context.Background(), os.Args); err != nil {
74 log.Fatal(err)
75 }
76 }
77 ```