Commit fa16bb40 authored by Jerome Mariette's avatar Jerome Mariette
Browse files

handle required exclusive sub parameters

parent 158242d1
......@@ -48,10 +48,23 @@ class MiltipleParameters(object):
class MiltipleAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
# what is commun within required and excludes
exclusif_required = {}
for exclude_group in values[0][3]:
exclusif_required[exclude_group] = False
for param in values[0][3][exclude_group]:
if param in values[0][2]:
exclusif_required[exclude_group] = True
given_params = []
# first check for required parameters
try:
required = _copy.copy(values[0][2])
# delete required that are exclusive
for group in exclusif_required:
if exclusif_required[group]:
for val in values[0][3][group]:
if val in required:
del required[required.index(val)]
for val in values:
given_params.append(val[0])
if val[0] in required:
......@@ -67,15 +80,37 @@ class MiltipleAction(argparse.Action):
parser.error("argument '" + found + "': not allowed with argument '" + param + "'")
break
elif param in given_params: found = param
# check for required exclusive if one of them is in
if len(exclusif_required.keys()) > 0:
for group in exclusif_required:
if exclusif_required[group]:
rfound = False
for param in values[0][3][group]:
if param in given_params: rfound = True
if not rfound: parser.error("one of the arguments: " + ", ".join(values[0][3][group]) + " is required")
# if ok add the value
setattr(namespace, self.dest, values)
class MiltipleAppendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
# what is commun within required and excludes
exclusif_required = {}
for exclude_group in values[0][3]:
exclusif_required[exclude_group] = False
for param in values[0][3][exclude_group]:
if param in values[0][2]:
exclusif_required[exclude_group] = True
given_params = []
# first check for required parameters
try:
required = _copy.copy(values[0][2])
# delete required that are exclusive
for group in exclusif_required:
if exclusif_required[group]:
for val in values[0][3][group]:
if val in required:
del required[required.index(val)]
for val in values:
given_params.append(val[0])
if val[0] in required:
......@@ -91,6 +126,15 @@ class MiltipleAppendAction(argparse.Action):
parser.error("argument '" + found + "': not allowed with argument '" + param + "'")
break
elif param in given_params: found = param
# check for required exclusive if one of them is in
if len(exclusif_required.keys()) > 0:
for group in exclusif_required:
if exclusif_required[group]:
rfound = False
for param in values[0][3][group]:
if param in given_params: rfound = True
if not rfound: parser.error("one of the arguments: " + ", ".join(values[0][3][group]) + " is required")
# if ok add the value
items = _copy.copy(_ensure_value(namespace, self.dest, []))
items.append(values)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment