简体   繁体   中英

Build a python argparser that takes arguments from a list implemented in the python script code

I am trying to develop a python script that instead of adding fix arguments, it will take the supported arguments from a list that is previously defined and filled with values parsed from an xml file. So that for instance - in the code example below, when passing test2.py -h it will display me the supported variables from that list. Later on each option from the list will have a relevant function to perform. (I've pre-defined the list in the example for simplification)

Is this supported by Python? Any ideas how this can be implemented?

import argparse
list = ['option1', 'option2', 'option3', 'option4', 'option5']
def arg_parser():
    '''setup argument parser'''
    parser = argparse.ArgumentParser()
    parser = argparse.ArgumentParser(description='Display data relevant to the option chosen')
    parser.add_argument(f"{list}", type=str, help='Choose one of the supported options')

    args = parser.parse_args()
    return args

arg_parser()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM