2

I am using ArgParse for giving commandline parameters in Python.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--quality", help="enter some quality limit")
args = parser.parse_args()
print "You gave quality = %s" % str(args.quality)

I saved this as a.py then ran this:

$ python a.py --quality 10
You gave quality = 10

I also want my code to run even if the commandline parameter is not provided.I want to make it optional.And if its provided,then it take a certain specific value which can be used further.

I have something like this in my code :

 if int(quality)==10:

So if I run without the parameter:

 $ python a.py

I get this error:

TypeError: int() argument must be a string or a number, not 'NoneType'

5
  • 1
    Erm... it is already optional. In fact the code you have written runs even without specifying the --quality option. You can specify a default value with the default keyword argument. Commented Jun 12, 2014 at 18:48
  • @Luigi That question is about making positional arguments optional. As pointed out by Bakuriu, arguments beginning with - are already optional. Commented Jun 12, 2014 at 18:57
  • I have added the error I am getting. Commented Jun 12, 2014 at 19:05
  • 1
    The default value is None. You need to either change the default or write code that can handle None. Commented Jun 12, 2014 at 19:23
  • I like to include a print(args) while developing with argparse. In this case it would have clearly shown quality=None. Commented Jun 12, 2014 at 19:48

2 Answers 2

6

I think you want:

parser.add_argument("--quality", 
                    type=int,
                    default=10,
                    help="enter some quality limit")

Specify the type as int and specify the default value you want. The problem you are encountering is because you haven't given the argument a default value (via the default keyword argument) so if it isn't specified on the command line it is defaulting to None, which int(quality) is choking on. Setting the type to int above avoids the need to do an int() cast at all.

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

Comments

0

Straight from the help document, read that first:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int, help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int,
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
    print "the square of {} equals {}".format(args.square, answer)
elif args.verbosity == 1:
    print "{}^2 == {}".format(args.square, answer)
else:
    print answer

Here's a solution to your question

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--quality", help="enter some quality limit")
args = parser.parse_args()
if args.quality: #Optional parameter
    #act if it was provided
    print "You gave quality = %s" % str(args.quality)
else:
    #what to do if it wasn't provided
    print "You failed to provide a quantity"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.