Skip to content

Commit 935d47b

Browse files
author
Ian Campbell
committed
Ignore unknown arguments on the top-level command.
This allows passing argument to plugins, otherwise they are caught by the parse loop, since cobra does not know about each plugin at this stage (to avoid having to always scan for all plugins) this means that e.g. `docker plugin --foo` would accumulate `plugin` as an arg to the `docker` command, then choke on the unknown `--foo`. This allows unknown global args only, unknown arguments on subcommands (e.g. `docker ps --foo`) are still correctly caught. Add an e2e test covering this case. Signed-off-by: Ian Campbell <[email protected]>
1 parent 1337895 commit 935d47b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

cli-plugins/examples/helloworld/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
},
3434
}
3535

36+
var who string
3637
cmd := &cobra.Command{
3738
Use: "helloworld",
3839
Short: "A basic Hello World plugin for tests",
@@ -41,9 +42,11 @@ func main() {
4142
// hook.
4243
PersistentPreRunE: plugin.PersistentPreRunE,
4344
Run: func(cmd *cobra.Command, args []string) {
44-
fmt.Fprintln(dockerCli.Out(), "Hello World!")
45+
fmt.Fprintf(dockerCli.Out(), "Hello %s!\n", who)
4546
},
4647
}
48+
flags := cmd.Flags()
49+
flags.StringVar(&who, "who", "World", "Who are we addressing?")
4750

4851
cmd.AddCommand(goodbye, apiversion)
4952
return cmd

cmd/docker/docker.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {
3232
SilenceUsage: true,
3333
SilenceErrors: true,
3434
TraverseChildren: true,
35+
FParseErrWhitelist: cobra.FParseErrWhitelist{
36+
// UnknownFlags ignores any unknown
37+
// --arguments on the top-level docker command
38+
// only. This is necessary to allow passing
39+
// --arguments to plugins otherwise
40+
// e.g. `docker plugin --foo` is caught here
41+
// in the monolithic CLI and `foo` is reported
42+
// as an unknown argument.
43+
UnknownFlags: true,
44+
},
3545
RunE: func(cmd *cobra.Command, args []string) error {
3646
if len(args) == 0 {
3747
return command.ShowHelp(dockerCli.Err())(cmd, args)

e2e/cli-plugins/run_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ func TestRunGoodSubcommand(t *testing.T) {
144144
})
145145
}
146146

147+
// TestRunGoodArgument ensures correct behaviour when running a valid plugin with an `--argument`.
148+
func TestRunGoodArgument(t *testing.T) {
149+
run, cleanup := prepare(t)
150+
defer cleanup()
151+
152+
res := icmd.RunCmd(run("helloworld", "--who", "Cleveland"))
153+
res.Assert(t, icmd.Expected{
154+
ExitCode: 0,
155+
Out: "Hello Cleveland!",
156+
})
157+
}
158+
147159
// TestHelpGoodSubcommand ensures correct behaviour when invoking help on a
148160
// valid plugin subcommand. A global argument is included to ensure it does not
149161
// interfere.

e2e/cli-plugins/testdata/docker-help-helloworld.golden

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

2-
Usage: docker helloworld COMMAND
2+
Usage: docker helloworld [OPTIONS] COMMAND
33

44
A basic Hello World plugin for tests
55

6+
Options:
7+
--who string Who are we addressing? (default "World")
8+
69
Commands:
710
apiversion Print the API version of the server
811
goodbye Say Goodbye instead of Hello

0 commit comments

Comments
 (0)