Sub-command options that are dynamically enabled depending on a parent command option
nchammas opened this issue · 1 comments
I'm writing a tool that launches clusters in a choice of different cloud providers like AWS EC2 and Google Compute Engine. Each cloud provider has options specific to it, but there are also options not specific to any provider.
To illustrate, here's a rough example of what I'm imagining the --help
text for the tool will look like:
Usage:
acme-launcher [--version]
[--help]
[--config <path>]
[--provider <name>]
<command> [<args>]
<command>:
launch <cluster-name>
# Shared options.
[--slaves <num>]
[--install-ganglia|--no-install-ganglia]
[--identity-file <path>]
# EC2-specific options.
[--ec2-key-name <name>]
[--ec2-instance-initiated-shutdown-behavior <type>]
[--ec2-iam-role <name>]
# GCE-specific options.
[--gce-machine-type <type>]
[--gce-disk-auto-delete <type>]
[--gce-target-pool <name>]
destroy <cluster-name>
Basically, acme-launcher
has some top-level options. Then, depending on what --provider
is chosen -- ec2
or gce
-- different options for the launch
sub-command become available. As noted earlier, there are some options like --install-ganglia
that apply regardless of the chosen provider.
I'm thinking the launch
command will map to a Python function which will process some of the top-level options like --provider
. Then, depending on the chosen provider, it will dispatch to the appropriate sub-command function (launch_ec2
, launch_gce
, etc.) to continue the job.
From a command-line UI perspective, I think this is roughly how Docker Machine works. Docker Machine will launch any kind of resource you specify -- a VM, EC2 instance, GCE instance, etc. -- and, depending on the chosen resource provider, accept additional options.
Is this kind of CLI design a good idea for this situation? And, more to the point of this project, can it be done in Click?
I ended up just exposing all the provider-specific options all the time, and just doing run-time checks to ignore or error out when the wrong options are used.