Last active
September 28, 2019 01:38
-
-
Save raytroop/91e59e4364380bd717bc6d4fe98d9b34 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
This page intentionally left blank |
This file contains hidden or 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.command() | |
@click.argument('subcommand') | |
@click.argument('args1') | |
def train(subcommand, args1): | |
print('---TRAIN---') | |
print(subcommand, args1) | |
@click.command() | |
@click.argument('subcommand') | |
@click.argument('args1') | |
def infer(subcommand, args1): | |
print('---INFER---') | |
print(subcommand, args1) | |
@click.group() | |
def cli(): | |
pass | |
cli.add_command(train) | |
cli.add_command(infer) | |
if __name__ == '__main__': | |
cli() | |
# $ python main.py train warmstart 20k | |
# ---TRAIN--- | |
# warmstart 20k | |
# $ python main.py infer loadmodel 20k | |
# ---INFER--- | |
# loadmodel 20k |
This file contains hidden or 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 argparse | |
import click | |
def manual_parser(args): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--iteration', '-i', type=int, help='the iteration to train or evaluate') | |
return parser.parse_args(args) | |
@click.command(help='TRAIN', context_settings=dict(ignore_unknown_options=True, allow_extra_args=True)) | |
@click.argument('exp_dir') | |
@click.pass_context # pass the extra args of unkown to `cxt.args` | |
def train(cxt, exp_dir): | |
print('--TRAIN--') | |
print(cxt.args) | |
print(exp_dir) | |
args = manual_parser(cxt.args) | |
print(args) | |
@click.command(help='INFER', context_settings=dict(ignore_unknown_options=True, allow_extra_args=True)) | |
@click.argument('exp_dir') | |
@click.pass_context | |
def infer(cxt, exp_dir): | |
print('--INFER--') | |
print(cxt.args) | |
print(exp_dir) | |
args = manual_parser(cxt.args) | |
print(args) | |
@click.command(cls=click.Group, help='main entry point') | |
def cli(): | |
pass | |
cli.add_command(train) | |
cli.add_command(infer) | |
if __name__ == '__main__': | |
cli() | |
""" | |
$ python main.py train EX01 -i 180 | |
--TRAIN-- | |
['-i', '180'] | |
EX01 | |
Namespace(iteration=180) | |
""" | |
""" | |
python main.py infer EX01 -i 180 | |
--INFER-- | |
['-i', '180'] | |
EX01 | |
Namespace(iteration=180) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment