예제 #1
0
    def add(self, *a, **kw):
        """Add a flag, subcommand, or middleware to this Command.

        If the first argument is a callable, this method contructs a
        Command from it and the remaining arguments, all of which are
        optional. See the Command docs for for full details on names
        and defaults.

        If the first argument is a string, this method constructs a
        Flag from that flag string and the rest of the method
        arguments, all of which are optional. See the Flag docs for
        more options.

        If the argument is already an instance of Flag or Command, an
        exception is only raised on conflicting subcommands and
        flags. See add_command for details.

        Middleware is only added if it is already decorated with
        @face_middleware. Use .add_middleware() for automatic wrapping
        of callables.

        """
        # TODO: need to check for middleware provides names + flag names
        # conflict

        target = a[0]

        if is_middleware(target):
            return self.add_middleware(target)

        subcmd = a[0]
        if not isinstance(subcmd,
                          Command) and callable(subcmd) or subcmd is None:
            subcmd = Command(*a, **kw)  # attempt to construct a new subcmd

        if isinstance(subcmd, Command):
            self.add_command(subcmd)
            return subcmd

        flag = a[0]
        if not isinstance(flag, Flag):
            flag = Flag(*a, **kw)  # attempt to construct a Flag from arguments
        super(Command, self).add(flag)

        return flag
예제 #2
0
파일: helpers.py 프로젝트: mahmoud/face
import os
import sys
import array
import textwrap

from boltons.iterutils import unique, split

from face.utils import format_flag_label, format_flag_post_doc, format_posargs_label, echo
from face.parser import Flag

DEFAULT_HELP_FLAG = Flag('--help',
                         parse_as=True,
                         char='-h',
                         doc='show this help message and exit')
DEFAULT_MAX_WIDTH = 120


def _get_termios_winsize():
    # TLPI, 62.9 (p. 1319)
    import fcntl
    import termios

    winsize = array.array('H', [0, 0, 0, 0])

    assert not fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ, winsize)

    ws_row, ws_col, _, _ = winsize

    return ws_row, ws_col

예제 #3
0
    cmd.add(verbose_mw)

    cmd.add(subtract, doc='', posargs=float)

    cmd.add(print_args, 'print', '', posargs=True)

    cmd.add('--loop-count', parse_as=int)

    return cmd.run()  # execute


from face.parser import Flag


@face_middleware(flags=[Flag('--verbose', char='-V', parse_as=True)])
def verbose_mw(next_, verbose):
    if verbose:
        print('starting in verbose mode')
    ret = next_()
    if verbose:
        print('complete')
    return ret


@face_middleware(provides=['stdout', 'stderr'], optional=True)
def output_streams_mw(next_):
    return next_(stdout=sys.stdout, stderr=sys.stderr)


if __name__ == '__main__':