By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

How to parse command line arguments separated by ':' instead of spacing?

P: 3
Hi, I'm working on implementing some python in django and trying to write a testing prog to accept a range of values. We'd like to be able to write the commands something like:

manage.py commandfile --values=80:90

I see I can use the make_option args to write something like:

manage.py commandfile --values=80 90

when I set the type to int & the nargs to 2. There should be a way to specify that the separation between the values can be something other than a space but I'm having trouble finding anything. It'd sure make things go easier if I could.

ie:
Expand|Select|Wrap|Line Numbers
  1. class Command(NoArgsCommand):
  2.  
  3.      option_list = BaseCommand.option_list + (
  4.          make_option('--values', type="int", nargs=2,
  5.                 default=None, dest='values',
  6.                 help='Specifies the range of values to add to the db')
  7.               )
  8.  
Jan 29 '11 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Why not make the option as a string (example "0:100") and parse the string, validate, and split the string into the lower and upper values. Simple interactive example:
Expand|Select|Wrap|Line Numbers
  1. from optparse import OptionParser
  2.  
  3. parser = OptionParser()
  4. # defaults to "string" and "store"
  5. parser.add_option("-r",
  6.                   action="store",
  7.                   type="string",
  8.                   dest="range",
  9.                   help="lower integer:upper integer",
  10.                   default="0:100")
  11.  
  12. options, args = parser.parse_args(["-r 120:240"])
  13.  
  14. try:
  15.     upper, lower = [int(item.strip()) for item in options.range.split(":")]
  16.     print options.range
  17.     print upper, lower
  18. except:
  19.     print "Invalid range option"
  20.     parser.print_help()
  21.  
Jan 30 '11 #2

P: 3
Thanks for your reply bvdet, but I'd rather not have to parse a string. Its ok if I was just doing one or two options but I have many to do, and it'd save on a lot of my own manual checking if I could just insert a different type of separator. Python has the nargs & type commands which does all that for me, so I just want to utilize it to its potential.
Jan 30 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
You could create your own customized argument type. See the docs here.
Jan 30 '11 #4

P: 3
Thanks, that's exactly what I was looking for (i think anyway), but Django seems to have some problem with this, it doesn't seem to like the idea of creating new types. There seems to be some limitations with the Base Commands and I can't seem to find any good documentation on it.

Thanks anyway.
Jan 30 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.