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'

Rgeek
  • 419
  • 1
  • 9
  • 23
  • 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. – Bakuriu Jun 12 '14 at 18:48
  • @Luigi That question is about making positional arguments optional. As pointed out by Bakuriu, arguments beginning with `-` are already optional. – chepner Jun 12 '14 at 18:57
  • I have added the error I am getting. – Rgeek Jun 12 '14 at 19:05
  • 1
    The default value is `None`. You need to either change the default or write code that can handle `None`. – hpaulj Jun 12 '14 at 19:23
  • I like to include a `print(args)` while developing with `argparse`. In this case it would have clearly shown `quality=None`. – hpaulj Jun 12 '14 at 19:48

2 Answers2

5

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.

mshildt
  • 8,782
  • 3
  • 34
  • 41
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"
Morgoth
  • 4,935
  • 8
  • 40
  • 66