0

my python code looks like this

parser.add_argument("-c","--configFile",action ='store_true',\
 help='I am here one travel')

the idea is when running with -c option, I could have select to use my special config file. However, I issue my command python mypython.py -c myconfig, I am getting unrecognized argument: myconfig. What am I missing?

Bhargav Rao
52.6k29 gold badges130 silver badges142 bronze badges
asked Jan 19, 2015 at 18:45

2 Answers 2

3

As Adam mentioned, you'll want to change your parser's "action", as stated in the docs.

But it sounds like you want to indicate whether that special config is active, or not. In which case, using action='store_true' or action='store_false' would be correct. You just won't pass the myconfig argument.

parser = argparse.ArgumentParser(description='So something')
parser.add_argument(
 '-c',
 '--c',
 dest='enable_config',
 action='store_true',
 required=False,
 help='Enable special config settings',
)
answered Jan 19, 2015 at 19:05
Sign up to request clarification or add additional context in comments.

3 Comments

I am kind of struggling here. I i enter my code here:
if I issue the command "Python myprog.py -c user_config_file." How do I refer to the value user_config_file? I need to do something with it. Thanks!
If you want to specify a filename, you'll need to capture it as an argument. For instance, here I am capturing a list of arguments for a parser: parser.add_argument( dest='filenames', metavar='filename', nargs='*', help='list of files to compress within a specified directory', ) Note the nargs argument allows for an unlimited number of filenames to be passed. You'll want to limit this to one file for your use. Also note that there is no action needed for add_argument since the default action is store, which captures the argument's value.
2

"-c" will store True or False, so why are you passing a value (myconfig) to it? You would call it as:

python mypython.py -c

If you want -c to take an argument, then it shouldn't have the action store_true.

answered Jan 19, 2015 at 18:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.