I have a specific set up where I wanted to call foo.py both as its own script, and possibly as part of another process in another python file.
In order to make its own script, i created a separate function to create an argparser and pass it into the main function:
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--customer_id', help='e.g., 0000', type=int, required=True)
parser.add_argument('--data_dir', default=os.environ['HOME'], help="Location to df from query.")
parser.add_argument('--customer_file', help='File containing invoice IDs and category mappings.')
parser.add_argument("--query_csv", help='Location to save query, or output from previous auto query.',
default='raw_ocr_tokens_df.csv')
return parser
if __name__ == "__main__":
arg_parser = parse_args()
main_args = arg_parser.parse_args()
BaseExtractionQuery(main_args).run_query()
I created a new subclass that also performs a query and takes in the exact same arguments and tries to merge the two argument parsers:
# in bar.py
from foo import parse_args
if __name__ == "__main__":
arg_parser = parse_args()
spend_parser = argparse.ArgumentParser('spend_query', conflict_handler='resolve', parents=[arg_parser])
spend_args = spend_parser.parse_args()
SpendExtractionQuery(spend_args).run_query()
But when I try to run 'bar.py --help' not only do i not get the original arg parser help message, but i get the help message of a python file in the next repo in my pythonpath:
>> python3 foo.py --help
usage: foo.py [-h] --customer_id CUSTOMER_ID [--data_dir DATA_DIR]
[--customer_file CUSTOMER_FILE] [--query_csv QUERY_CSV]
optional arguments:
-h, --help show this help message and exit
--customer_id CUSTOMER_ID
e.g., 0000
--data_dir DATA_DIR Location to df from query.
--customer_file CUSTOMER_FILE
File containing invoice IDs and category mappings.
--query_csv QUERY_CSV
Location to save query, or output from previous auto
query.
>> python3 bar.py --help
usage: bar.py [-h] [--var1 VAR1] # but this is really the args for baz.py
[--var2 VAR2]
[--var3 VAR3]
[--var4 VAR4]
[--var5 VAR5]
optional arguments:
-h, --help show this help message and exit
Is there anyway I can force the printing of the help message when passing in a pre-made argparser?