def parse_args(args):
    usage = ('usage: %prog service [service params] [general params] '
             '[resource|param, ...] method [param, ...]')
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('--verbose', '-v', dest='verbose',
                      action='count', default=0)
    parser.add_option('--executor', '-x', dest='executor', action='store')

    if len(args) < 2:
        parser.error('not enough arguments')

    service = args[1]

    # hack to detect if the first argument is -h or --help, since we don't call
    # parse_args before adding the service-specific parameters
    if service in ('-h', '--help'):
        parser.print_help()
        parser.exit()

    try:
        module = __import__('libsaas.services', globals(), locals(), [service])
        module = getattr(module, service)
    except (__HOLE__, AttributeError):
        parser.error('no such service %s' % service)

    members = inspect.getmembers(module, lambda obj: inspect.isclass(obj) and
                                 issubclass(obj, base.Resource))
    if not members:
        parser.error('no such service %s' % service)

    _, klass = members[0]

    # got the service class, inspect its __init__ method to extract the keyword
    # arguments
    argspec = inspect.getargspec(klass.__init__)
    for argname, default in zip(reversed(argspec.args),
                                 chain(reversed(argspec.defaults or ()),
                                       repeat(None))):
        if argname == 'self':
            continue
        optargs = {'dest': argname}
        if default is not None:
            optargs.update({'default': default})
        parser.add_option('--%s' % argname, **optargs)

    options, args = parser.parse_args(args)

    if options.executor:
        try:
            module_name = '{0}_executor'.format(options.executor)
            module = __import__('libsaas.executors', globals(), locals(),
                                [module_name])
            module = getattr(module, module_name)
        except ImportError:
            parser.error('no such executor %s' % options.executor)

        module.use()

    level = logging.ERROR
    if options.verbose > 1:
        level = logging.DEBUG
    elif options.verbose > 0:
        level = logging.INFO

    logging.basicConfig(level=level)

    del options.verbose
    del options.executor

    if len(args) < 2:
        parser.error('not enough arguments')

    # instantiate the class, get the resource
    instance = klass(**options.__dict__)
    action, args = extract_action(instance, parser, args[2:])
    pprint.pprint(action(*list(map(try_interpret_arg, args))))
dataset/ETHPy150Open ducksboard/libsaas/libsaas/scripts/saas.py/parse_args