rubik/mando

Handle usage without specified command

Opened this issue · 1 comments

When implementing the various commands using @command, they are treated as necessary all the time. This works like
$ program add
or
$ program something-else
but i'd like this
$ program
to be treated as a separate command that does something other than provide the help.

I understand I can achieve this behaviour by modifying sys.argv[], but was wondering if there's an in-built way to specify a function to run when no commands are given.

Thanks for developing this tool by the way, it's excellent and i've found it really useful already!

rubik commented

Hi Cam, thanks for the kind words.

Mando exposes the default Program() instance that it uses to convert functions to commands (as you can see here), or you could create your own Program() instance. In both cases, you have access to the argparse implementation and could change its behavior. After all, Mando is just a wrapper for argparse. However, off the top of my head I don't know exactly how you would implement that.

Checking if sys.argv does not contain anything past the program name is an alternative that feels like a hack but it should certainly be considered in my opinion. After all, it isolates exactly the case you are interested in. You don't have to change sys.argv, you could just check if it contains options after the program name or not before Mando is invoked.