简体   繁体   中英

how to read argparser values from a config file in python?

I have a lot of arguments for my script. And along with the argparser, I want users to also have the option to specify those arguments via a config file.

parser.add_argument('-a','--config_file_name' ...required=False)
parser.add_argument('-b' ...required=True)
parser.add_argument('-c' ...required=False)
....

At this point I just need the logic to implement the following:

  • Either the users can type in all the arguments in the command line or

  • They can type in the first argument, specify the file name and the code fills in/overwrites all the remaining optional arguments from the config file.

How can this be achieved?

I don't think this is up to argparse to handle. Argparse simple needs to check if the argument for the config file is there and pass it on to your program.

You need to handle this in your program, which would mean doing something like:

...
arguments=parser.parse_args()
if len(arguments.config_file_name):
    f=fopen(arguments.config_file_name,'rb')
    conf_settings = f.read()
    for line in conf_settings:
        #parse your config format here.

this way, if the config_file_name is set, you will overwrite any possible given arguments, and if not, the program will be executed with the arguments specified by the user.

for example:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("a")
args = parser.parse_args()
if args.a:
    #We're getting config from config file here...
else:
    #We're getting config from the command line arguments
#Now we can call all functions with correct configuration applied.

One way to solve this problem is with xargs command line utility. This wouldn't give you exactly the command line interface that you describe, but would achieve the same effect.

xargs will run a command with arguments that it has read from standard input. If you pipe your arguments from the config file into xargs then your python program will be called with the arguments stored in the file. For example:

config.txt

-a -b --input "/some/file/path" --output "/another"

You could then use these arguments to your program like so

xargs ./my-prog.py --optional-arg < config.txt

because of the way that argparse works, it will take the value of the last argument if duplicates are found. Thus, if you have duplicate arguments, the ones from the config file will be chosen.

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