Esempio n. 1
0
 def test_boolean_true(self):
     """
     Passing in --option results in option being true
     """
     simple = caparg.command(
         '', caparg.command('eat', alot=caparg.option(type=bool)))
     self.assertTrue(simple.parse(['eat', '--alot'])['alot'])
Esempio n. 2
0
 def test_boolean_false(self):
     """
     Not passing in --option results in option being false
     """
     simple = caparg.command(
         '', caparg.command('eat', alot=caparg.option(type=bool)))
     self.assertFalse(simple.parse(['eat'])['alot'])
Esempio n. 3
0
 def test_no_required(self):
     """
     Not passing in a required argument raises ParseError
     """
     command = caparg.command
     simple = command(
         '', command('eat', where=caparg.option(type=str, required=True)))
     with self.assertRaises(caparg.ParseError):
         simple.parse(['eat'])
Esempio n. 4
0
 def test_optional(self):
     """
     Not passing in an optional argument causes nothing to be in the return
     """
     simple = caparg.command(
         '', caparg.command('eat', what=caparg.option(type=str)))
     parsed = dict(simple.parse(['eat']))
     self.assertEqual(list(parsed.pop('__caparg_subcommand__')), ['eat'])
     self.assertEqual(parsed, {})
Esempio n. 5
0
 def test_inherit(self):
     """
     Subcommands inherit options from parents and grandparents
     """
     simple = caparg.command('',
                             caparg.options(where=caparg.option(type=str)),
                             caparg.command('eat', caparg.command('lunch')))
     parsed = dict(simple.parse(['eat', 'lunch', '--where', 'cafe']))
     self.assertEqual(list(parsed.pop('__caparg_subcommand__')),
                      ['eat', 'lunch'])
     self.assertEqual(parsed.pop('where'), 'cafe')
     self.assertEqual(parsed, {})
Esempio n. 6
0
    """
    output(foo, lili)


@COMMANDS.command(dependencies=['foo', 'print'],
                  aliases=['view'],
                  parser=ca.command('', ca.positional('lala', type=str)))
def show(args, dependencies):
    """
    Print then arguments.
    """
    dependencies['print'](args, "BREAK", dependencies)


@COMMANDS.command(dependencies=['bar', 'print'],
                  parser=ca.command('', wooo=ca.option(type=str)))
def gowoo(args, dependencies):
    """
    Print 'woo' and then arguments.
    """
    dependencies['print']("woo", args, dependencies)


@COMMANDS.command(dependencies=['print'],
                  parser=ca.command('',
                                    foo=ca.option(type=str),
                                    bar=ca.option(type=str, required=True)))
def interesting_args(args, dependencies):
    """
    Print arguments.
    """
Esempio n. 7
0
    return os.environ


@COMMANDS.dependency(dependencies=['environment', 'current_directory'])
def secret_filename(dependencies, _maybedeps):
    """
    The filename to put the encrypted secrets in.
    """
    if 'VOYNICH_FILE' in dependencies['environment']:
        return dependencies['environment']['VOYNICH_FILE']
    return os.path.join(dependencies['current_directory'], 'voynich.json')


@COMMANDS.command(dependencies=['secret_filename'],
                  parser=ca.command('',
                                    key_file=ca.option(type=str,
                                                       required=True)))
def create(args, dependencies):
    """
    Create a new secrets file (and save the private key).
    """
    sys.stdout.write("writing key to {} and public data to {}\n".format(
        args.key_file,
        dependencies['secret_filename']))


@COMMANDS.command(dependencies=['secret_filename'],
                  parser=ca.command('',
                                    name=ca.option(type=str, required=True),
                                    value=ca.option(type=str, required=True)))
def encrypt(args, dependencies):
    """
Esempio n. 8
0
"""
Demonstration of middlefield plugins
"""

from __future__ import print_function

import typing

from caparg import command, option
import middlefield


@middlefield.COMMANDS.command(parser=command('',
                                             what=option(type=typing.List[str],
                                                         have_default=True)),
                              dependencies=['echo_printer'])
def echo(args, dependencies):
    """
    Print out arguments
    """
    dependencies['echo_printer'](*args['what'])


@middlefield.COMMANDS.dependency()
def echo_printer(_dependencies, _maybe_dependencies):
    """
    Define what printing means
    """
    return print
Esempio n. 9
0

@contextlib.contextmanager
def tmpdir():
    """
    Context manager creating a temporary directory
    """
    ret = tempfile.mkdtemp()
    try:
        yield ret
    finally:
        shutil.rmtree(ret)


@COMMANDS.command(parser=command('',
                                 requirements=option(type=typing.List[str],
                                                     have_default=True),
                                 package=option(type=typing.List[str],
                                                have_default=True),
                                 shebang=option(type=str),
                                 output=option(type=str, required=True)),
                  dependencies=['executor', 'pex_builder'],
                  aliases=['self build'])
def self_build(args, dependencies):
    """
    Build middlefield, together with any plugins, into a Pex file
    """
    package = list(args['package'])
    my_version = _version.__version__.short()
    package.append('middlefield=={}'.format(my_version))
    requirements = list(args['requirements'])
    output = args['output']
Esempio n. 10
0
"""
Really complicated command-line parser definition.
"""

import typing

from caparg import command, options, option, positional

PARSER = command(
    '',
    # These are common options.
    # They will be inherited by all sub-commands.
    options(
        messages=option(type=str, required=True),
        config=option(type=str, required=True),
    ),
    # This is a subcommand, "add"
    # It takes a bewildering array of options!
    command('add',
            name=option(type=str, required=True),
            cmd=option(type=str, required=True),
            arg=option(type=typing.List[str], have_default=True),
            env=option(type=typing.Dict[str, str], have_default=True),
            uid=option(type=int),
            gid=option(type=int),
            extras=option(type=str)),
    # This is a subcommand, "remove"
    # It takes one option
    command('remove', name=option(type=str, required=True)),
    # This is a subcommand, "restart"
    # It takes one option