Debian Codegraph @

docs/v1/examples/subcommands.md

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