Creating a config file with specified parameters

I want to create a config file that has parameters that can be specified using - or -- (without an equals sign), as well as the ability to output the file using - or --. Could somebody point me to a generic script to do this? Say for example, I wanted to run MainScript and have it set alpha to 2 and beta to 3 in config.cfg by saying

MainScript --alpha 2 --beta 3 --output config.cfg

and have it also work by saying

MainScript -a 2 -b 3 -o config.cfg

So then if I open config.cfg it has

alpha = 2
beta = 3

The input values for alpha and beta can be any positive integer, and these are just arbitrary names. I want to be able to eventually set default values for alpha and beta, as well.

Thanks!

You might want to investigate getopts to parse the command line, which will do a lot of the "cooking" already (tokenizing by word splitting, for instance). It will, nevertheless, restrict you to a predefined set of options you can pass this way. You can predefine it yourself but then you will have to stick with it (unless you chang the script). So the question is: is that sufficient for your purposes or not?

If it is, you might want to acquaint yourself with getopts first and then ask specific questions if you still need help. If this is not sufficient for your purpose, then i suppose you are on your own and i wouldn't know of any "generic" script you could use. You should perhaps start by deciding what your config files should look like:

  • do you want to allow commentaries like this:
alpha = 1
# this is a line to be ignored
beta = 2
  • do you want to allow inline commentaries like this:
alpha = 1     # this is a comment
beta = 2      # this is another comment

It is easy to dismiss such comments as unnecessary and complicating things but when you have longer complex configs you might want to have some comments explaining inplace why something is what it is and what were your reasons to put it there.

  • will you need "chapters" in these config files, like in the Windows init-files-style:
[part1]
alpha =  1
beta = 2

[part2]
gamma = 3
delta = 4

and perhaps many more questions. You should, before trying to write a script, first define E-X-A-C-T-L-Y how the files you want to produce should look like. Decisions like this will not be easily corrected once you start programming and may well mean that you throw away a script that is already "almost" working and start anew.

I hope this helps.

bakunin