Skip to content
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

default 'app.commands' if one is not is supplied #657

Closed
jare19 opened this issue Aug 8, 2017 · 4 comments
Closed

default 'app.commands' if one is not is supplied #657

jare19 opened this issue Aug 8, 2017 · 4 comments
Labels
kind/question someone asking a question

Comments

@jare19
Copy link

jare19 commented Aug 8, 2017

I am having a hard time figuring out how to make one of my app.commands default when a command is not supplied at run time.
for example

...
app.Commands = []cli.command{
                       {
                        Name: "command that I want to be default"
			Usage:  "Does something",
			Action: runthedefaultcommand,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:   "something.something",
					Usage:  "stuff",
					EnvVar: "ENV_VAR",
				},
                          },
                      },
                      {
			Name:   "second optional command that overwrites default",
			Usage:  "Do something else",
			Action: runoptcommand,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "something.somethingelse",
					Usage: "stuff",
				},
                          },
                      },
...
@jszwedko
Copy link
Contributor

jszwedko commented Aug 9, 2017

Hi @jare19,

There isn't currently a way to default a subcommand, but you can set the fields on the App itself to have default behavior. Something like:

app.Action = runthedefaultcommand
app.Flags = []cli.Flag{
				cli.StringFlag{
					Name:   "something.something",
					Usage:  "stuff",
					EnvVar: "ENV_VAR",
				},
                          }

app.Commands = []cli.command{
                     {
			Name:   "second optional command that overwrites default",
			Usage:  "Do something else",
			Action: runoptcommand,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "something.somethingelse",
					Usage: "stuff",
				},
                          },
                      },

Just edited this in Github so it may not compile, but should hopefully point you in the right direction. Let me know if this helps!

@meatballhat meatballhat added the kind/question someone asking a question label Aug 13, 2017
@OneOfOne
Copy link
Contributor

@jszwedko I actually need this too, do you mind if I take a crack at it and send a (v2) PR?

@OneOfOne
Copy link
Contributor

@jare19 here's what I used as a workaround locally:

func DefaultCommand(name string) ActionFunc {
	return func(ctx *Context) error {
		return ctx.App.Command(name).Run(ctx)
	}
}

@coilysiren
Copy link
Member

Given that this is from last year, I think I'm comfortable closing it 🙂 feel free to re-open / open a new issue / comment in support if there's still interest here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question someone asking a question
Projects
None yet
Development

No branches or pull requests

5 participants