-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add Command.Walk() and Command.Path() convenience methods #2353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad20753
feat: add Command.Walk() and Command.Path() convenience methods
dearchap 20a6331
Add docs
dearchap d274d31
Update command.go
dearchap 3b59ce9
fix: reword Walk godoc per review feedback
dearchap d6b165b
fix: update godoc for FullName() and Walk() per review feedback
dearchap e204611
chore: update godoc-current.txt and v3 snapshot
dearchap f9f0d03
address review feedback: use Walk internally, move near Lineage, add …
dearchap c079d6c
Merge branch 'main' into issue_2323
dearchap 0b9c2ae
fix: suppress errcheck lint for Walk call in setupCommandGraph
dearchap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,11 +165,13 @@ func (cmd *Command) setupDefaults(osArgs []string) { | |
| func (cmd *Command) setupCommandGraph() { | ||
| tracef("setting up command graph (cmd=%[1]q)", cmd.Name) | ||
|
|
||
| for _, subCmd := range cmd.Commands { | ||
| subCmd.parent = cmd | ||
| subCmd.setupSubcommand() | ||
| subCmd.setupCommandGraph() | ||
| } | ||
| _ = cmd.Walk(func(sub *Command) error { | ||
| for _, subCmd := range sub.Commands { | ||
| subCmd.parent = sub | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange that parent is injected only here and not when |
||
| subCmd.setupSubcommand() | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func (cmd *Command) setupSubcommand() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| --- | ||
| tags: | ||
| - v3 | ||
| search: | ||
| boost: 2 | ||
| --- | ||
|
|
||
| # Path and Walk | ||
|
|
||
| `Command.Path()` returns the list of command names from the root to the | ||
| current command. `Command.Walk()` visits a command and every subcommand | ||
| recursively, calling a function on each. | ||
|
|
||
| ## Path | ||
|
|
||
| The `Path()` method returns `[]string` where each element is a `Command.Name` | ||
| starting from the root. `FullName()` is equivalent to | ||
| `strings.Join(cmd.Path(), " ")`. | ||
|
|
||
| <!-- { | ||
| "output": "top mid bottom" | ||
| } --> | ||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| func main() { | ||
| subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }} | ||
| subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }} | ||
| cmd := &cli.Command{ | ||
| Name: "top", | ||
| Subcommands: []*cli.Command{subCmd}, | ||
| Action: func(ctx context.Context, c *cli.Command) error { | ||
| fmt.Println(strings.Join(c.Path(), " ")) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Run(context.Background(), []string{"top", "mid", "bottom"}) | ||
| } | ||
| ``` | ||
|
|
||
| ```sh-session | ||
| $ go run . | ||
| top mid bottom | ||
| ``` | ||
|
|
||
| ## Walk | ||
|
|
||
| `Walk()` traverses the command tree depth-first, visiting the command itself | ||
| first, then each subcommand recursively. | ||
|
|
||
| <!-- { | ||
| "output": "top\nmid\nbottom" | ||
| } --> | ||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| func main() { | ||
| subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }} | ||
| subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }} | ||
| cmd := &cli.Command{ | ||
| Name: "top", | ||
| Subcommands: []*cli.Command{subCmd}, | ||
| Action: func(ctx context.Context, c *cli.Command) error { return nil }, | ||
| } | ||
|
|
||
| cmd.Walk(func(c *cli.Command) error { | ||
| fmt.Println(c.Name) | ||
| return nil | ||
| }) | ||
| } | ||
| ``` | ||
|
|
||
| ```sh-session | ||
| $ go run . | ||
| top | ||
| mid | ||
| bottom | ||
| ``` | ||
|
|
||
| ### Short-circuiting | ||
|
|
||
| Return a non-nil error from the walk function to stop traversal early. | ||
|
|
||
| <!-- { | ||
| "output": "top\nmid" | ||
| } --> | ||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| func main() { | ||
| subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }} | ||
| subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }} | ||
| cmd := &cli.Command{ | ||
| Name: "top", | ||
| Subcommands: []*cli.Command{subCmd}, | ||
| Action: func(ctx context.Context, c *cli.Command) error { return nil }, | ||
| } | ||
|
|
||
| err := cmd.Walk(func(c *cli.Command) error { | ||
| fmt.Println(c.Name) | ||
| if c.Name == "mid" { | ||
| return errors.New("stop") | ||
| } | ||
| return nil | ||
| }) | ||
| fmt.Println(err) | ||
| } | ||
| ``` | ||
|
|
||
| ```sh-session | ||
| $ go run . | ||
| top | ||
| mid | ||
| stop | ||
| ``` | ||
|
|
||
| ## Relation to Lineage | ||
|
|
||
| [`Lineage()`](https://pkg.go.dev/github.com/urfave/cli/v3#Command.Lineage) | ||
| returns the command and all its ancestors as `[]*Command` (child first). | ||
| `Path()` is similar but returns only the command names as `[]string` (root | ||
| first). Use `Lineage()` when you need access to the ancestor `*Command` | ||
| values; use `Path()` when you only need the names. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.