# Copyright (C) 2019-2021 Intel Corporation
#
# SPDX-License-Identifier: MIT
from typing import Iterable, List
import argparse
import textwrap
[docs]def add_subparser(subparsers, name, builder):
return builder(lambda **kwargs: subparsers.add_parser(name, **kwargs))
[docs]def required_count(nmin=0, nmax=0):
assert 0 <= nmin and 0 <= nmax and nmin or nmax
class RequiredCount(argparse.Action):
def __call__(self, parser, args, values, option_string=None):
k = len(values)
if not ((nmin and (nmin <= k) or not nmin) and \
(nmax and (k <= nmax) or not nmax)):
msg = "Argument '%s' requires" % self.dest
if nmin and nmax:
msg += " from %s to %s arguments" % (nmin, nmax)
elif nmin:
msg += " at least %s arguments" % nmin
else:
msg += " no more %s arguments" % nmax
raise argparse.ArgumentTypeError(msg)
setattr(args, self.dest, values)
return RequiredCount
[docs]def at_least(n):
return required_count(n, 0)
[docs]def join_cli_args(args: argparse.Namespace, *names: Iterable[str]) -> List:
"Merges arg values in a list"
joined = []
for name in names:
value = getattr(args, name)
if not isinstance(value, list):
value = [value]
joined += value
return joined