Skip to content

Instantly share code, notes, and snippets.

@aladinoster
Created November 22, 2021 20:16
Show Gist options
  • Save aladinoster/517d6031f5109aa4d2e74edfa282f81d to your computer and use it in GitHub Desktop.
Save aladinoster/517d6031f5109aa4d2e74edfa282f81d to your computer and use it in GitHub Desktop.
Python: Click application + tests
import click
@click.group()
@click.pass_context
@click.argument('CHALLENGE', type=int)
def challenge(ctx, challenge):
ctx.obj = challenge
@challenge.command()
@click.pass_obj
@click.argument('PHASE', type=int)
def phase1(ctx, phase):
"""Phase1 Command"""
click.echo((phase))
@challenge.group(invoke_without_command=True, no_args_is_help=True)
@click.pass_obj
@click.argument('PHASE', type=int)
def phase2(ctx, phase):
"""Phase2 Group"""
click.echo((phase))
if __name__ == "__main__":
commands = (
'--help',
'1 phase1',
'1 phase1 --help',
'1 phase1 2 ',
'1 phase1 2 --help',
'1 phase2',
'1 phase2 --help',
'1 phase2 2 ',
'1 phase2 2 --help',
'--help',
)
import sys, time
time.sleep(1)
print('Click Version: {}'.format(click.__version__))
print('Python Version: {}'.format(sys.version))
for cmd in commands:
try:
time.sleep(0.1)
print('-----------')
print('> ' + cmd)
time.sleep(0.1)
challenge(cmd.split())
except BaseException as exc:
if str(exc) != '0' and \
not isinstance(exc, (click.ClickException, SystemExit)):
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment