Last active
December 16, 2015 06:23
-
-
Save mountcedar/94a64d3b144fab9bd287 to your computer and use it in GitHub Desktop.
python clickでサブコマンドを簡単に実装する ref: http://qiita.com/mountcedar/items/45b06ce58b1d2ae001f3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pip install click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import click | |
@click.group(invoke_without_command=True) | |
@click.pass_context | |
def cli(ctx): | |
if ctx.invoked_subcommand is None: | |
print ctx.get_help() | |
else: | |
print('gonna invoke %s' % ctx.invoked_subcommand) | |
@cli.command(help='description 1') | |
@click.argument('target', required=False) | |
def subcommand1(target): | |
print "sub command 1" | |
@cli.command(help='description 2') | |
@click.argument('target', required=False) | |
def subcommand2(target): | |
print "sub command 2" | |
if __name__ == '__main__': cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage: sample.py [OPTIONS] COMMAND [ARGS]... | |
Options: | |
--help Show this message and exit. | |
Commands: | |
subcommand1 description 1 | |
subcommand2 description 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
class AliasedGroup(click.Group): | |
def get_command(self, ctx, cmd_name): | |
rv = click.Group.get_command(self, ctx, cmd_name) | |
if rv is not None: | |
return rv | |
if cmd_name == 'shortcut': | |
return click.Group.get_command(self, ctx, 'subcommand1') | |
return None | |
@click.group(cls=AliasedGroup, invoke_without_command=True) | |
@click.pass_context | |
def cli(ctx): | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ python ./sample.py shortcut | |
gonna invoke shortcut | |
sub command 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@click.group(invoke_without_command=True) | |
@click.pass_context | |
def handle(ctx): | |
if ctx.invoked_subcommand is None: | |
print ctx.get_help() | |
else: | |
print('gonna invoke %s' % ctx.invoked_subcommand) | |
@click.command() | |
def hoge(): | |
pass | |
handle.add_command(hoge) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment