|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +def dump_options(options, pos_args=None, exclude=None): |
| 7 | + options_dict = vars(options) |
| 8 | + with open('rerunnable_cla.txt', 'w') as file: |
| 9 | + for key, value in options_dict.items(): |
| 10 | + if ((exclude is None or key not in exclude) and |
| 11 | + (pos_args is None or key not in pos_args)): |
| 12 | + if isinstance(value, bool): |
| 13 | + if value: |
| 14 | + print(f'--{key}', file=file) |
| 15 | + else: |
| 16 | + print(f'--{key}\n{value}', file=file) |
| 17 | + if pos_args is not None: |
| 18 | + for key in pos_args: |
| 19 | + print(options_dict[key], file=file) |
| 20 | + |
| 21 | + |
| 22 | +if __name__ == '__main__': |
| 23 | + arg_parser = argparse.ArgumentParser( |
| 24 | + fromfile_prefix_chars='@', |
| 25 | + description='application that saves its command line options and can be rerun' |
| 26 | + ) |
| 27 | + arg_parser.add_argument('--number', type=int, default=1, |
| 28 | + help='number of messages to write') |
| 29 | + arg_parser.add_argument('--type', choices=('hello', 'bye'), default='hello', |
| 30 | + help='message type') |
| 31 | + arg_parser.add_argument('--verbose', action='store_true', |
| 32 | + help='verbose output') |
| 33 | + arg_parser.add_argument('name', help='person to message') |
| 34 | + options = arg_parser.parse_args() |
| 35 | + dump_options(options, pos_args=('name', )) |
| 36 | + if options.verbose: |
| 37 | + print(f'printing {options.number} messages') |
| 38 | + for _ in range(options.number): |
| 39 | + print(f'{options.type} {options.name}') |
0 commit comments