Debian Codegraph @

docs/v3/path-and-walk.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 # Path and Walk
9
10 `Command.Path()` returns the list of command names from the root to the
11 current command. `Command.Walk()` visits a command and every subcommand
12 recursively, calling a function on each.
13
14 ## Path
15
16 The `Path()` method returns `[]string` where each element is a `Command.Name`
17 starting from the root. `FullName()` is equivalent to
18 `strings.Join(cmd.Path(), " ")`.
19
20 <!-- {
21 "output": "top mid bottom"
22 } -->
23 ```go
24 package main
25
26 import (
27 "context"
28 "fmt"
29 "strings"
30
31 "github.com/urfave/cli/v3"
32 )
33
34 func main() {
35 subSubCmd := &cli.Command{
36 Name: "bottom",
37 Action: func(ctx context.Context, c *cli.Command) error {
38 fmt.Println(strings.Join(c.Path(), " "))
39 return nil
40 },
41 }
42 subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
43 cmd := &cli.Command{
44 Name: "top",
45 Commands: []*cli.Command{subCmd},
46 }
47
48 cmd.Run(context.Background(), []string{"top", "mid", "bottom"})
49 }
50 ```
51
52 ```sh-session
53 $ go run .
54 top mid bottom
55 ```
56
57 ## Walk
58
59 `Walk()` traverses the command tree depth-first, visiting the command itself
60 first, then each subcommand recursively.
61
62 <!-- {
63 "output": "top\nmid\nbottom"
64 } -->
65 ```go
66 package main
67
68 import (
69 "context"
70 "fmt"
71
72 "github.com/urfave/cli/v3"
73 )
74
75 func main() {
76 subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }}
77 subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
78 cmd := &cli.Command{
79 Name: "top",
80 Commands: []*cli.Command{subCmd},
81 Action: func(ctx context.Context, c *cli.Command) error { return nil },
82 }
83
84 cmd.Walk(func(c *cli.Command) error {
85 fmt.Println(c.Name)
86 return nil
87 })
88 }
89 ```
90
91 ```sh-session
92 $ go run .
93 top
94 mid
95 bottom
96 ```
97
98 ### Short-circuiting
99
100 Return a non-nil error from the walk function to stop traversal early.
101
102 <!-- {
103 "output": "top\nmid"
104 } -->
105 ```go
106 package main
107
108 import (
109 "context"
110 "errors"
111 "fmt"
112
113 "github.com/urfave/cli/v3"
114 )
115
116 func main() {
117 subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }}
118 subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
119 cmd := &cli.Command{
120 Name: "top",
121 Commands: []*cli.Command{subCmd},
122 Action: func(ctx context.Context, c *cli.Command) error { return nil },
123 }
124
125 err := cmd.Walk(func(c *cli.Command) error {
126 fmt.Println(c.Name)
127 if c.Name == "mid" {
128 return errors.New("stop")
129 }
130 return nil
131 })
132 fmt.Println(err)
133 }
134 ```
135
136 ```sh-session
137 $ go run .
138 top
139 mid
140 stop
141 ```
142
143 ## Relation to Lineage
144
145 [`Lineage()`](https://pkg.go.dev/github.com/urfave/cli/v3#Command.Lineage)
146 returns the command and all its ancestors as `[]*Command` (child first).
147 `Path()` is similar but returns only the command names as `[]string` (root
148 first). Use `Lineage()` when you need access to the ancestor `*Command`
149 values; use `Path()` when you only need the names.