1

I've been studying a little bit of Python and I came across the getopt module for parsing command-line arguments.

Basically, I've got the following code:

import sys, getopt

print("The list of %s arguments passed:" % len(sys.argv))

# Print each argument
for arg in sys.argv:
    print(arg)
print()

# Now print parsed arguments
opts, args = getopt.getopt(sys.argv[1:], "ab:cd", ["arbitrary", "balance=", "cite"])
for opt in opts:
    print(opt)
print()

# Print the arguments returned
print(args)

However, I need the -b option to take two different arguments, E.g -b one two. I've tried putting two colons after the b in the argument list of getopt, but it didn't work.

If someone can tell me how to do this using the getopt module and post examples it would be really useful!

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Mat Gomes
  • 435
  • 1
  • 8
  • 22
  • 2
    Note that `getopt` is deprecated; use the far more versatile [`argparse` module](https://docs.python.org/2/library/argparse.html) instead. It supports multiple values per option. – Martijn Pieters Sep 04 '15 at 17:41
  • 1
    Related read - [PEP 0389](https://www.python.org/dev/peps/pep-0389/) which has mentioned [deprecation of `optparse`](https://www.python.org/dev/peps/pep-0389/#deprecation-of-optparse) and [de-emphasized `getopt`](https://www.python.org/dev/peps/pep-0389/#updates-to-getopt-documentation) – Bhargav Rao Sep 04 '15 at 17:44
  • use `argparse` and surround the argument with double quotes `-b "argument with spaces"` – Pedro Lobito Sep 04 '15 at 18:03
  • @MartijnPieters Ahhhh, I didn't know that! Sometimes I forget I'm reading 'Dive Into Python' which was written a few years ago... Perhaps if you made your comment into an answer I'd accept it as the main answer :) – Mat Gomes Sep 04 '15 at 18:05
  • @MatGomes Why don't you divide the `-b` in two arguments ? – Pedro Lobito Sep 04 '15 at 18:06
  • @BhargavRao Thanks! I'm reading this article right now! – Mat Gomes Sep 04 '15 at 18:09
  • @PedroLobito I guess I could do that, but I just wanted to know if there's a way of getting multiple arguments for a parameter – Mat Gomes Sep 04 '15 at 18:17
  • @MatGomes : it's possible with Docopt; have a look to the answer I wrote :) – Bruno Duyé Sep 04 '15 at 18:31

1 Answers1

2

Forget getopt, use Docopt (really):

If I understand well, you want user to pass 2 arguments to balance. This can be acheived by :

doc = """Usage:
   test.py balance= <b1> <b2>
   test.py
"""

from docopt import docopt

options, arguments = docopt(__doc__)  # parse arguments based on docstring above

This program accepts : test.py balance= X Y, or no arguments.

Now if we add 'cite' and 'arbitrary' options, this should give us :

doc = """
Usage:
   test.py balance= <b1> <b2>
   test.py

Options:
   --cite -c           Cite option 
   --arbitrary -a      Arbitrary option   
"""

The program now accepts options. Example :

test.py balance= 3 4 --cite

=> options = {
    "--arbitrary": false, 
    "--cite": true, 
    "<b1>": "3", 
    "<b2>": "4", 
    "balance=": true
}

tip: in addition, you can test your documentation string directly in your browser before using it in your code.

Life saver !

Bruno Duyé
  • 1,014
  • 11
  • 13
  • I'm currently looking into the argparse module, I'll have a look at the docopt module once I've understood argparse . Which one is supposed to be better/simpler? argparse or docopt? – Mat Gomes Sep 04 '15 at 18:24
  • @MatGomes [Python - Difference between docopt and argparse](http://stackoverflow.com/questions/20599513/python-difference-between-docopt-and-argparse) – Bhargav Rao Sep 04 '15 at 18:26
  • I learned argparse, then Docopt. And I realised that I'd learned argparse for nothing, because Docopt is straightforward, you don't have to learn an API, you just have to learn (if you don't already know it) a standard. This is also used in Linux man pages so you should be used to it. – Bruno Duyé Sep 04 '15 at 18:38
  • If you're looking for really (really) more complicated things, you can have a look to [Click](http://click.pocoo.org/) – Bruno Duyé Sep 04 '15 at 18:39