0

I have created a python program that I will be compiling into an EXE but before that I want to have a command line switch that lets me specify the config file I want to use. My idea is to have multiple config files that server different purposes. I have searched the internet but am confused by what I am seeing. Any advice on how to do this would be greatly appreciated...

2
  • Take a look at argparse. Commented Aug 25, 2014 at 17:37
  • Why not just use a command line parameter for the config file? Commented Aug 25, 2014 at 17:37

1 Answer 1

3

You should have a look at the argparse module, which handles command line options for you.

Edit: Let me give you a simple example.

import argparse

# create a new argument parser
parser = argparse.ArgumentParser(description="Simple argument parser")
# add a new command line option, call it '-c' and set its destination to 'config'
parser.add_argument("-c", action="store", dest="config_file")

# get the result
result = parser.parse_args()
# since we set 'config_file' as destination for the argument -c, 
# we can fetch its value like this (and print it, for example):
print(result.config_file)

You can then proceed to use result.config_file as the file name of the config file passed to the script.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I was looking at that but I am a huge newbie at this and I am a little confused by what I am seeing... If you could provide any additional insight it would be greatly appreciated... I will continue to try to figure it out...
So if I understand what I am seeing I can import argparse parser = argparse.ArgumentParser parser.add_argument('-c', action="store", dest="config") config = ConfigParser.ConfigParser() config.readfp(open(config))
user3739525: Try it and see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.