# Path: torext/errors.py
# class CommandArgumentError(TorextException):
#     pass
# 
# Path: torext/script.py
# class Command(object):
#     allow_types = (int, float, str, str, bool)
# 
#     def __init__(self, func, profile=False):
#         self.func = func
#         self.profile_flag = profile
# 
#         spec = inspect.getargspec(func)
# 
#         argnames = (spec.args or [])[:]
#         defaults = list(spec.defaults or [])
# 
#         self.parameters = argnames[:len(argnames) - len(defaults)]
#         self.keyword_parameters = dict(list(zip(argnames[- len(defaults):], defaults)))
# 
#         # Check defined keyword parameters
#         for k, v in self.keyword_parameters.items():
#             self._get_value_type(v)
# 
#         self.has_varargs = bool(spec.varargs)
#         self.has_kwargs = bool(spec.keywords)
# 
#         if func.__doc__:
#             doc = func.__doc__
#             if '\n' in doc:
#                 doc = ' '.join(i.strip() for i in doc.split('\n'))
#         else:
#             doc = "Command '%s' in manage script" % func.__name__
#         self.doc = doc
# 
#     def parse_args(self, all_args=None):
#         if all_args is not None:
#             all_args = all_args[:]
#         else:
#             all_args = sys.argv[2:]
# 
#         _kw_pos = []
#         for loop, i in enumerate(all_args):
#             if i.startswith('--'):
#                 _kw_pos.append(loop)
# 
#         if _kw_pos:
#             # Check positions
#             all_args_len = len(all_args)
#             _fixed_kw_pos = _kw_pos + [all_args_len]
#             for loop, i in enumerate(_fixed_kw_pos):
#                 if i == all_args_len:
#                     continue
#                 if _fixed_kw_pos[loop + 1] - i != 2:
#                     raise CommandArgumentError(
#                         'Invalid arguments: %s should have one and only one value' % all_args[i])
# 
#             # Get args and keyword_args
#             args = all_args[:_kw_pos[0]]
#             #keyword_args = {all_args[i][2:]: all_args[i + 1] for i in _kw_pos}
#             keyword_args = dict((all_args[i][2:], all_args[i + 1]) for i in _kw_pos)
#         else:
#             args = all_args
#             keyword_args = {}
# 
#         # Check args
#         if len(args) < len(self.parameters):
#             raise CommandArgumentError('Arguments too little')
#         else:
#             if not self.has_varargs and\
#                len(args) > len(self.parameters) + len(self.keyword_parameters):
#                 raise CommandArgumentError('Arguments too much')
# 
#         # Check keyword args
#         if not self.has_kwargs:
#             for i in keyword_args:
#                 if not i in self.keyword_parameters:
#                     raise CommandArgumentError('Unknown keyword parameter %s' % i)
# 
#         # Convert keyword args type
#         # NOTE As keyword arguments passed as arguments are allwed,
#         # and only recognized keyword arguments will be converted,
#         # so keyword arguments passed as arguments will not be converted.
#         for i in keyword_args:
#             if i in self.keyword_parameters:
#                 default = self.keyword_parameters[i]
#                 converted = self._convert_to_type(
#                     keyword_args[i], self._get_value_type(default))
#                 keyword_args[i] = converted
# 
#         return args, keyword_args
# 
#     def _get_value_type(self, value):
#         if isinstance(value, self.allow_types):
#             typ = type(value)
#         elif value is None:
#             typ = None
#         else:
#             raise TypeError('Parameters for command function can only be type of %s' % self.allow_types)
#         return typ
# 
#     def _convert_to_type(self, source, typ):
#         if typ in (int, float, str, str):
#             try:
#                 v = typ(source)
#             except ValueError:
#                 raise CommandArgumentError("'%s' could not be converted to %s"
#                                            % (source, typ))
#         elif typ == bool:
#             if source in ('True', '1'):
#                 v = True
#             elif source in ('False', '0'):
#                 v = False
#             else:
#                 raise CommandArgumentError("'%s' could not be converted to bool type" % source)
#         elif typ is None:
#             v = source
# 
#         return v
# 
#     def execute(self, all_args=()):
#         if not all_args:
#             return self.func()
#         else:
#             args, keyword_args = self.parse_args(all_args)
#             return self.func(*args, **keyword_args)


from torext.errors import CommandArgumentError
from torext.script import Command
from nose.tools import assert_raises

    c = Command(pri)
    assert c.parameters == ['a', 'b']
    assert c.keyword_parameters == {'c': True, 'd': 3}

    with assert_raises(CommandArgumentError):
        c.parse_args([])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a'])

    c.parse_args(['v a', 'v b'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c'])

    c.parse_args(['v a', 'v b', '--c', '1'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '1', '2'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '1', '2', '--d'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '--d'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '--d', '3'])


# Path: torext/errors.py
# class CommandArgumentError(TorextException):
#     pass
# 
# Path: torext/script.py
# class Command(object):
#     allow_types = (int, float, str, str, bool)
# 
#     def __init__(self, func, profile=False):
#         self.func = func
#         self.profile_flag = profile
# 
#         spec = inspect.getargspec(func)
# 
#         argnames = (spec.args or [])[:]
#         defaults = list(spec.defaults or [])
# 
#         self.parameters = argnames[:len(argnames) - len(defaults)]
#         self.keyword_parameters = dict(list(zip(argnames[- len(defaults):], defaults)))
# 
#         # Check defined keyword parameters
#         for k, v in self.keyword_parameters.items():
#             self._get_value_type(v)
# 
#         self.has_varargs = bool(spec.varargs)
#         self.has_kwargs = bool(spec.keywords)
# 
#         if func.__doc__:
#             doc = func.__doc__
#             if '\n' in doc:
#                 doc = ' '.join(i.strip() for i in doc.split('\n'))
#         else:
#             doc = "Command '%s' in manage script" % func.__name__
#         self.doc = doc
# 
#     def parse_args(self, all_args=None):
#         if all_args is not None:
#             all_args = all_args[:]
#         else:
#             all_args = sys.argv[2:]
# 
#         _kw_pos = []
#         for loop, i in enumerate(all_args):
#             if i.startswith('--'):
#                 _kw_pos.append(loop)
# 
#         if _kw_pos:
#             # Check positions
#             all_args_len = len(all_args)
#             _fixed_kw_pos = _kw_pos + [all_args_len]
#             for loop, i in enumerate(_fixed_kw_pos):
#                 if i == all_args_len:
#                     continue
#                 if _fixed_kw_pos[loop + 1] - i != 2:
#                     raise CommandArgumentError(
#                         'Invalid arguments: %s should have one and only one value' % all_args[i])
# 
#             # Get args and keyword_args
#             args = all_args[:_kw_pos[0]]
#             #keyword_args = {all_args[i][2:]: all_args[i + 1] for i in _kw_pos}
#             keyword_args = dict((all_args[i][2:], all_args[i + 1]) for i in _kw_pos)
#         else:
#             args = all_args
#             keyword_args = {}
# 
#         # Check args
#         if len(args) < len(self.parameters):
#             raise CommandArgumentError('Arguments too little')
#         else:
#             if not self.has_varargs and\
#                len(args) > len(self.parameters) + len(self.keyword_parameters):
#                 raise CommandArgumentError('Arguments too much')
# 
#         # Check keyword args
#         if not self.has_kwargs:
#             for i in keyword_args:
#                 if not i in self.keyword_parameters:
#                     raise CommandArgumentError('Unknown keyword parameter %s' % i)
# 
#         # Convert keyword args type
#         # NOTE As keyword arguments passed as arguments are allwed,
#         # and only recognized keyword arguments will be converted,
#         # so keyword arguments passed as arguments will not be converted.
#         for i in keyword_args:
#             if i in self.keyword_parameters:
#                 default = self.keyword_parameters[i]
#                 converted = self._convert_to_type(
#                     keyword_args[i], self._get_value_type(default))
#                 keyword_args[i] = converted
# 
#         return args, keyword_args
# 
#     def _get_value_type(self, value):
#         if isinstance(value, self.allow_types):
#             typ = type(value)
#         elif value is None:
#             typ = None
#         else:
#             raise TypeError('Parameters for command function can only be type of %s' % self.allow_types)
#         return typ
# 
#     def _convert_to_type(self, source, typ):
#         if typ in (int, float, str, str):
#             try:
#                 v = typ(source)
#             except ValueError:
#                 raise CommandArgumentError("'%s' could not be converted to %s"
#                                            % (source, typ))
#         elif typ == bool:
#             if source in ('True', '1'):
#                 v = True
#             elif source in ('False', '0'):
#                 v = False
#             else:
#                 raise CommandArgumentError("'%s' could not be converted to bool type" % source)
#         elif typ is None:
#             v = source
# 
#         return v
# 
#     def execute(self, all_args=()):
#         if not all_args:
#             return self.func()
#         else:
#             args, keyword_args = self.parse_args(all_args)
#             return self.func(*args, **keyword_args)


# Path: torext/test/script_test.py
from torext.errors import CommandArgumentError
from torext.script import Command
from nose.tools import assert_raises


    c = Command(pri)
    assert c.parameters == ['a', 'b']
    assert c.keyword_parameters == {'c': True, 'd': 3}

    with assert_raises(CommandArgumentError):
        c.parse_args([])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a'])

    c.parse_args(['v a', 'v b'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c'])

    c.parse_args(['v a', 'v b', '--c', '1'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '1', '2'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '1', '2', '--d'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '--d'])

    with assert_raises(CommandArgumentError):
        c.parse_args(['v a', 'v b', '--c', '--d', '3'])


    with assert_raises(CommandArgumentError):