-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to handle multiple sub-commands that use the same argument sets #36
Comments
I could change them from sub-commands to a single command with a --comand flag... |
Hmm, yes. I've pondered how to do bundles of flags backed by a struct, which this would be a good use case for, but I haven't come up with an elegant solution. Any ideas? The other option is to restructure your commands so there is a parent command, eg. "apply": apply = app.Command("apply", "apply changes to an app")
create_cpu = apply.Flag("cpu", "cpu share").Short('c').Required().Float()
create_mem = apply.Flag("mem", "mem share (integer MB)").Short('m').Required().Int()
create_instances = apply.Flag("instances", "instance count").Short('i').Required().Int()
create_app_name = apply.Arg("app_name", "application name").Required().String()
create_app_build = apply.Arg("app_build", "application build").Required().String()
create_app_revision = apply.Arg("app_revision", "application revision name").Required().String()
create_app_extension = apply.Arg("app_extension", "application file extension").Required().String()
create_app_run_as = apply.Arg("app_run_as", "application run as user").Required().String()
create = apply.Command("create", "initial create/deploy of an app")
update = apply.Command("update", "update definition of an app (automatically deploys new definition)") It's not great though :\ |
Actually sorry, that only works for flags anyway :( |
Actually there is one solution that works now: type CreateUpdateOptions struct {
CPU float64
Memory int
Instances int
Name string
Build string
Revision string
Extension string
User string
}
func NewCreateUpdateOptions(cmd *kingpin.CmdClause) *CreateUpdateOptions {
options := &CreateUpdateOptions{}
cmd.Flag("cpu", "cpu share").Short('c').Required().FloatVar(&options.CPU)
cmd.Flag("mem", "mem share (integer MB)").Short('m').Required().IntVar(&options.Memory)
cmd.Flag("instances", "instance count").Short('i').Required().IntVar(&options.Instances)
cmd.Arg("app_name", "application name").Required().StringVar(&options.Name)
cmd.Arg("app_build", "application build").Required().StringVar(&options.Build)
cmd.Arg("app_revision", "application revision name").Required().StringVar(&options.Revision)
cmd.Arg("app_extension", "application file extension").Required().StringVar(&options.Extension)
cmd.Arg("app_run_as", "application run as user").Required().StringVar(&options.User)
return options
}
var (
app = kingpin.New("myapp", "help")
create = app.Command("create", "initial create/deploy of an app")
createOptions = NewCreateUpdateOptions(create)
update = app.Command("update", "update definition of an app (automatically deploys new definition)")
updateOptions = NewCreateUpdateOptions(update)
) |
Thank you! |
Currently, I have 3 sub commands that take the exact same flags and args. What follows is an example of defining two of them. Is there a cleaner way to do this?
The text was updated successfully, but these errors were encountered: