|
| 1 | +# Verbose Command Processor # |
| 2 | +import typer |
| 3 | +import logging |
| 4 | + |
| 5 | + |
| 6 | +# Verbose Options # |
| 7 | +DebugOption = typer.Option( |
| 8 | + False, '--debug', |
| 9 | + help = 'Set verbosity level to DEBUG', |
| 10 | + is_flag = True |
| 11 | +) |
| 12 | +VerboseOption = typer.Option( |
| 13 | + False, '--verbose', '-v', |
| 14 | + help = 'Set verbosity level to INFO', |
| 15 | + is_flag = True |
| 16 | +) |
| 17 | +QuietOption = typer.Option( |
| 18 | + False, '--quiet', '-q', |
| 19 | + help = 'Set verbosity level to ERROR', |
| 20 | + is_flag = True |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +# Helper functions # |
| 25 | + |
| 26 | + |
| 27 | +# Processors # |
| 28 | +def verbose_args_processor(debug: bool, verbose: bool, quiet: bool): |
| 29 | + """Handles setting and raising exceptions for verbose""" |
| 30 | + if verbose and quiet: |
| 31 | + raise typer.BadParameter('--quiet cannot be used with --verbose') |
| 32 | + |
| 33 | + if verbose and debug: |
| 34 | + raise typer.BadParameter('--debug cannot be used with --verbose') |
| 35 | + |
| 36 | + logging.getLogger('base').setLevel(( |
| 37 | + (debug and logging.DEBUG) or |
| 38 | + (verbose and logging.INFO) or |
| 39 | + logging.ERROR |
| 40 | + )) |
| 41 | + |
| 42 | +def kwargs_processor(ctx: typer.Context) -> dict: |
| 43 | + """Processes overflow arguments into kwargs""" |
| 44 | + kwargs = {} |
| 45 | + length = len(ctx.args) // 2 |
| 46 | + |
| 47 | + for i in range(length): |
| 48 | + kv_pair = ctx.args[i*2:i*2+2] |
| 49 | + kwargs[kv_pair[0]] = kv_pair[1] |
| 50 | + |
| 51 | + return kwargs |
| 52 | + |
| 53 | + |
0 commit comments