Example #1
0
    def build_parser(self, choices: list) -> ArgumentParser:
        parser = ArgumentParser(
            prog="i3grid",
            description="i3grid: Manage your floating windows with ease.",
            formatter_class=CustomFormatter,
        )
        parser.register("action", "none", NoAction)
        parser.add_argument(
            "actions",
            metavar="<action>",
            type=str,
            nargs="+",
            choices=choices,
            help=self.action_header(),
        )

        for n, flag in self.flags.items():
            parser.add_argument(
                f"--{n}",
                type=eval(flag["type"]) if "type" in flag else None,
                nargs=flag["nargs"] if "nargs" in flag else None,
                help=flag["help"],
            )
        for n, desc in self.state_flags.items():
            parser.add_argument(f"--{n}", action="store_true", help=desc)

        group = parser.add_argument_group(title="Actions")
        for action, desc in Documentation.actions.items():
            group.add_argument(action, help=desc, action="none")
        return parser
Example #2
0
def test_lazy_choices_help():
    mock = Mock()
    getter = mock.getter
    getter.return_value = ['a', 'b', 'c']

    help_formatter = mock.help_formatter
    help_formatter.return_value = '<my help>'

    parser = ArgumentParser()
    parser.register('action', 'lazy_choices', LazyChoices)
    parser.add_argument(
        '--lazy-option',
        default='a',
        metavar='SYMBOL',
        action='lazy_choices',
        getter=getter,
        help_formatter=help_formatter,
        cache=False  # for test purposes
    )

    # Parser initalization doesn't call it.
    getter.assert_not_called()

    # If we don't use `--help`, we don't use it.
    parser.parse_args([])
    getter.assert_not_called()
    help_formatter.assert_not_called()

    parser.parse_args(['--lazy-option', 'b'])
    help_formatter.assert_not_called()

    # If we use --help, then we call it with styles
    with pytest.raises(SystemExit):
        parser.parse_args(['--help'])
    help_formatter.assert_called_once_with(['a', 'b', 'c'])
Example #3
0
def add_gp_torch_args(parser: ArgumentParser):
    parser.register('type', dict, parse_dict)
    gp_sparse_group = parser.add_argument_group("Sparse GP")
    gp_sparse_group.add_argument("--n_inducing_points", type=int, default=500)
    gp_sparse_group.add_argument("--n_rand_points", type=int, default=8000)
    gp_sparse_group.add_argument("--n_best_points", type=int, default=2000)
    gp_sparse_group.add_argument("--invalid_score", type=float, default=-4.0)

    gp_group = parser.add_argument_group("GP tuning")
    gp_group.add_argument("--scale",
                          type=int,
                          choices=(0, 1),
                          default=1,
                          help="Whether to use a scaled covar module")
    gp_group.add_argument(
        "--covar-name",
        type=str,
        default='matern-5/2',
        help="Name of the kernel to use (`matern-5/2`, `RBF`...)")

    gp_group = parser.add_argument_group("Acquisition function maximisation")
    gp_group.add_argument(
        "--acq-func-id",
        type=str,
        default='ExpectedImprovement',
        help=
        "Name of the acquisition function to use (`ExpectedImprovement`, `UpperConfidenceBound`...)"
    )
    gp_group.add_argument("--acq-func-kwargs",
                          type=dict,
                          default={},
                          help="Acquisition function kwargs")
    gp_group.add_argument("--acq-func-opt-kwargs",
                          type=dict,
                          default={},
                          help="Acquisition function Optimisation kwargs")
    gp_group.add_argument("--q",
                          type=int,
                          default=1,
                          help="Acquisition batch size")
    gp_group.add_argument("--num-restarts",
                          type=int,
                          default=100,
                          help="Number of start points")
    gp_group.add_argument(
        "--raw-initial-samples",
        type=int,
        default=1000,
        help="Number of initial points used to find start points")
    gp_group.add_argument(
        "--num-MC-sample-acq",
        type=int,
        default=256,
        help="Number of samples to use to evaluate posterior distribution")

    return parser
Example #4
0
def test_argumentparser_log_level_action(const, count, expect):
    """The log level action works correctly with an ``ArgumentParser``"""
    parser = ArgumentParser()
    parser.register("action", "log_level", argparse_helpers.LogLevelAction)
    parser.add_argument("-l",
                        dest="log_level",
                        action="log_level",
                        const=const)

    args = parser.parse_args(count * ["-l"])

    assert args.log_level == expect
Example #5
0
def register_argparse_extend_action_in_pre_py38(
        parser: argparse.ArgumentParser):
    import sys

    if sys.version_info < (3, 8):

        class ExtendAction(argparse.Action):
            def __call__(self, parser, namespace, values, option_string=None):
                items = getattr(namespace, self.dest) or []
                items.extend(values)
                setattr(namespace, self.dest, items)

        parser.register('action', 'extend', ExtendAction)
Example #6
0
def make_argument_parser(require_src: bool) -> ArgumentParser:
    """Create the argument parser object

    :param require_src: ``True`` to require at least one path as a positional argument
                        on the command line. ``False`` to not require on.

    """
    parser = ArgumentParser(
        description=hlp.DESCRIPTION, formatter_class=NewlinePreservingFormatter
    )
    parser.register("action", "log_level", LogLevelAction)

    def add_arg(help_text: Optional[Text], *name_or_flags: Text, **kwargs: Any) -> None:
        kwargs["help"] = help_text
        parser.add_argument(*name_or_flags, **kwargs)

    add_arg(hlp.SRC, "src", nargs="+" if require_src else "*", metavar="PATH")
    add_arg(hlp.REVISION, "-r", "--revision", default="HEAD")
    add_arg(hlp.DIFF, "--diff", action="store_true")
    add_arg(hlp.CHECK, "--check", action="store_true")
    add_arg(hlp.ISORT, "-i", "--isort", action="store_true")
    add_arg(hlp.LINT, "-L", "--lint", action="append", metavar="CMD", default=[])
    add_arg(hlp.CONFIG, "-c", "--config", metavar="PATH")
    add_arg(
        hlp.VERBOSE,
        "-v",
        "--verbose",
        action="log_level",
        dest="log_level",
        const=-10,
    )
    add_arg(hlp.QUIET, "-q", "--quiet", action="log_level", dest="log_level", const=10)
    add_arg(hlp.VERSION, "--version", action="version", version=__version__)
    add_arg(
        hlp.SKIP_STRING_NORMALIZATION,
        "-S",
        "--skip-string-normalization",
        action="store_const",
        const=True,
    )
    add_arg(
        hlp.NO_SKIP_STRING_NORMALIZATION,
        "--no-skip-string-normalization",
        action="store_const",
        dest="skip_string_normalization",
        const=False,
    )
    add_arg(hlp.LINE_LENGTH, "-l", "--line-length", type=int, dest="line_length")
    return parser
Example #7
0
    def execute(self, version):
        """
        Entry point to the command execution. It is used for the main entry function of an application.

        :param version: Version string to display for --version calls
        :type version: str
        :return: None
        """
        parser = ArgumentParser(prog=os.path.basename(sys.argv[0]),
                                formatter_class=_LeappHelpFormatter)
        parser.register('action', 'parsers', _SubParserActionOverride)
        parser.add_argument('--version', action='version', version=version)
        parser.set_defaults(func=None)
        s = parser.add_subparsers(title='Main commands', metavar='')
        self.apply_parser(s, parser=parser)
        args = parser.parse_args()
        args.func(args)
Example #8
0
def test_lazy_choices():
    mock = Mock()
    getter = mock.getter
    getter.return_value = ['a', 'b', 'c']

    parser = ArgumentParser()
    parser.register('action', 'lazy_choices', LazyChoices)
    parser.add_argument(
        '--option',
        help="the regular option",
        default='a',
        metavar='SYMBOL',
        choices=['a', 'b'],
    )
    parser.add_argument(
        '--lazy-option',
        help="the lazy option",
        default='a',
        metavar='SYMBOL',
        action='lazy_choices',
        getter=getter,
        cache=False  # for test purposes
    )

    # Parser initalization doesn't call it.
    getter.assert_not_called()

    # If we don't use --lazy-option, we don't retrieve it.
    parser.parse_args([])
    getter.assert_not_called()

    parser.parse_args(['--option', 'b'])
    getter.assert_not_called()

    # If we pass a value, it will retrieve to verify.
    parser.parse_args(['--lazy-option', 'c'])
    getter.assert_called()
    getter.reset_mock()

    with pytest.raises(SystemExit):
        parser.parse_args(['--lazy-option', 'z'])
    getter.assert_called()
    getter.reset_mock()
def build_option():
    parser = ArgumentParser()
    parser.register('type', 'bool', _bool)

    parser.add_argument('-ex',
                        '--experiment_name',
                        type=str,
                        default='test_1',
                        help='실험 이름. 이것에 따라 log와 checkpoint 이름 결정')

    # Model
    # ============================================================================
    parser.add_argument('-m',
                        '--model_name',
                        type=str,
                        default="VGGNet",
                        help='model 이름(VGGNet, ResNet')

    parser.add_argument('-ls',
                        '--loss',
                        type=str,
                        default="L1",
                        help='model 학습할 때 쓰일 loss')

    parser.add_argument('-sl',
                        '--start_lr',
                        type=float,
                        default=0.001,
                        help='model 학습할 때 처음 learning rate 값')

    parser.add_argument('-lds',
                        '--lr_decay_step',
                        type=int,
                        default=4000,
                        help='learning rate decay할 주기(step)')

    parser.add_argument('-ldr',
                        '--lr_decay_rate',
                        type=float,
                        default=0.8,
                        help='learning rate decay 비율')

    # ============================================================================

    # Path
    # ============================================================================
    parser.add_argument('-l',
                        '--logs_dir',
                        type=str,
                        default='./logs',
                        help='tensorflow model log를 저장할 폴더')

    parser.add_argument('-c',
                        '--checkpoint_dir',
                        type=str,
                        default='./checkpoint',
                        help='model의 paramter를 저장할 폴더')

    parser.add_argument('-t',
                        '--tfrecord_train_path',
                        type=str,
                        default='./data/tfrecord/train_40.tfrecord',
                        help="train set을 읽을 tfrecord 파일 이름과 경로")

    parser.add_argument('-e',
                        '--tfrecord_test_path',
                        type=str,
                        default='./data/tfrecord/test.tfrecord',
                        help="test set을 읽을 tfrecord 파일 이름과 경로")

    parser.add_argument('-v',
                        '--tfrecord_valid_path',
                        type=str,
                        default='./data/tfrecord/valid.tfrecord',
                        help="valid set을 읽을 tfrecord 파일 이름과 경로")

    parser.add_argument('-d',
                        '--debug_image_dir',
                        type=str,
                        default='./debug/image',
                        help="학습하면서 나오는 이미지들을 저장하는 폴더")

    # ============================================================================

    # Dataset
    # ============================================================================
    parser.add_argument('-nic',
                        '--n_input_channel',
                        type=int,
                        default=66,
                        help='이미지가 가지고 있는 채널 수')

    parser.add_argument('-noc',
                        '--n_output_channel',
                        type=int,
                        default=3,
                        help='이미지가 가지고 있는 채널 수')

    parser.add_argument('-wi',
                        '--img_width',
                        type=int,
                        default=1280,
                        help='이미지 가로 크기')

    parser.add_argument('-he',
                        '--img_height',
                        type=int,
                        default=720,
                        help='이미지 세로 크기')

    parser.add_argument('-p',
                        '--patch_size',
                        type=int,
                        default=40,
                        help="학습용 패치 크기")

    parser.add_argument('-b',
                        '--batch_size',
                        type=int,
                        default=32,
                        help="minibatch 크기")

    parser.add_argument('-sf',
                        '--shuffle_buffer',
                        type=int,
                        default=100,
                        help="tfrecord 파일을 읽을 때 섞기 위한 shuffle buffer 크기")

    parser.add_argument('-ps',
                        '--prefetch_size',
                        type=int,
                        default=1,
                        help="미리 읽는 거라는데 잘 모르겠음")

    # ============================================================================

    # Training
    # ============================================================================
    parser.add_argument('-ep',
                        '--n_epoch',
                        type=int,
                        default=1000000000,
                        help='epoch 수. 보통은 그냥 크게!')

    parser.add_argument('-lp',
                        '--log_period',
                        type=int,
                        default=100,
                        help='일정 주기마다 logging(print, log)')

    parser.add_argument('-vp',
                        '--valid_period',
                        type=int,
                        default=3000,
                        help='일정 주기마다 모델 parameter 저장')

    parser.add_argument('-sp',
                        '--save_period',
                        type=int,
                        default=5000,
                        help='일정 주기마다 모델 parameter 저장')

    parser.add_argument('-nsp',
                        '--n_save_patch_img',
                        type=int,
                        default=2,
                        help='학습 중 저장할 patch 개수')

    # ============================================================================

    option = parser.parse_args()

    option.text_dir = os.path.join(option.debug_image_dir,
                                   option.experiment_name)
    make_dir(option.text_dir)
    f = open(option.text_dir + '/option.txt', 'w')

    for op in vars(option):
        f.write(op + ': ' + str(getattr(option, op)) + '\n')

    f.close()
    return option
Example #10
0
from __future__ import print_function

import numpy as np
import cv2
import os.path as osp
from os import listdir
import fnmatch
from omnistereo.common_tools import make_sure_path_exists, str2bool, load_obj_from_pickle

from argparse import ArgumentParser
parser = ArgumentParser(
    description=
    'Demo of frame-to-frame visual odometry for the Single-camera SOS images collected for the vo_single_camera_sos project.'
)
parser.register(
    'type', 'bool', str2bool
)  # add type 'bool' keyword to the registry because it doesn't exist by default!

parser.add_argument(
    'sequence_path',
    nargs=1,
    help='The path to the sequence where the omni folder is located.')

parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
required.add_argument(
    '--calibrated_gums_file',
    help=
    'Indicates the complete path and name of the calibrated GUMS pickle file',
    default='gums-calibrated.pkl',
Example #11
0
class SoS():
    """Main entrypoint for sos from the command line

    Upon intialization, this class loads the basic option parser which will
    include the options shared by support components/subcommands. This is also
    where all subcommands present in the local installation are discovered,
    loaded, and if a matching one is found, intialized.
    """
    def __init__(self, args):
        self.cmdline = args
        # define the local subcommands that exist on the system
        # first import the necessary module, then add an entry to the dict that
        # follows the tuple format (class, [aliases]), where aliases is a list
        # of shorthand names to accept in place of the full subcommand
        # if no aliases are desired, pass an empty list
        import sos.report
        import sos.cleaner
        import sos.help
        self._components = {
            'report': (sos.report.SoSReport, ['rep']),
            'clean': (sos.cleaner.SoSCleaner, ['cleaner', 'mask']),
            'help': (sos.help.SoSHelper, [])
        }
        # some distros do not want pexpect as a default dep, so try to load
        # collector here, and if it fails add an entry that implies it is at
        # least present on this installation
        try:
            import sos.collector
            self._components['collect'] = (sos.collector.SoSCollector,
                                           ['collector'])
        except ModuleNotFoundError as err:
            import sos.missing
            if 'sos.collector' in err.msg:
                # is not locally installed - packaged separately
                self._components['collect'] = (sos.missing.MissingCollect, [])
            elif 'pexpect' in err.msg:
                # cannot be imported due to missing the pexpect dep
                self._components['collect'] = (sos.missing.MissingPexpect, [])
            else:
                # we failed elsewhere, re-raise the exception
                raise
        # build the top-level parser
        _com_string = ''
        for com in self._components:
            aliases = self._components[com][1]
            aliases.insert(0, com)
            _com = ', '.join(aliases)
            desc = self._components[com][0].desc
            _com_string += ("\t{com:<30}{desc}\n".format(com=_com, desc=desc))
        usage_string = ("%(prog)s <component> [options]\n\n"
                        "Available components:\n")
        usage_string = usage_string + _com_string
        epilog = ("See `sos <component> --help` for more information")
        self.parser = ArgumentParser(usage=usage_string, epilog=epilog)
        self.parser.register('action', 'extend', SosListOption)
        # set the component subparsers
        self.subparsers = self.parser.add_subparsers(
            dest='component', metavar='component', help='sos component to run')
        self.subparsers.required = True
        # now build the parser for each component.
        # this needs to be done here, as otherwise --help will be unavailable
        # for the component subparsers
        for comp in self._components:
            _com_subparser = self.subparsers.add_parser(
                comp, aliases=self._components[comp][1], prog="sos %s" % comp)
            _com_subparser.usage = "sos %s [options]" % comp
            _com_subparser.register('action', 'extend', SosListOption)
            self._add_common_options(_com_subparser)
            self._components[comp][0].add_parser_options(parser=_com_subparser)
            _com_subparser.set_defaults(component=comp)
        self.args = self.parser.parse_args(self.cmdline)
        self._init_component()

    def _add_common_options(self, parser):
        """Adds the options shared across components to the parser
        """
        global_grp = parser.add_argument_group('Global Options')
        global_grp.add_argument("--batch",
                                default=False,
                                action="store_true",
                                help="Do not prompt interactively")
        global_grp.add_argument("--config-file",
                                type=str,
                                action="store",
                                dest="config_file",
                                default="/etc/sos/sos.conf",
                                help="specify alternate configuration file")
        global_grp.add_argument("--debug",
                                action="store_true",
                                dest="debug",
                                help="enable interactive debugging using the "
                                "python debugger")
        global_grp.add_argument("-q",
                                "--quiet",
                                action="store_true",
                                dest="quiet",
                                default=False,
                                help="only print fatal errors")
        global_grp.add_argument("-s",
                                "--sysroot",
                                action="store",
                                dest="sysroot",
                                default=None,
                                help="system rootdir path (default='/')")
        global_grp.add_argument("--tmp-dir",
                                action="store",
                                dest="tmp_dir",
                                default=None,
                                help="specify alternate temporary directory")
        global_grp.add_argument("-t",
                                "--threads",
                                action="store",
                                dest="threads",
                                default=4,
                                type=int,
                                help="Number of threads to use")
        global_grp.add_argument("-v",
                                "--verbose",
                                action="count",
                                dest="verbosity",
                                default=0,
                                help="increase verbosity")

        global_grp.add_argument('-z',
                                '--compression-type',
                                dest="compression_type",
                                choices=['auto', 'gzip', 'xz'],
                                help="compression technology to use")

        # Group to make tarball encryption (via GPG/password) exclusive
        encrypt_grp = global_grp.add_mutually_exclusive_group()
        encrypt_grp.add_argument("--encrypt",
                                 default=False,
                                 action="store_true",
                                 help=("Encrypt the archive, either prompting "
                                       "for a password/key or referencing "
                                       "an environment variable"))
        encrypt_grp.add_argument("--encrypt-key",
                                 help="Encrypt the archive using a GPG "
                                 "key-pair")
        encrypt_grp.add_argument("--encrypt-pass",
                                 help="Encrypt the archive using a password")

    def _init_component(self):
        """Determine which component has been requested by the user, and then
        initialize that component.
        """
        _com = self.args.component
        if _com not in self._components.keys():
            print("Unknown subcommand '%s' specified" % _com)
        try:
            _to_load = self._components[_com][0]
            if _to_load.root_required and not os.getuid() == 0:
                raise Exception("Component must be run with root privileges")
            self._component = _to_load(self.parser, self.args, self.cmdline)

        except Exception as err:
            print("Could not initialize '%s': %s" % (_com, err))
            if self.args.debug:
                raise err
            sys.exit(1)

    def execute(self):
        self._component.execute()
Example #12
0
def _init_argument_parser(argument_parser: ArgumentParser):
    register_type(tp_bool, argument_parser)
    argument_parser.register('delay', AKU, [])
Example #13
0
def install_set_ops(parser: ArgumentParser):
    parser.register('action', 'add_to_set', AddToSetAction)
    parser.register('action', 'remove_from_set', RemoveFromSetAction)
Example #14
0
class SoS():
    """Main entrypoint for sos from the command line

    Upon intialization, this class loads the basic option parser which will
    include the options shared by support components/subcommands. This is also
    where all subcommands present in the local installation are discovered,
    loaded, and if a matching one is found, intialized.
    """

    def __init__(self, args):
        self.cmdline = args
        # define the local subcommands that exist on the system
        # first import the necessary module, then add an entry to the dict that
        # follows the tuple format (class, [aliases]), where aliases is a list
        # of shorthand names to accept in place of the full subcommand
        # if no aliases are desired, pass an empty list
        import sos.report
        self._components = {
            'report': (sos.report.SoSReport, ['rep'])
        }
        # build the top-level parser
        _com_string = ''
        for com in self._components:
            _com_string += ("\t%s\t\t\t%s\n"
                            % (com, self._components[com][0].desc))
        usage_string = ("%(prog)s <component> [options]\n\n"
                        "Available components:\n")
        usage_string = usage_string + _com_string
        epilog = ("See `sos <component> --help` for more information")
        self.parser = ArgumentParser(usage=usage_string, epilog=epilog)
        self.parser.register('action', 'extend', SosListOption)
        # set the component subparsers
        self.subparsers = self.parser.add_subparsers(
            dest='component',
            help='sos component to run'
        )
        self.subparsers.required = True
        # now build the parser for each component.
        # this needs to be done here, as otherwise --help will be unavailable
        # for the component subparsers
        for comp in self._components:
            _com_subparser = self.subparsers.add_parser(
                comp,
                aliases=self._components[comp][1]
            )
            _com_subparser.usage = "sos %s [options]" % comp
            _com_subparser.register('action', 'extend', SosListOption)
            self._add_common_options(_com_subparser)
            self._components[comp][0].add_parser_options(parser=_com_subparser)
            _com_subparser.set_defaults(component=comp)
        self.args = self.parser.parse_args(self.cmdline)
        self._init_component()

    def _add_common_options(self, parser):
        """Adds the options shared across components to the parser
        """
        parser.add_argument("--config-file", type=str, action="store",
                            dest="config_file", default="/etc/sos.conf",
                            help="specify alternate configuration file")
        parser.add_argument("--debug", action="store_true", dest="debug",
                            help="enable interactive debugging using the "
                            "python debugger")
        parser.add_argument("-q", "--quiet", action="store_true",
                            dest="quiet", default=False,
                            help="only print fatal errors")
        parser.add_argument("-s", "--sysroot", action="store", dest="sysroot",
                            help="system root directory path (default='/')",
                            default=None)
        parser.add_argument("--tmp-dir", action="store",
                            dest="tmp_dir",
                            help="specify alternate temporary directory",
                            default=None)
        parser.add_argument("-t", "--threads", action="store", dest="threads",
                            default=4, type=int,
                            help="Number of threads to use")
        parser.add_argument("-v", "--verbose", action="count",
                            dest="verbosity", default=0,
                            help="increase verbosity")

    def _init_component(self):
        """Determine which component has been requested by the user, and then
        initialize that component.
        """
        _com = self.args.component
        if _com not in self._components.keys():
            print("Unknown subcommand '%s' specified" % _com)
        try:
            self._component = self._components[_com][0](self.parser, self.args,
                                                        self.cmdline)
        except Exception as err:
            print("Could not initialize '%s': %s" % (_com, err))
            sys.exit(1)

    def execute(self):
        self._component.execute()
Example #15
0
def parse_args(argv=None):
    """
    Argument parsing routine.

    :param argv: A list of argument strings.
    :type argv: list

    :return: A parsed and verified arguments namespace.
    :rtype: :py:class:`argparse.Namespace`
    """

    parser = ArgumentParser(description=('Ninjecto - Ninja Injection Tool'))
    parser.register('action', 'extend', ExtendAction)

    # Standard options
    parser.add_argument(
        '-v',
        '--verbose',
        action='count',
        dest='verbosity',
        default=0,
        help='Increase verbosity level',
    )
    parser.add_argument(
        '--version',
        action='version',
        version='{} {}'.format(
            parser.description,
            __version__,
        ),
    )
    parser.add_argument('--no-color',
                        action='store_false',
                        dest='colorize',
                        help='Do not colorize the log output')

    # Configuration
    parser.add_argument(
        '-c',
        '--config',
        action='append',
        dest='configs',
        default=[],
        help=('Ninjecto and plugins configuration files. '
              'Must be a .toml, .yaml or .json. '
              'All files are parsed and merged left to right.'),
    )

    # Templates libraries
    parser.add_argument(
        '-l',
        '--library',
        action='append',
        dest='libraries',
        default=[],
        help=(
            'One or more paths to directories with a templates library. '
            'A library allows to inherit, import and other advanced '
            'templating features. All path are made available, load priority '
            'is left to right.'),
    )

    # Dry run
    parser.add_argument(
        '-d',
        '--dry-run',
        action='store_true',
        default=False,
        help='Dry run the pipeline',
    )

    # Values
    parser.add_argument('-a',
                        '--values',
                        nargs='+',
                        action='extend',
                        metavar='KEY=VALUE',
                        help='Values to render inputs with')
    parser.add_argument(
        '-u',
        '--values-file',
        action='append',
        dest='values_files',
        default=[],
        metavar='VALUES_FILE',
        help=('One or more paths to files with values to render inputs with. '
              'Must be a .toml, .yaml or .json'),
    )

    # Input and outputs
    parser.add_argument(
        '-f',
        '--force',
        action='store_true',
        dest='override',
        default=False,
        help='Override existing files',
    )
    parser.add_argument(
        '-r',
        '--levels',
        type=int,
        default=None,
        help='Limit recursion for directories to this number of levels',
    )

    parser.add_argument(
        '-o',
        '--output',
        action='store_true',
        default=None,
        help='Write generated file or directory to OUTPUT',
    )
    parser.add_argument(
        '-i',
        '--output-in',
        action='store_true',
        default=None,
        help='Write generated files in the OUTPUT directory',
    )
    parser.add_argument(
        '-p',
        '--parents',
        action='store_true',
        default=False,
        help='Create directory and its parents when using --output-in mode',
    )

    parser.add_argument(
        'source',
        metavar='SRC',
        help='File or directory to render',
    )
    parser.add_argument(
        'destination',
        metavar='DST',
        help='File or directory to write to',
    )

    # Parse and validate arguments
    args = parser.parse_args(argv)

    try:
        args = validate_args(args)
    except InvalidArguments as e:
        log.critical(e)
        raise e

    return args
Example #16
0
def parse_args(argv=None):
    """
    Argument parsing routine.

    :param list argv: A list of argument strings.

    :return: A parsed and verified arguments namespace.
    :rtype: :py:class:`argparse.Namespace`
    """

    parser = ArgumentParser(
        description=(
            'Topology is a framework for building and testing network '
            'topologies, with support for pytest.'
        )
    )
    parser.register('action', 'extend', ExtendAction)

    # Standard options
    parser.add_argument(
        '-v', '--verbose',
        help='Increase verbosity level',
        default=0,
        action='count'
    )
    parser.add_argument(
        '--version',
        action='version',
        version='Topology Framework v{}'.format(__version__)
    )

    # Platforms options
    parser.add_argument(
        '--platform',
        default=DEFAULT_PLATFORM,
        help='Platform engine to build the topology with',
        choices=platforms()
    )
    parser.add_argument(
        '-o', '--option',
        nargs='+',
        dest='options',
        action='extend',
        help='Platform options'
    )

    # Ploting options
    parser.add_argument(
        '--plot-dir',
        default=None,
        help=(
            'Obsolete. No longer supported. Used in previous versions of '
            'topology to specify the directory to autoplot topologies'
        )
    )
    parser.add_argument(
        '--plot-format',
        default='svg',
        help=(
            'Obsolete. No longer supported. Used in previous versions of '
            'topology to specify the plot format'
        )
    )

    # Export options
    parser.add_argument(
        '--nml-dir',
        default=None,
        help=(
            'Obsolete. No longer supported. Used in previous versions of '
            'topology to specify the directory to export topologies as NML XML'
        )
    )

    # Logging options
    parser.add_argument(
        '--log-dir',
        default=None,
        help='Directory to create log files'
    )
    parser.add_argument(
        '--show-build-commands',
        help='Show commands executed in nodes during build',
        action='store_true'
    )

    # Building options
    parser.add_argument(
        '--inject',
        default=None,
        help='Path to an attributes injection file'
    )
    parser.add_argument(
        '--non-interactive',
        help='Just build the topology and exit',
        action='store_true'
    )
    parser.add_argument(
        'topology',
        help='File with the topology description to build'
    )

    args = parser.parse_args(argv)
    args = validate_args(args)
    return args
Example #17
0
 def __init__(
         self,
         name: str,
         version: str,
         parser: ArgumentParser = None) -> None:
     if parser is None:
         parser = ArgumentParser(
             prog=name,
             formatter_class=ArgumentDefaultsHelpFormatter,
             add_help=True,
             allow_abbrev=False)
     parser.register('action', 'environment', _EnvDefault)
     parser.register('action', 'wantlist', _WantlistFile)
     mutually_exclusive = \
         parser.add_mutually_exclusive_group(required=False)
     parser.add_argument(
         '-c',
         '--currency',
         action='store',
         nargs='?',
         default='EUR',
         type=str,
         choices=[
             'AUD',
             'BRL',
             'CAD',
             'CHF',
             'EUR',
             'GBP',
             'JPY',
             'MXN',
             'NZD',
             'SEK',
             'USD',
             'ZAR'],
         help='currency for prices')
     mutually_exclusive.add_argument(
         '--debug',
         action='store_true',
         help='debug mode')
     parser.add_argument(
         '-d',
         '--details',
         action='store_true',
         help='exports extra details')
     parser.add_argument(
         '-f',
         '--file',
         action='store',
         nargs='?',
         default=DEFAULT_FILE_COLLECTION,
         type=str,
         help='output file name')
     parser.add_argument(
         '-p',
         '--prices',
         action='store_true',
         help='exports recommended prices')
     mutually_exclusive.add_argument(
         '-q',
         '--quiet',
         action='store_true',
         help='quiet mode')
     parser.add_argument(
         '-t',
         '--token',
         action='environment',
         nargs='?',
         env_var='DISCOGS_TOKEN',
         type=str,
         required=True,
         help=(
             'discogs token (can also be set usibg the DISCOGS_TOKEN '
             'environment variable)'))
     parser.add_argument(
         '-v',
         '--version',
         action='version',
         version=version)
     parser.add_argument(
         '-w',
         '--wantlist',
         action='wantlist',
         target='file',
         help='exports the wantlist instead of the collection')
     self._all = vars(parser.parse_args())
Example #18
0
def add_arguments(parser: argparse.ArgumentParser):
    """Build ArgumentParser."""
    assert isinstance(parser, argparse.ArgumentParser)

    parser.register("type", "bool", lambda v: v.lower() == "true")

    # Data
    parser.add_argument("--mode",
                        type=str,
                        default='train',
                        help=""""Specify the mode of program.Options include:
                        train: Training chatbot with train files.
                        sample: Select a sentence randomly from train source file and decode.
                        infer: Do inference with test source file, the results are saved to file 
                               "out_dir/infer_output".
                        eval: Inference and compute bleu score.
                        chat: Accept a input string and output a inference result."""
                        )
    parser.add_argument("--data_dir",
                        type=str,
                        default=None,
                        help="Data directory, e.g., en.")
    parser.add_argument("--src_suffix",
                        type=str,
                        default=None,
                        help="Source file suffix, e.g., de.")
    parser.add_argument("--tgt_suffix",
                        type=str,
                        default=None,
                        help="Target file suffix, e.g., de.")
    parser.add_argument("--train_prefix",
                        type=str,
                        default=None,
                        help="Train file prefix.")
    parser.add_argument("--dev_prefix",
                        type=str,
                        default=None,
                        help="Develop file prefix.")
    parser.add_argument("--test_prefix",
                        type=str,
                        default=None,
                        help="Test file prefix.")
    parser.add_argument("--vocab_prefix",
                        type=str,
                        default=None,
                        help="Vocab file prefix.")
    parser.add_argument("--embed_prefix",
                        type=str,
                        default=None,
                        help="Embed file prefix.")
    parser.add_argument("--out_dir",
                        type=str,
                        default=None,
                        help="Misc data output directory")

    # Network
    parser.add_argument("--embedding_size",
                        type=int,
                        default=128,
                        help="Word embedding size(word vector length)")
    parser.add_argument("--unit_type",
                        type=str,
                        default='lstm',
                        help="Optional: 'lstm', 'gru' or 'nas'")
    parser.add_argument("--num_units",
                        type=int,
                        default=32,
                        help="The number of hidden units(network size).")
    parser.add_argument("--forget_bias",
                        type=float,
                        default=1.0,
                        help="Forget bias for BasicLSTMCell.")
    parser.add_argument("--dropout",
                        type=float,
                        default=0.2,
                        help="Dropout rate (not keep_prob)")
    parser.add_argument("--encoder_type",
                        type=str,
                        default="uni",
                        help="""uni | bi | gnmt. For bi, we build num_layers/2 
                        bi-directional layers.For gnmt, we build 1 bi-directional layer, 
                        and (num_layers - 1) uni-directional layers.""")
    parser.add_argument("--num_encoder_layers",
                        type=int,
                        default=2,
                        help="The number of encoder layers(encoder depth).")
    parser.add_argument("--num_encoder_residual_layers",
                        type=int,
                        default=0,
                        nargs="?",
                        const=True,
                        help="The number of encoder's residual connections.")
    parser.add_argument("--num_decoder_layers",
                        type=int,
                        default=2,
                        help="The number of decoder layers(decoder depth).")
    parser.add_argument("--num_decoder_residual_layers",
                        type=int,
                        default=0,
                        nargs="?",
                        const=True,
                        help="The number of decoder's residual connections.")
    parser.add_argument("--time_major",
                        type="bool",
                        nargs="?",
                        const=True,
                        default=True,
                        help="Whether to use time-major mode for dynamic RNN.")

    # Attention mechanisms
    parser.add_argument("--attention_option",
                        type=str,
                        default="",
                        help="""\
      luong | scaled_luong | bahdanau | normed_bahdanau or set to "" for no attention"""
                        )
    parser.add_argument(
        "--output_attention",
        type="bool",
        nargs="?",
        const=True,
        default=True,
        help=
        """Only used in standard attention_architecture. Whether use attention as
                        the cell output at each timestep.""")
    parser.add_argument(
        "--pass_hidden_state",
        type="bool",
        nargs="?",
        const=True,
        default=True,
        help=
        """Whether to pass encoder's hidden state to decoder when using an attention
                        based model.""")

    # Train
    parser.add_argument("--num_train_steps",
                        type=int,
                        default=10000,
                        help="Num steps to train.")
    parser.add_argument("--batch_size",
                        type=int,
                        default=32,
                        help="Batch size for inference mode.")
    parser.add_argument("--optimizer",
                        type=str,
                        default="sgd",
                        help="sgd | rmsprop | adam")
    parser.add_argument("--learning_rate",
                        type=float,
                        default=1.0,
                        help="Learning rate. Adam: 0.001 | 0.0001")
    parser.add_argument("--decay_scheme",
                        type=str,
                        default="",
                        help="""How we decay learning rate. Options include:
                        luong234: after 2/3 num train steps, we start halving the learning rate
                        for 4 times before finishing.
                        luong10: after 1/2 num train steps, we start halving the learning rate
                        for 10 times before finishing.""")

    # initializer
    parser.add_argument("--var_initializer",
                        type=str,
                        default="uniform",
                        help="uniform | glorot_normal | glorot_uniform")
    parser.add_argument("--init_weight",
                        type=float,
                        default=0.1,
                        help=("for uniform init_op, initialize weights "
                              "between [-this, this]."))

    # Sequence lengths
    parser.add_argument("--src_max_len",
                        type=int,
                        default=50,
                        help="Max length of src sequences during training.")
    parser.add_argument("--tgt_max_len",
                        type=int,
                        default=50,
                        help="Max length of tgt sequences during training.")
    parser.add_argument("--src_max_len_infer",
                        type=int,
                        default=None,
                        help="Max length of src sequences during inference.")
    parser.add_argument("--tgt_max_len_infer",
                        type=int,
                        default=None,
                        help="""Max length of tgt sequences during inference.  
                        Also use to restrict the maximum decoding length.""")

    # Inference
    parser.add_argument("--beam_width",
                        type=int,
                        default=0,
                        help=("""beam width when using beam search decoder. 
                        If 0 (default), use standard decoder with greedy helper."""
                              ))
    parser.add_argument("--length_penalty_weight",
                        type=float,
                        default=0.0,
                        help="Length penalty for beam search.")
    parser.add_argument("--infer_batch_size",
                        type=int,
                        default=32,
                        help="Batch size for inference mode.")

    # Misc
    parser.add_argument("--sos",
                        type=str,
                        default="<s>",
                        help="Start-of-sentence symbol.")
    parser.add_argument("--eos",
                        type=str,
                        default="</s>",
                        help="End-of-sentence symbol.")
    parser.add_argument("--max_gradient_norm",
                        type=float,
                        default=5.0,
                        help="Clip gradients to this norm.")
    parser.add_argument("--num_buckets",
                        type=int,
                        default=5,
                        help="Put data into similar-length buckets.")
    parser.add_argument("--steps_per_stats",
                        type=int,
                        default=500,
                        help="How many training steps to do per logging.")
    parser.add_argument(
        "--share_vocab",
        type="bool",
        nargs="?",
        const=True,
        default=False,
        help="""Whether to use the source vocab and embeddings for both source 
                        and target.""")
    parser.add_argument("--random_seed",
                        type=int,
                        default=None,
                        help="Random seed (>0, set a specific seed).")
    parser.add_argument("--num_keep_ckpts",
                        type=int,
                        default=0,
                        help="Max number of checkpoints to save.")
Example #19
0
def parse_arguments(*unparsed_arguments):
    '''
    Given command-line arguments with which this script was invoked, parse the arguments and return
    them as a dict mapping from subparser name (or "global") to an argparse.Namespace instance.
    '''
    config_paths = collect.get_default_config_paths(expand_home=True)
    unexpanded_config_paths = collect.get_default_config_paths(expand_home=False)

    global_parser = ArgumentParser(add_help=False)
    global_parser.register('action', 'extend', Extend_action)
    global_group = global_parser.add_argument_group('global arguments')

    global_group.add_argument(
        '-c',
        '--config',
        nargs='*',
        dest='config_paths',
        default=config_paths,
        help='Configuration filenames or directories, defaults to: {}'.format(
            ' '.join(unexpanded_config_paths)
        ),
    )
    global_group.add_argument(
        '--excludes',
        dest='excludes_filename',
        help='Deprecated in favor of exclude_patterns within configuration',
    )
    global_group.add_argument(
        '-n',
        '--dry-run',
        dest='dry_run',
        action='store_true',
        help='Go through the motions, but do not actually write to any repositories',
    )
    global_group.add_argument(
        '-nc', '--no-color', dest='no_color', action='store_true', help='Disable colored output'
    )
    global_group.add_argument(
        '-v',
        '--verbosity',
        type=int,
        choices=range(-1, 3),
        default=0,
        help='Display verbose progress to the console (from only errors to very verbose: -1, 0, 1, or 2)',
    )
    global_group.add_argument(
        '--syslog-verbosity',
        type=int,
        choices=range(-1, 3),
        default=0,
        help='Log verbose progress to syslog (from only errors to very verbose: -1, 0, 1, or 2). Ignored when console is interactive or --log-file is given',
    )
    global_group.add_argument(
        '--log-file-verbosity',
        type=int,
        choices=range(-1, 3),
        default=0,
        help='Log verbose progress to log file (from only errors to very verbose: -1, 0, 1, or 2). Only used when --log-file is given',
    )
    global_group.add_argument(
        '--monitoring-verbosity',
        type=int,
        choices=range(-1, 3),
        default=0,
        help='Log verbose progress to monitoring integrations that support logging (from only errors to very verbose: -1, 0, 1, or 2)',
    )
    global_group.add_argument(
        '--log-file',
        type=str,
        default=None,
        help='Write log messages to this file instead of syslog',
    )
    global_group.add_argument(
        '--override',
        metavar='SECTION.OPTION=VALUE',
        nargs='+',
        dest='overrides',
        action='extend',
        help='One or more configuration file options to override with specified values',
    )
    global_group.add_argument(
        '--version',
        dest='version',
        default=False,
        action='store_true',
        help='Display installed version number of borgmatic and exit',
    )

    top_level_parser = ArgumentParser(
        description='''
            Simple, configuration-driven backup software for servers and workstations. If none of
            the action options are given, then borgmatic defaults to: prune, create, and check
            archives.
            ''',
        parents=[global_parser],
    )

    subparsers = top_level_parser.add_subparsers(
        title='actions',
        metavar='',
        help='Specify zero or more actions. Defaults to prune, create, and check. Use --help with action for details:',
    )
    init_parser = subparsers.add_parser(
        'init',
        aliases=SUBPARSER_ALIASES['init'],
        help='Initialize an empty Borg repository',
        description='Initialize an empty Borg repository',
        add_help=False,
    )
    init_group = init_parser.add_argument_group('init arguments')
    init_group.add_argument(
        '-e',
        '--encryption',
        dest='encryption_mode',
        help='Borg repository encryption mode',
        required=True,
    )
    init_group.add_argument(
        '--append-only',
        dest='append_only',
        action='store_true',
        help='Create an append-only repository',
    )
    init_group.add_argument(
        '--storage-quota',
        dest='storage_quota',
        help='Create a repository with a fixed storage quota',
    )
    init_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    prune_parser = subparsers.add_parser(
        'prune',
        aliases=SUBPARSER_ALIASES['prune'],
        help='Prune archives according to the retention policy',
        description='Prune archives according to the retention policy',
        add_help=False,
    )
    prune_group = prune_parser.add_argument_group('prune arguments')
    prune_group.add_argument(
        '--stats',
        dest='stats',
        default=False,
        action='store_true',
        help='Display statistics of archive',
    )
    prune_group.add_argument(
        '--files', dest='files', default=False, action='store_true', help='Show per-file details'
    )
    prune_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    create_parser = subparsers.add_parser(
        'create',
        aliases=SUBPARSER_ALIASES['create'],
        help='Create archives (actually perform backups)',
        description='Create archives (actually perform backups)',
        add_help=False,
    )
    create_group = create_parser.add_argument_group('create arguments')
    create_group.add_argument(
        '--progress',
        dest='progress',
        default=False,
        action='store_true',
        help='Display progress for each file as it is backed up',
    )
    create_group.add_argument(
        '--stats',
        dest='stats',
        default=False,
        action='store_true',
        help='Display statistics of archive',
    )
    create_group.add_argument(
        '--files', dest='files', default=False, action='store_true', help='Show per-file details'
    )
    create_group.add_argument(
        '--json', dest='json', default=False, action='store_true', help='Output results as JSON'
    )
    create_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    check_parser = subparsers.add_parser(
        'check',
        aliases=SUBPARSER_ALIASES['check'],
        help='Check archives for consistency',
        description='Check archives for consistency',
        add_help=False,
    )
    check_group = check_parser.add_argument_group('check arguments')
    check_group.add_argument(
        '--progress',
        dest='progress',
        default=False,
        action='store_true',
        help='Display progress for each file as it is checked',
    )
    check_group.add_argument(
        '--repair',
        dest='repair',
        default=False,
        action='store_true',
        help='Attempt to repair any inconsistencies found (experimental and only for interactive use)',
    )
    check_group.add_argument(
        '--only',
        metavar='CHECK',
        choices=('repository', 'archives', 'data', 'extract'),
        dest='only',
        action='append',
        help='Run a particular consistency check (repository, archives, data, or extract) instead of configured checks; can specify flag multiple times',
    )
    check_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    extract_parser = subparsers.add_parser(
        'extract',
        aliases=SUBPARSER_ALIASES['extract'],
        help='Extract files from a named archive to the current directory',
        description='Extract a named archive to the current directory',
        add_help=False,
    )
    extract_group = extract_parser.add_argument_group('extract arguments')
    extract_group.add_argument(
        '--repository',
        help='Path of repository to extract, defaults to the configured repository if there is only one',
    )
    extract_group.add_argument(
        '--archive', help='Name of archive to extract (or "latest")', required=True
    )
    extract_group.add_argument(
        '--path',
        '--restore-path',
        metavar='PATH',
        nargs='+',
        dest='paths',
        help='Paths to extract from archive, defaults to the entire archive',
    )
    extract_group.add_argument(
        '--destination',
        metavar='PATH',
        dest='destination',
        help='Directory to extract files into, defaults to the current directory',
    )
    extract_group.add_argument(
        '--strip-components',
        type=int,
        metavar='NUMBER',
        dest='strip_components',
        help='Number of leading path components to remove from each extracted path. Skip paths with fewer elements',
    )
    extract_group.add_argument(
        '--progress',
        dest='progress',
        default=False,
        action='store_true',
        help='Display progress for each file as it is extracted',
    )
    extract_group.add_argument(
        '-h', '--help', action='help', help='Show this help message and exit'
    )

    export_tar_parser = subparsers.add_parser(
        'export-tar',
        aliases=SUBPARSER_ALIASES['export-tar'],
        help='Export an archive to a tar-formatted file or stream',
        description='Export an archive to a tar-formatted file or stream',
        add_help=False,
    )
    export_tar_group = export_tar_parser.add_argument_group('export-tar arguments')
    export_tar_group.add_argument(
        '--repository',
        help='Path of repository to export from, defaults to the configured repository if there is only one',
    )
    export_tar_group.add_argument(
        '--archive', help='Name of archive to export (or "latest")', required=True
    )
    export_tar_group.add_argument(
        '--path',
        metavar='PATH',
        nargs='+',
        dest='paths',
        help='Paths to export from archive, defaults to the entire archive',
    )
    export_tar_group.add_argument(
        '--destination',
        metavar='PATH',
        dest='destination',
        help='Path to destination export tar file, or "-" for stdout (but be careful about dirtying output with --verbosity or --files)',
        required=True,
    )
    export_tar_group.add_argument(
        '--tar-filter', help='Name of filter program to pipe data through'
    )
    export_tar_group.add_argument(
        '--files', default=False, action='store_true', help='Show per-file details'
    )
    export_tar_group.add_argument(
        '--strip-components',
        type=int,
        metavar='NUMBER',
        dest='strip_components',
        help='Number of leading path components to remove from each exported path. Skip paths with fewer elements',
    )
    export_tar_group.add_argument(
        '-h', '--help', action='help', help='Show this help message and exit'
    )

    mount_parser = subparsers.add_parser(
        'mount',
        aliases=SUBPARSER_ALIASES['mount'],
        help='Mount files from a named archive as a FUSE filesystem',
        description='Mount a named archive as a FUSE filesystem',
        add_help=False,
    )
    mount_group = mount_parser.add_argument_group('mount arguments')
    mount_group.add_argument(
        '--repository',
        help='Path of repository to use, defaults to the configured repository if there is only one',
    )
    mount_group.add_argument('--archive', help='Name of archive to mount (or "latest")')
    mount_group.add_argument(
        '--mount-point',
        metavar='PATH',
        dest='mount_point',
        help='Path where filesystem is to be mounted',
        required=True,
    )
    mount_group.add_argument(
        '--path',
        metavar='PATH',
        nargs='+',
        dest='paths',
        help='Paths to mount from archive, defaults to the entire archive',
    )
    mount_group.add_argument(
        '--foreground',
        dest='foreground',
        default=False,
        action='store_true',
        help='Stay in foreground until ctrl-C is pressed',
    )
    mount_group.add_argument('--options', dest='options', help='Extra Borg mount options')
    mount_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    umount_parser = subparsers.add_parser(
        'umount',
        aliases=SUBPARSER_ALIASES['umount'],
        help='Unmount a FUSE filesystem that was mounted with "borgmatic mount"',
        description='Unmount a mounted FUSE filesystem',
        add_help=False,
    )
    umount_group = umount_parser.add_argument_group('umount arguments')
    umount_group.add_argument(
        '--mount-point',
        metavar='PATH',
        dest='mount_point',
        help='Path of filesystem to unmount',
        required=True,
    )
    umount_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    restore_parser = subparsers.add_parser(
        'restore',
        aliases=SUBPARSER_ALIASES['restore'],
        help='Restore database dumps from a named archive',
        description='Restore database dumps from a named archive. (To extract files instead, use "borgmatic extract".)',
        add_help=False,
    )
    restore_group = restore_parser.add_argument_group('restore arguments')
    restore_group.add_argument(
        '--repository',
        help='Path of repository to restore from, defaults to the configured repository if there is only one',
    )
    restore_group.add_argument(
        '--archive', help='Name of archive to restore from (or "latest")', required=True
    )
    restore_group.add_argument(
        '--database',
        metavar='NAME',
        nargs='+',
        dest='databases',
        help='Names of databases to restore from archive, defaults to all databases. Note that any databases to restore must be defined in borgmatic\'s configuration',
    )
    restore_group.add_argument(
        '-h', '--help', action='help', help='Show this help message and exit'
    )

    list_parser = subparsers.add_parser(
        'list',
        aliases=SUBPARSER_ALIASES['list'],
        help='List archives',
        description='List archives or the contents of an archive',
        add_help=False,
    )
    list_group = list_parser.add_argument_group('list arguments')
    list_group.add_argument(
        '--repository',
        help='Path of repository to list, defaults to the configured repository if there is only one',
    )
    list_group.add_argument('--archive', help='Name of archive to list (or "latest")')
    list_group.add_argument(
        '--path',
        metavar='PATH',
        nargs='+',
        dest='paths',
        help='Paths to list from archive, defaults to the entire archive',
    )
    list_group.add_argument(
        '--short', default=False, action='store_true', help='Output only archive or path names'
    )
    list_group.add_argument('--format', help='Format for file listing')
    list_group.add_argument(
        '--json', default=False, action='store_true', help='Output results as JSON'
    )
    list_group.add_argument(
        '-P', '--prefix', help='Only list archive names starting with this prefix'
    )
    list_group.add_argument(
        '-a', '--glob-archives', metavar='GLOB', help='Only list archive names matching this glob'
    )
    list_group.add_argument(
        '--successful',
        default=False,
        action='store_true',
        help='Only list archive names of successful (non-checkpoint) backups',
    )
    list_group.add_argument(
        '--sort-by', metavar='KEYS', help='Comma-separated list of sorting keys'
    )
    list_group.add_argument(
        '--first', metavar='N', help='List first N archives after other filters are applied'
    )
    list_group.add_argument(
        '--last', metavar='N', help='List last N archives after other filters are applied'
    )
    list_group.add_argument(
        '-e', '--exclude', metavar='PATTERN', help='Exclude paths matching the pattern'
    )
    list_group.add_argument(
        '--exclude-from', metavar='FILENAME', help='Exclude paths from exclude file, one per line'
    )
    list_group.add_argument('--pattern', help='Include or exclude paths matching a pattern')
    list_group.add_argument(
        '--patterns-from',
        metavar='FILENAME',
        help='Include or exclude paths matching patterns from pattern file, one per line',
    )
    list_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    info_parser = subparsers.add_parser(
        'info',
        aliases=SUBPARSER_ALIASES['info'],
        help='Display summary information on archives',
        description='Display summary information on archives',
        add_help=False,
    )
    info_group = info_parser.add_argument_group('info arguments')
    info_group.add_argument(
        '--repository',
        help='Path of repository to show info for, defaults to the configured repository if there is only one',
    )
    info_group.add_argument('--archive', help='Name of archive to show info for (or "latest")')
    info_group.add_argument(
        '--json', dest='json', default=False, action='store_true', help='Output results as JSON'
    )
    info_group.add_argument(
        '-P', '--prefix', help='Only show info for archive names starting with this prefix'
    )
    info_group.add_argument(
        '-a',
        '--glob-archives',
        metavar='GLOB',
        help='Only show info for archive names matching this glob',
    )
    info_group.add_argument(
        '--sort-by', metavar='KEYS', help='Comma-separated list of sorting keys'
    )
    info_group.add_argument(
        '--first',
        metavar='N',
        help='Show info for first N archives after other filters are applied',
    )
    info_group.add_argument(
        '--last', metavar='N', help='Show info for last N archives after other filters are applied'
    )
    info_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')

    arguments = parse_subparser_arguments(unparsed_arguments, subparsers)
    arguments['global'] = parse_global_arguments(unparsed_arguments, top_level_parser, subparsers)

    if arguments['global'].excludes_filename:
        raise ValueError(
            'The --excludes option has been replaced with exclude_patterns in configuration'
        )

    if 'init' in arguments and arguments['global'].dry_run:
        raise ValueError('The init action cannot be used with the --dry-run option')

    if 'list' in arguments and arguments['list'].glob_archives and arguments['list'].successful:
        raise ValueError('The --glob-archives and --successful options cannot be used together')

    if (
        'list' in arguments
        and 'info' in arguments
        and arguments['list'].json
        and arguments['info'].json
    ):
        raise ValueError('With the --json option, list and info actions cannot be used together')

    return arguments
Example #20
0
def get_argparse():
    layout_choices = sorted(
        i for i in pathmaker.__all__
        if not i.startswith('_') and callable(getattr(pathmaker, i)))

    parser = ArgumentParser(
        description='ExplosiveFUSE: Explode compressed files into a '
        'filesystem, carefully.')
    parser.register('action', 'version_verbose', _Version)
    parser.register('action', 'layout_help', _LayoutHelp)
    parser.register('action', 'pathmaker_store', _PathmakerChoiceStoreAction)

    parser.add_argument('-l',
                        '--layout',
                        dest='pathmaker',
                        action='pathmaker_store',
                        metavar='<strategy>',
                        default=pathmaker.default(),
                        help="Directory layout presentation strategy.  "
                        "Available strategies are: '" +
                        "', '".join(layout_choices) + "'. "
                        "If unspecified, the default is '%(default)s'.")
    parser.add_argument(
        '--layout-info',
        action='layout_help',
        help='More detailed information on the usage of layout presentation '
        'strategy (such as extra arguments).')
    parser.add_argument('-d',
                        '--debug',
                        dest='debug',
                        action='store_true',
                        help='Run with debug messages.')
    parser.add_argument('-f',
                        '--foreground',
                        dest='foreground',
                        action='store_true',
                        help='Run in foreground.')
    parser.add_argument(
        '-m',
        '--manager',
        dest='manager',
        action='store_true',
        help='Enable the symlink manager directory, where all the archives '
        'indexed and available within this ExplosiveFUSE instance are '
        'exposed as symlinks.  These symlinks can be removed to remove '
        'the associated files from the filesystem, and new symlinks to '
        'other archives can be created to add them to the filesystem.')
    parser.add_argument(
        '--manager-dir',
        dest='manager_dir',
        nargs='?',
        default='.manager',
        help='Explictly define the name of the symlink manager directory. '
        "Default is '%(default)s'.")
    parser.add_argument(
        '--overwrite',
        dest='overwrite',
        action='store_true',
        help='Newly added entries will overlay existing entries of a given '
        'name for a name collision, as if overwriting the existing '
        'version. Default behavior to keep the oldest added file in '
        'that place until its source archive is removed, then the next '
        'one added with that name then be presented there.')
    parser.add_argument(
        '--omit-arcname',
        dest='include_arcname',
        action='store_false',
        help='Omit the basename of the origin archive from the generated '
        'paths.')
    parser.add_argument('-V',
                        '--version',
                        action='version_verbose',
                        help='Print version information and exit.')
    parser.add_argument(
        'dir', help='The directory to mount the compressed archive(s) to.')
    parser.add_argument(
        'archives',
        metavar='archives',
        nargs='+',
        help='The archive(s) to generate directory structures with')

    return parser
Example #21
0
def get_argparse():
    layout_choices = sorted(
        i for i in pathmaker.__all__
        if not i.startswith('_') and callable(getattr(pathmaker, i))
    )

    parser = ArgumentParser(
        description='ExplosiveFUSE: Explode compressed files into a '
                    'filesystem, carefully.'
    )
    parser.register('action', 'version_verbose', _Version)
    parser.register('action', 'layout_help', _LayoutHelp)
    parser.register('action', 'pathmaker_store', _PathmakerChoiceStoreAction)

    parser.add_argument(
        '-l', '--layout', dest='pathmaker', action='pathmaker_store',
        metavar='<strategy>', default=pathmaker.default(),
        help="Directory layout presentation strategy.  "
             "Available strategies are: '" +
             "', '".join(layout_choices) + "'. "
             "If unspecified, the default is '%(default)s'."
    )
    parser.add_argument(
        '--layout-info', action='layout_help',
        help='More detailed information on the usage of layout presentation '
             'strategy (such as extra arguments).')
    parser.add_argument(
        '-d', '--debug', dest='debug', action='store_true',
        help='Run with debug messages.')
    parser.add_argument(
        '-f', '--foreground', dest='foreground', action='store_true',
        help='Run in foreground.')
    parser.add_argument(
        '-m', '--manager', dest='manager', action='store_true',
        help='Enable the symlink manager directory, where all the archives '
             'indexed and available within this ExplosiveFUSE instance are '
             'exposed as symlinks.  These symlinks can be removed to remove '
             'the associated files from the filesystem, and new symlinks to '
             'other archives can be created to add them to the filesystem.')
    parser.add_argument(
        '--manager-dir', dest='manager_dir', nargs='?', default='.manager',
        help='Explictly define the name of the symlink manager directory. '
             "Default is '%(default)s'.")
    parser.add_argument(
        '--overwrite', dest='overwrite', action='store_true',
        help='Newly added entries will overlay existing entries of a given '
             'name for a name collision, as if overwriting the existing '
             'version. Default behavior to keep the oldest added file in '
             'that place until its source archive is removed, then the next '
             'one added with that name then be presented there.')
    parser.add_argument(
        '--omit-arcname', dest='include_arcname', action='store_false',
        help='Omit the basename of the origin archive from the generated '
             'paths.')
    parser.add_argument(
        '-V', '--version', action='version_verbose',
        help='Print version information and exit.')
    parser.add_argument(
        'dir',
        help='The directory to mount the compressed archive(s) to.')
    parser.add_argument(
        'archives', metavar='archives', nargs='+',
        help='The archive(s) to generate directory structures with')

    return parser
Example #22
0
def make_argument_parser(require_src: bool) -> ArgumentParser:
    """Create the argument parser object

    :param require_src: ``True`` to require at least one path as a positional argument
                        on the command line. ``False`` to not require on.

    """
    description = [
        "Re-format Python source files by using",
        "- `isort` to sort Python import definitions alphabetically within logical"
        " sections",
        "- `black` to re-format code changed since the last Git commit",
    ]
    try:
        import isort
    except ImportError:
        isort = None
        description.extend([
            "", f"{ISORT_INSTRUCTION} to enable sorting of import definitions"
        ])
    parser = ArgumentParser(
        description="\n".join(description),
        formatter_class=NewlinePreservingFormatter,
    )
    parser.register("action", "log_level", LogLevelAction)
    parser.add_argument(
        "src",
        nargs="+" if require_src else "*",
        help="Path(s) to the Python source file(s) to reformat",
        metavar="PATH",
    )
    parser.add_argument(
        "-r",
        "--revision",
        default="HEAD",
        help=
        ("Git revision against which to compare the working tree. Tags, branch"
         " names, commit hashes, and other expressions like HEAD~5 work here. Also"
         " a range like master...HEAD or master... can be used to compare the best"
         " common ancestor."),
    )
    isort_help = ["Also sort imports using the `isort` package"]
    if not isort:
        isort_help.append(
            f". {ISORT_INSTRUCTION} to enable usage of this option.")
    parser.add_argument(
        "--diff",
        action="store_true",
        help=
        ("Don't write the files back, just output a diff for each file on stdout."
         " Highlight syntax on screen if the `pygments` package is available."
         ),
    )
    parser.add_argument(
        "--check",
        action="store_true",
        help=
        ("Don't write the files back, just return the status.  Return code 0 means"
         " nothing would change.  Return code 1 means some files would be"
         " reformatted."),
    )
    parser.add_argument(
        "-i",
        "--isort",
        action="store_true",
        help="".join(isort_help),
    )
    parser.add_argument(
        "-L",
        "--lint",
        action="append",
        metavar="CMD",
        default=[],
        help=
        ("Also run a linter on changed files. CMD can be a name of path of the "
         "linter binary, or a full quoted command line"),
    )
    parser.add_argument(
        "-c",
        "--config",
        metavar="PATH",
        help="Ask `black` and `isort` to read configuration from PATH.",
    )
    parser.add_argument(
        "-v",
        "--verbose",
        dest="log_level",
        action="log_level",
        const=-10,
        help="Show steps taken and summarize modifications",
    )
    parser.add_argument(
        "-q",
        "--quiet",
        dest="log_level",
        action="log_level",
        const=10,
        help="Reduce amount of output",
    )
    parser.add_argument(
        "--version",
        action="version",
        version=__version__,
        help="Show the version of `darker`",
    )
    parser.add_argument(
        "-S",
        "--skip-string-normalization",
        action="store_const",
        const=True,
        dest="skip_string_normalization",
        help="Don't normalize string quotes or prefixes",
    )
    parser.add_argument(
        "--no-skip-string-normalization",
        action="store_const",
        const=False,
        dest="skip_string_normalization",
        help=(
            "Normalize string quotes or prefixes. This can be used to override"
            " `skip_string_normalization = true` from a configuration file."),
    )
    parser.add_argument(
        "-l",
        "--line-length",
        type=int,
        dest="line_length",
        help="How many characters per line to allow [default: 88]",
    )
    return parser