argtable / argtable3

A single-file, ANSI C, command-line parsing library that parses GNU-style command-line options.
http://www.argtable.org
Other
372 stars 65 forks source link

simple config file parsing? #42

Closed paul-chambers closed 4 years ago

paul-chambers commented 4 years ago

I often find myself often wanting the same set of options defined in a config file as I do on the command line. In other words, use a config file as a stored set of options, which get parsed and override the default settings of the executable, and then can be 'fine-tuned'/overridden by options if provided on the command line.

I guess I could parse the config file into a fake argc/argv array, and feed that to argtable3, but that feels like I'm throwing away information, and potentially messing up the error reporting.

Reasonable enhancement request, or outside the scope of argtablle3?

tomghuang commented 4 years ago

@paul-chambers There are several issues to combine both config file parsing and command line parsing in a single library. First, it will make the library too large for people who only want to use the config file parsing API or the command line parsing API of the library. Second, there are already too many config file formats, and creating a new one seems doesn't help the community too much. Third, if we want to build in any existing config file parser, people who use other formats won't be comfortable.

I think config file parsing and command line parsing are better separated, because you'll have freedom to choose any config file parsing library you like. Besides, it should not be too hard to achieve what you want.

For example, you can load the config file and put all the keys and values in an internal dictionary. At the same time, you can override any key by specifying the new value in command-line option:

$ myprog --config myconfig.json --set key=new_value

The new value will override the old value in the internal dictionary. That's how I implement my applications. I've combined the power of argtable3 and many config file parsers, and I'm glad that I can choose whatever config parser I need for a specific project.

tomghuang commented 2 years ago

@paul-chambers I never expect that some day I will have an interview with some one asking questions in Argtable3. Now I know how small the world is, and I am glad that I always try my best to answer questions in GitHub. :smile: