Default subcommand

I have an application with a number of subcommand's, for example: add, delete and list.
If no subcommand has been specified, I would like it to call the list subcommand.

So instead of

$ application list

I would like to run just

$ application

And it should behave as if application list was entered.

Is it possible for a subcommand to be the default? Alternatively is there a possibility to call a subcommand from the main run() method?

The non-attractive alternative is to migrate the list logic into the top level struct but that also makes the list options visible to the add and delete where some of the options don't make sense. Additionally - in my case - I have to make one of the options optional where they are mandatory in some subcommands and not mandatory in others. By having all the logic in subcommands I can type the options appropriately.

When you setup subcommands (here’s how), there’s a defaultSubcommand: parameter for exactly this purpose. It defaults to nil so you probably miss it.

Edit:

It’s called defaultSubCommand:, not default:.

1 Like

Thanks. Appreciate the swift reply.

Terms of Service

Privacy Policy

Cookie Policy