Source code for cowbird.cli.utils

import argparse
import json
import logging
from typing import TYPE_CHECKING

import yaml

from cowbird.constants import get_constant

    from typing import Callable, Dict, Iterable, Optional, Sequence

[docs] CommandPrefixes = Optional[Iterable[str]]
SharedParsers = Optional[Iterable[argparse.ArgumentParser]] ParsedArgs = Optional[argparse.Namespace] ParserArgs = Optional[Sequence[str]] HelperParser = Optional[argparse.ArgumentParser] ParseResult = int ParserMaker = Callable[[SharedParsers, CommandPrefixes], argparse.ArgumentParser] ParserRunner = Callable[[ParserArgs, HelperParser, ParsedArgs], ParseResult]
[docs]def subparser_help(description, parent_parser=None): # type: (str, Optional[argparse.ArgumentParser]) -> Dict[str, str] """ Generates both fields with the same description as each parameter is used in different context. Field ``help`` is printed next to the subparser name when *parent parser* is called with ``--help``. Field ``description`` populates the help details under the usage command when calling *child parser* ``--help``. """ desc = {"help": description, "description": description} if parent_parser: desc.update({"usage": parent_parser.usage}) return desc
[docs]def get_config_parser(): # type: () -> argparse.ArgumentParser parser = argparse.ArgumentParser(add_help=False) parser.add_argument("-c", "--config", help="INI configuration file to employ.", default=get_constant("COWBIRD_INI_FILE_PATH", raise_missing=False, raise_not_set=False)) return parser
[docs]def get_logger_parser(): # type: () -> argparse.ArgumentParser parser = argparse.ArgumentParser(add_help=False) group = parser.add_mutually_exclusive_group() group.add_argument("-q", "--quiet", action="store_true", help="Suppress informative logging.") group.add_argument("-d", "--debug", action="store_true", help="Set debug logging level.") group.add_argument("-l", "--level", choices=["debug", "info", "warn", "error"], default="info") return parser
[docs]def set_log_level(args, logger=None): # type: (argparse.Namespace, Optional[logging.Logger]) -> None from cowbird.cli import LOGGER logger = logger or LOGGER if args.quiet: logger.setLevel(logging.ERROR) elif args.debug: logger.setLevel(logging.DEBUG) elif args.level: logger.setLevel(args.level.upper()) else: logger.setLevel(logging.INFO)
[docs]def get_format_parser(): # type: () -> argparse.ArgumentParser parser = argparse.ArgumentParser(add_help=False) parser.add_argument("-f", "--format", choices=["flat", "json", "table", "yaml"], default="json", help="Output format of the command.") return parser