views:

27

answers:

1

I use config module to store variables global to all modules. Is it a good place to parse the script arguments? (Note: config module is my own module, it just contains a bunch of global variables.)

----- config.py -----
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-t", "--test", action = "store_true", dest = "test")
#add other options here
(options, args) = parser.parse_args()

------ file1.py ------
import config.py

if config.options.test:
   #do something

------ file2.py ------
import config.py

if config.options.test:
   #do something

I am concerned about executing the parse_args() function in a file other than the "main" file (invoked from the command line).

+1  A: 

What are you trying to do? There are 2 ways you can try -

  1. Pass argument values to your program from command-line (using optparse).
  2. or write a config.py import it in your program & proceed.

Why would you want to do both (& that too in your config file)?

I see that you have written your config module as .py (which is good). But this file should contain minimal code in it. Just config settings. It would help you to keep code & config separate.

The general rule I follow is, if the arguments that need to be passed to my program is more than 6-7, I use a config file. Otherwise optparse it is.

MovieYoda
That's a good point. I guess I was thinking I would just put --verbose and --file filename into the command line arguments. Since those change so often, I thought opening config.py for that would be silly. But I guess I can combine everything into config.py.
max
MovieYoda