Source code for arg_parse_utils

 1import argparse
 2
 3"""This file is where we can keep scripts to interpret arguments that are going to be used in multiple command-line scripts.
 4This is intended to keep imports clear and allow easy reuse of this code."""
 5
[docs] 6def add_bool_argument(arg_parser:argparse.ArgumentParser, 7 feature_name:str, 8 flag_names:{'enable':[str],'disable':[str]} = {'enable':[],'disable':[]}, 9 default:bool=False, 10 is_required:bool=False): 11 "This allows you to create mutually exclusive boolean flags for the feature, and even specify the names of the flags you desire to switch it with, but you don't need to." 12 13 group = arg_parser.add_mutually_exclusive_group(required=is_required) 14 15 ## create all flags enabling it 16 if len(flag_names['enable']) == 0: 17 group.add_argument('--'+feature_name,dest=feature_name,action="store_true") 18 else: 19 for flag in flag_names['enable']: 20 group.add_argument(flag,dest=feature_name,action="store_true") 21 22 ## create all flags disabling it 23 if len(flag_names['disable']) == 0: 24 group.add_argument('--no-'+feature_name,dest=feature_name, action="store_false") 25 else: 26 for flag in flag_names['disable']: 27 group.add_argument(flag,dest=feature_name, action='store_false') 28 29 arg_parser.set_defaults(**{feature_name:default})