Passing configs from `.txt` file into argparse? (Similar to youtube-dl's `--config-location`)

Viewed 53

I have a program with ~20 configs, which I would like to be accessed from the command-line via argparse, either by something like --config_1 'This is the first config' or --config-location <path_to_config_file>, where the path to the config file would be a .txt of form:

--config_1 'This is the first config'
--config_2 '2nd Config'
--config_3 3
...

Currently, my code looks something like:

parser = argparse.ArgumentParser()
parser.add_argument('--config_1')
args = vars(parser.parse_args())

This approach was inspired by youtube-dl's --config-location. If there's a better way to accomplish what I'm trying to do, suggestions are also appreciated and welcome. Thank you for reading!

2 Answers

argparse supports something similar out of the box.

parser = ArgumentParser(fromfile_prefix_chars='@')
parser.parse_args()

Now any argument starting with @ will be treated as a filename to be read, which each line being treated as a separate argument. This would require your file to look like

--config_1
This is the first config
--config_2
2nd Config
--config_3
3
...

instead.

What youtube-dl does is simply open the file, read its contents, and split it on whitespace similar to how the shell would.

With that many command line options, a better approach would be to make a configuration file. There are two ways of doing this:

The first is to create a separate Python file. Create a file called config.py:

config_1 = 'This is the first config'
config_2 = '2nd config'
config_3 = 3

Then, in another file (say, prog.py):

import config

print(config.config_1)
print(config.config_2)
print(config.config_3)

This will output:

This is the first config
2nd config
3

The second way is an improvement upon the above approach. You can use ConfigParser. For example, with an example config.ini:

[DEFAULT]
config_1 = 'This is the first config'
config_2 = '2nd config'
config_3 = 3

and an example prog.py:

import configparser

config = configparser.ConfigParser()
config.read('config.ini')
print(config['DEFAULT']['config_1'])
print(config['DEFAULT']['config_2'])
print(config['DEFAULT']['config_3'])

We can get:

This is the first config
2nd config
3

See here for a discussion between these two approaches.

Related