Пример #1
0
    def main(self, raw_args=None):
        '''Start to parse the raw arguments and send them to a
        :py:class:`~clime.core.Command` instance.

        :param raw_args: The arguments from command line. By default, it takes from ``sys.argv``.
        :type raw_args: list
        '''

        if raw_args is None:
            raw_args = sys.argv[1:]
        elif isinstance(raw_args, str):
            raw_args = raw_args.split()

        # try to find a command name in the raw arguments.
        cmd_name = None
        cmd_func = None

        if len(raw_args) == 0:
            pass
        elif not self.ignore_help and raw_args[0] in ('--help', '-h'):
            self.print_usage()
            return
        else:
            cmd_func = self.command_funcs.get(raw_args[0].replace('-', '_'))
            if cmd_func is not None:
                cmd_name = raw_args.pop(0).replace('-', '_')

        if cmd_func is None:
            # we can't find a command name in normal procedure
            if self.default:
                cmd_name = cmd_name
                cmd_func = self.command_funcs[self.default]
            else:
                self.print_usage()
                return

        if not self.ignore_help and '--help' in raw_args:
            # the user requires help of this command
            self.print_usage(cmd_name)
            return

        # convert the function to a Command object
        cmd = Command(cmd_func, cmd_name)

        try:
            # execute the command with the raw arguments
            return_val = cmd.execute(raw_args)
        except BaseException, e:
            if self.debug:
                from traceback import print_exception
                print_exception(*sys.exc_info())
            else:
                self.complain(e)
            sys.exit(1)
Пример #2
0
    def main(self, raw_args=None):
        '''Start to parse the raw arguments and send them to a
        :py:class:`~clime.core.Command` instance.

        :param raw_args: The arguments from command line. By default, it takes from ``sys.argv``.
        :type raw_args: list
        '''

        if raw_args is None:
            raw_args = sys.argv[1:]
        elif isinstance(raw_args, str):
            raw_args = raw_args.split()

        # try to find a command name in the raw arguments.
        cmd_name = None
        cmd_func = None

        if len(raw_args) == 0:
            pass
        elif not self.ignore_help and raw_args[0] in ('--help', '-h'):
            self.print_usage()
            return
        else:
            cmd_func = self.command_funcs.get(raw_args[0].replace('-', '_'))
            if cmd_func is not None:
                cmd_name = raw_args.pop(0).replace('-', '_')

        if cmd_func is None:
            # we can't find a command name in normal procedure
            if self.default:
                cmd_name = cmd_name
                cmd_func = self.command_funcs[self.default]
            else:
                self.print_usage()
                return

        if not self.ignore_help and '--help' in raw_args:
            # the user requires help of this command
            self.print_usage(cmd_name)
            return

        # convert the function to a Command object
        cmd = Command(cmd_func, cmd_name)

        try:
            # execute the command with the raw arguments
            return_val = cmd.execute(raw_args)
        except BaseException, e:
            if self.debug:
                from traceback import print_exception
                print_exception(*sys.exc_info())
            else:
                self.complain(e)
            sys.exit(1)
Пример #3
0
 def append_usage(cmd_name, without_name=False):
     # nonlocal usages
     cmd_func = self.command_funcs[cmd_name]
     usages.append(
         Command(cmd_func, cmd_name).build_usage(without_name))
Пример #4
0
    Use :py:func:`start` instead.
'''

if __name__ == '__main__':

    import doctest
    doctest.testmod()

    def read_json(json=None):
        '''
        options:
            --json=<json>
        '''
        return json

    read_json_cmd = Command(read_json)

    print '---'
    print read_json_cmd.build_usage()
    print read_json_cmd.execute('[1,2,3]')
    print read_json_cmd.execute(['--json', '{"x": 1}'])
    print '---'

    prog = Program(white_list=['read_json'], debug=True)
    prog.main()
    # python -m clime.core read-json --help
    # python -m clime.core read-json '{"x": 1}'
    # python -m clime.core read-json --json='{"x":1}'

########NEW FILE########
__FILENAME__ = now
Пример #5
0
    Use :py:func:`start` instead.
'''

if __name__ == '__main__':

    import doctest
    doctest.testmod()

    def read_json(json=None):
        '''
        options:
            --json=<json>
        '''
        return json

    read_json_cmd = Command(read_json)

    print '---'
    print read_json_cmd.build_usage()
    print read_json_cmd.execute('[1,2,3]')
    print read_json_cmd.execute(['--json', '{"x": 1}'])
    print '---'

    prog = Program(white_list=['read_json'], debug=True)
    prog.main()
    # python -m clime.core read-json --help
    # python -m clime.core read-json '{"x": 1}'
    # python -m clime.core read-json --json='{"x":1}'

########NEW FILE########
__FILENAME__ = now