|
| 1 | +from shellwrap import color |
| 2 | +from shellwrap import interactive |
| 3 | +import argparse |
| 4 | + |
| 5 | +# ###################################### |
| 6 | +#mark - Setup |
| 7 | + |
| 8 | +def initialize_arguments(): |
| 9 | + """ UPDATE: Setup command line parameters """ |
| 10 | + parser = argparse.ArgumentParser(description="Tester for CMR-7487") |
| 11 | + parser.add_argument('-t', '--task', help='run some task', action='store_true') |
| 12 | + parser.add_argument('-C', '--color-off', help='no color', action='store_true') |
| 13 | + parser.add_argument('-i', '--interactive', help='enter interactive mode', action='store_true') |
| 14 | + parser.add_argument('-u', '--user', help='enter user mode', action='store_true') |
| 15 | + parser.add_argument('-o', '--option', help='no color') |
| 16 | + parser.add_argument('-v', '--verbose', help='verbose mode', action='store_true') |
| 17 | + parser.add_argument('-vv', '--very-verbose', help='verbose mode', action='store_true') |
| 18 | + args = parser.parse_args() |
| 19 | + return args |
| 20 | + |
| 21 | +def initialize_enviornment(args): |
| 22 | + """ UPDATE: Initialize the Environment variable with app settings """ |
| 23 | + environment = {} |
| 24 | + if args.color_off: |
| 25 | + environment["color"] = False |
| 26 | + |
| 27 | + environment["verbose"] = 0 |
| 28 | + if args.verbose: |
| 29 | + environment["verbose"]=1 |
| 30 | + if args.very_verbose: |
| 31 | + environment["verbose"]=3 |
| 32 | + return environment |
| 33 | + |
| 34 | +# ###################################### |
| 35 | +#mark - Script tasks |
| 36 | + |
| 37 | +def do_action(): |
| 38 | + """ This action is global """ |
| 39 | + print ('action') |
| 40 | + |
| 41 | +def some_task(option, env:dict=None): |
| 42 | + """ UPDATE: Do some task ; what is the point of this script? """ |
| 43 | + if env is not None and env["verbose"] > 0: |
| 44 | + color.cprint(color.tcode.blue, "Running a task") |
| 45 | + color.cprint(color.tcode.green, "Results: " + option) |
| 46 | + |
| 47 | +def process_actions(action=None, env:dict=None): |
| 48 | + """ |
| 49 | + UPDATE: Example of how to process user actions |
| 50 | + """ |
| 51 | + if action is None: |
| 52 | + return False |
| 53 | + if 'one' in action: |
| 54 | + print("do command 1") |
| 55 | + if 'two' in action: |
| 56 | + print("do command 2") |
| 57 | + if 'some_task' in action: |
| 58 | + some_task(action, env) |
| 59 | + return True |
| 60 | + |
| 61 | +# ###################################### |
| 62 | +#mark - Main |
| 63 | + |
| 64 | +def main(): |
| 65 | + """ UPDATE: Main body of the script """ |
| 66 | + args: argparse.Namespace = initialize_arguments() |
| 67 | + env = initialize_enviornment(args) |
| 68 | + |
| 69 | + world = globals() |
| 70 | + world['do_action'] = do_action |
| 71 | + world['process_actions'] = process_actions |
| 72 | + |
| 73 | + # inside the command type "process_actions('some_task')" |
| 74 | + interactive.interactive(env, g=globals()) |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments