Skip to content

Default subcommand if no one specified#2

Open
everbird wants to merge 4 commits intokadirpekel:masterfrom
everbird:master
Open

Default subcommand if no one specified#2
everbird wants to merge 4 commits intokadirpekel:masterfrom
everbird:master

Conversation

@everbird
Copy link
Copy Markdown

I would like go write some script without subcommand like this:

def run(args):
    print args.foo

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--foo', help='bar', default='baz')
    args = parser.parse_args()
    run(args)

So that I can just simply run ./script.py.

But I found it's hard to achieve this using komandr. Because self.parser.add_subparsers() in the constructor make the subcommand a must. So I did a little hack to make the script work like this:

@komandr.arg('foo', '-f', '--foo', help='baz')
@komandr.command
def run(foo='bar'):
    print foo

if __name__ == __main__:
    komandr.main(default_subcommand='run')

The specified default subcommand would be called unless the first argument was --help or --version or other options which registed to the parser at first.

komandr is cool and I hope this a little hack would help to make it better.

Cheers

@tsileo
Copy link
Copy Markdown

tsileo commented Mar 4, 2013

👍

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

Successfully merging this pull request may close these issues.

2 participants