if (!arg.getAction().consumeArgument()) {
if (embeddedValue == null) {
arg.run(this, res, flag, null);
return;
} else {
throw new ArgumentParserException(String.format(
TextHelper.LOCALE_ROOT,
"ignore implicit argument '%s'", embeddedValue), this,
arg);
}
}
if (arg.getMinNumArg() == -1
|| (arg.getMinNumArg() == 0 && arg.getMaxNumArg() == 1)) {
// In case of: option takes exactly one argument, or nargs("?")
String argval = null;
if (embeddedValue == null) {
if (state.isArgAvail() && !flagFound(state)) {
argval = state.getArg();
++state.index;
}
} else {
argval = embeddedValue;
}
if (argval == null) {
if (arg.getMinNumArg() == -1) {
if (arg.isOptionalArgument()) {
throw new ArgumentParserException(
"expected one argument", this, arg);
} else {
throw new ArgumentParserException("too few arguments",
this);
}
} else if (arg.isOptionalArgument()) {
// This is a special treatment for nargs("?"). If flag is
// given but no argument follows, produce const value.
arg.run(this, res, flag, arg.getConst());
}
} else {
arg.run(this, res, flag, arg.convert(this, argval));
}
} else {
List<Object> list = new ArrayList<Object>();
if (embeddedValue == null) {
for (int i = 0; i < arg.getMaxNumArg() && state.isArgAvail(); ++i, ++state.index) {
if (flagFound(state)) {
break;
}
list.add(arg.convert(this, state.getArg()));
}
} else {
list.add(arg.convert(this, embeddedValue));
}
if (list.size() < arg.getMinNumArg()) {
if (arg.isOptionalArgument()) {
throw new ArgumentParserException(String.format(
TextHelper.LOCALE_ROOT, "expected %d argument(s)",
arg.getMinNumArg()), this, arg);
} else {
throw new ArgumentParserException("too few arguments", this);
}
}
// For optional arguments, always process the list even if it is
// empty.
// For positional arguments, empty list means no positional argument