cobra
cobra copied to clipboard
Args in middle of command
I'm trying to create a command that would be invoked like:
app configure dev destination k8s
I would like both dev
and k8s
to be arguments, so the command structure would be:
app configure [environment] destination [name]
There is a global command to configure the the entire environment configuration that I would like it to be a child of:
app configure [environment]
My question is how to accomplish setting up the configure destination command. I have tried having the destination command be a child of the configure command, but that resulted in:
app configure destination [name]
I have tried setting Use: "[environment] destination [name]"
, but that resulted in this is the config help:
$ app configure --help
...
Available Commands:
[environment] Configures a named destination for a given environment
Lastly, I tried making the configure destination command a child of the root command, setting Use: "configure [environment] destination [name]"
, but that resulted in the configure
command appearing twice in the root help message.
Is there a way to accomplish the command structure I am wanting?
Hi @lamebear. I don't believe you can do exactly what you describe. However there is another way by using flags.
You can choose that your command should be configure
and that it needs an environment as an argument, and then for the destination you would create a flag --destination
that would take a string value.
app configure dev --destination k8s
# also valid
app configure --destination k8s dev
Our you can choose the opposite where the environment is specified by a flag instead:
app configure destination k8s --env dev
# also valid
app configure --env dev destination k8s
You can even make two flags
app configure --destination k8s --env dev
or even removing the destination
subcommand:
app configure k8s --env dev
I hope this helps
This issue is being marked as stale due to a long period of inactivity
I would appreciate to permit the support of args in the middle of a command.
Okay, i make it works, but the auto-completion is not working, but they are documented in the Additionnal help topics
Conceptually this only seems to work if you have arguments that must accept an exact number of args, but even then the logic seems confusing to determine errors.
At each new word you have to determine whether or not its an argument or a command and it depends on a specific property of the last read command word; if there are variable arguments allowed it would be ambiguous at best.
So the logic for the parsing gets much more complicated and potentially error prone, only applies if the command expects ExactlyN args (not sure if it can work reliably otherwise?), and all of this to just avoid writing app configure destination dev k8s
(arguments at end) or app configure --env=dev destination k8s
(using flags in the middle of the commands).
Hey @henri9813 👋🏻
How exactly could you make it work? Thanks for the help