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 <ijc@docker.com>
This commit is contained in:
Ian Campbell
2019-01-17 13:38:38 +00:00
parent 1337895751
commit 935d47bbe9
4 changed files with 30 additions and 2 deletions

View File

@ -144,6 +144,18 @@ func TestRunGoodSubcommand(t *testing.T) {
})
}
// TestRunGoodArgument ensures correct behaviour when running a valid plugin with an `--argument`.
func TestRunGoodArgument(t *testing.T) {
run, cleanup := prepare(t)
defer cleanup()
res := icmd.RunCmd(run("helloworld", "--who", "Cleveland"))
res.Assert(t, icmd.Expected{
ExitCode: 0,
Out: "Hello Cleveland!",
})
}
// TestHelpGoodSubcommand ensures correct behaviour when invoking help on a
// valid plugin subcommand. A global argument is included to ensure it does not
// interfere.

View File

@ -1,8 +1,11 @@
Usage: docker helloworld COMMAND
Usage: docker helloworld [OPTIONS] COMMAND
A basic Hello World plugin for tests
Options:
--who string Who are we addressing? (default "World")
Commands:
apiversion Print the API version of the server
goodbye Say Goodbye instead of Hello