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

Handle usage without specified command #53

Open
kamurani opened this issue Aug 13, 2021 · 1 comment
Open

Handle usage without specified command #53

kamurani opened this issue Aug 13, 2021 · 1 comment

Comments

@kamurani
Copy link

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
Copy link
Owner

rubik commented Sep 3, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants