45

I'm trying to use argument parser to parse a 3D coordinate so I can use

--cord 1,2,3 2,4,6 3,6,9

and get

((1,2,3),(2,4,6),(3,6,9))

My attempt is

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--cord', help="Coordinate", dest="cord", type=tuple, nargs=3)
args = parser.parse_args(["--cord","1,2,3","2,4,6","3,6,9"])

vars(args) 
{'cord': [('1', ',', '2', ',', '3'),
  ('2', ',', '4', ',', '6'),
  ('3', ',', '6', ',', '9')]}

What would the replacement of the comma be?

joedborg
  • 17,651
  • 32
  • 84
  • 118

2 Answers2

78

You can add your own type. This also allows for additional validations, for example:

def coords(s):
    try:
        x, y, z = map(int, s.split(','))
        return x, y, z
    except:
        raise argparse.ArgumentTypeError("Coordinates must be x,y,z")


parser.add_argument('--cord', help="Coordinate", dest="cord", type=coords, nargs=3)
georg
  • 211,518
  • 52
  • 313
  • 390
  • 1
    +1 Thanks! Have changed the raise to a TypeError. Thanks for the answer! – joedborg Apr 02 '12 at 16:20
  • 2
    since this case is a `3D` coordinate `nargs=3` is obvious, but if your case doesn't require a fixed number of tuples, you can use `nargs='+'` – Juan-Kabbali May 19 '21 at 15:01
0

For my problem, I had to have a more general approach, not linked to the number of inputs.

Starting from the great answer by georg, I solved my problem as follows

# additional type
def coords(s):
    seps = r'[ ;.]'
    try:
        situp = []
        for si in re.split(seps, s):
            situp.append(tuple(map(int, si.split(','))))
        return situp
    except:
        raise argparse.ArgumentTypeError("Coordinates must be given divided by commas and space, dot, or semicolon e.g.: 'x,y k,l,m'")

With this, an input like 1,2 3,4,5 will be turned in a list of tuples like [(1,2), (3,4,5)]

EDIT: It might be that the for loop is not optimal, but I wrote it to avoid the use of nargs

EDIT 2:

  1. to have a list of list, one should change

    the line situp.append(tuple(map(int, si.split(','))))

    with situp.append(list(map(int, si.split(','))))

  2. to have a tuple of uples one can just change the return with

    return tuple(situp)

brodegon
  • 231
  • 2
  • 12