Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit 6101092

Browse files
author
Nathan Potter
committed
Allow passing rawargs to commands
1 parent d3a506b commit 6101092

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type CommandSpec struct {
1919
// Desc is the description of the command.
2020
// The first line is used as an abbreviated description.
2121
Desc string
22+
// RawArgs indicates that flags should not be parsed, and they should be deferred
23+
// to the command.
24+
RawArgs bool
2225

2326
// Hidden indicates that this command should not show up in it's parent's
2427
// subcommand help.

run.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package cli
22

33
import (
4-
"flag"
5-
"os"
4+
"flag"
5+
"os"
66
)
77

88
func appendParent(parent string, add string) string {
9-
if parent == "" {
10-
return add + " "
11-
}
12-
return parent + add + " "
9+
if parent == "" {
10+
return add + " "
11+
}
12+
return parent + add + " "
1313
}
1414

1515
// Run sets up flags, helps, and executes the command with the provided
@@ -20,38 +20,43 @@ func appendParent(parent string, add string) string {
2020
//
2121
// Use RunRoot if this package is managing the entire CLI.
2222
func Run(cmd Command, args []string, parent string) {
23-
fl := flag.NewFlagSet(parent+""+cmd.Spec().Name, flag.ExitOnError)
24-
25-
if fc, ok := cmd.(FlaggedCommand); ok {
26-
fc.RegisterFlags(fl)
27-
}
28-
29-
fl.Usage = func() {
30-
renderHelp(cmd, fl, os.Stderr)
31-
}
32-
_ = fl.Parse(args)
33-
34-
subcommandArg := fl.Arg(0)
35-
36-
// Route to subcommand.
37-
if pc, ok := cmd.(ParentCommand); ok && subcommandArg != "" {
38-
for _, subcommand := range pc.Subcommands() {
39-
if subcommand.Spec().Name != subcommandArg {
40-
continue
41-
}
42-
43-
Run(
44-
subcommand, fl.Args()[1:],
45-
appendParent(parent, cmd.Spec().Name),
46-
)
47-
return
48-
}
49-
}
50-
51-
cmd.Run(fl)
23+
fl := flag.NewFlagSet(parent+""+cmd.Spec().Name, flag.ExitOnError)
24+
25+
if fc, ok := cmd.(FlaggedCommand); ok {
26+
fc.RegisterFlags(fl)
27+
}
28+
29+
fl.Usage = func() {
30+
renderHelp(cmd, fl, os.Stderr)
31+
}
32+
33+
if cmd.Spec().RawArgs {
34+
// Use `--` to return immediately when parsing the flags.
35+
args = append([]string{"--"}, args...)
36+
}
37+
_ = fl.Parse(args)
38+
39+
subcommandArg := fl.Arg(0)
40+
41+
// Route to subcommand.
42+
if pc, ok := cmd.(ParentCommand); ok && subcommandArg != "" {
43+
for _, subcommand := range pc.Subcommands() {
44+
if subcommand.Spec().Name != subcommandArg {
45+
continue
46+
}
47+
48+
Run(
49+
subcommand, fl.Args()[1:],
50+
appendParent(parent, cmd.Spec().Name),
51+
)
52+
return
53+
}
54+
}
55+
56+
cmd.Run(fl)
5257
}
5358

5459
// RunRoot calls Run with the process's arguments.
5560
func RunRoot(cmd Command) {
56-
Run(cmd, os.Args[1:], "")
61+
Run(cmd, os.Args[1:], "")
5762
}

0 commit comments

Comments
 (0)