Source code for datumaro.cli.util

# 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]class MultilineFormatter(argparse.HelpFormatter): """ Keeps line breaks introduced with '|n' separator and spaces introduced with '|s'. """
[docs] def __init__(self, keep_natural=False, **kwargs): super().__init__(**kwargs) self._keep_natural = keep_natural
def _fill_text(self, text, width, indent): text = self._whitespace_matcher.sub(' ', text).strip() text = text.replace('|s', ' ') paragraphs = text.split('|n ') if self._keep_natural: paragraphs = sum((p.split('\n ') for p in paragraphs), []) multiline_text = '' for paragraph in paragraphs: formatted_paragraph = textwrap.fill(paragraph, width, initial_indent=indent, subsequent_indent=indent) + '\n' multiline_text += formatted_paragraph return multiline_text
[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