Ejemplo n.º 1
0
def _parse(args):
    options = {}
    cmdoptions = {}
    try:
        args = parseopts(args, commands.globalopts, options)
    except getopt.GetoptError as e:
        raise CommandLineError(str(e))

    if args:
        cmd, args = args[0], args[1:]
        if cmd in commands.table:
            cmdopts = list(commands.table[cmd][1])
        else:
            cmdopts = []
    else:
        cmd = "help"
        cmdopts = list(commands.table[cmd][1])

    for opt in commands.globalopts:
        cmdopts.append((opt[0], opt[1], options[opt[1]], opt[3]))

    try:
        args = parseopts(args, cmdopts, cmdoptions)
    except getopt.GetoptError as e:
        raise CommandLineError((cmd, e))

    for opt in cmdoptions.keys():
        if opt in options:
            options[opt] = cmdoptions[opt]
            del cmdoptions[opt]

    return cmd, options, cmdoptions, args
Ejemplo n.º 2
0
def _parse(args):
    options = {}
    cmdoptions = {}
    try:
        args = parseopts(args, commands.globalopts, options)
    except getopt.GetoptError, e:
        raise CommandLineError(str(e))
Ejemplo n.º 3
0
def test_dispatch_CLIError(_dispatch, logger):
    '''
    Test case for CommandLineError
    '''
    args = ['strange']
    _dispatch.side_effect = CommandLineError('some error')

    assert dispatch.dispatch(args) == -1
    _dispatch.assert_called_with(args)
Ejemplo n.º 4
0
def _dispatch(args):
    conf = Config()

    # update commands
    for mod in conf.extensions:
        cmdtable = getattr(mod, 'cmdtable', {})
        commands.table.update(cmdtable)

    cmd, globalopts, opts, args = _parse(args)

    if globalopts["help"]:
        del globalopts["help"]
        return commands.usage(conf, *args, **globalopts)
    elif globalopts["version"]:
        del globalopts["version"]
        return commands.version(conf, *args, **globalopts)

    verbose = 2
    if globalopts["debug"]:
        verbose = 1
        import restkit
        restkit.set_logging("debug")
    elif globalopts["verbose"]:
        verbose = 1
    elif globalopts["quiet"]:
        verbose = 0

    set_logging_level(verbose)
    if cmd is None:
        raise CommandLineError("unknown command")

    fun = commands.table[cmd][0]
    if cmd in commands.incouchapp:
        return fun(conf, conf.app_dir, *args, **opts)

    return fun(conf, *args, **opts)
Ejemplo n.º 5
0
        cmd, args = args[0], args[1:]
        if cmd in commands.table:
            cmdopts = list(commands.table[cmd][1])
        else:
            cmdopts = []
    else:
        cmd = "help"
        cmdopts = list(commands.table[cmd][1])

    for opt in commands.globalopts:
        cmdopts.append((opt[0], opt[1], options[opt[1]], opt[3]))

    try:
        args = parseopts(args, cmdopts, cmdoptions)
    except getopt.GetoptError, e:
        raise CommandLineError((cmd, e))

    for opt in cmdoptions.keys():
        if opt in options:
            options[opt] = cmdoptions[opt]
            del cmdoptions[opt]

    return cmd, options, cmdoptions, args


def parseopts(args, options, state):
    namelist = []
    shortlist = ''
    argmap = {}
    defmap = {}