You'll need to define a custom action for such specialized behavior.
import sys
import argparse
class AbsAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
if len(values) % 2 == 0:
# If valid, store the values.
setattr(namespace, self.dest, values)
# You could convert the flat list to a list of 2-tuples, if needed:
# zip(values[::2], values[1::2])
else:
# Otherwise, invoke a parser error with a message.
parser.error('abs must be supplied as pairs')
ap = argparse.ArgumentParser()
ap.add_argument('z')
ap.add_argument('abs', nargs = '+', action = AbsAction)
opt = ap.parse_args()
print opt
nargs='+'and then can use a custom action to check for even pairs. and as @deshko mentioned the first positional argument can be specified as z withnargs=1check stackoverflow.com/questions/13174975/… for the choice on nargs