Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update arguments parser.

  • Loading branch information...
commit 9ee45428cf71fe3b2773a35c85e80a75eed96d46 1 parent bb341ab
@GrAndSE authored
Showing with 52 additions and 11 deletions.
  1. +3 −3 lighty/commands.py
  2. +7 −8 lighty/utils.py
  3. +42 −0 tests/optparse.py
View
6 lighty/commands.py
@@ -49,10 +49,10 @@ def parse_args(command_arg_type=None, args=default_args):
parser.add_option('config', 'c', default=default_conf, flag=False,
help='configuration file')
if command_arg_type:
- parser.add_argument('command', type=command_arg_type,
+ parser.add_argument('command', type=command_arg_type, default='',
optional=False, help='command to execute')
else:
- parser.add_argument('command', optional=False,
+ parser.add_argument('command',default='',
help='command to execute')
for args, kwargs in args:
parser.add_argument(*args, **kwargs)
@@ -70,7 +70,7 @@ def parse_args(command_arg_type=None, args=default_args):
# Try to get command name
def error_msg(msg):
- raise TypeError('%s. Commands available:\n\t%s\n' % (msg,
+ raise TypeError('%s. Commands available:\n\t%s' % (msg,
"\n\t".join(sorted(commands.keys()))))
def command_name(name):
View
15 lighty/utils.py
@@ -155,20 +155,19 @@ def _parse(self, args_for_parse):
for alias in self.options:
option = self.options[alias]
if option.name not in result:
- if option.default:
- result[option.name] = option.default
+ if option.default or option.default == '':
+ result[option.name] = option.type(option.default)
elif not option.optional:
- raise LookupError(option.name + ' value missed')
+ raise LookupError('Option "%s" value missed' % option.name)
# Arguments
index = 0
for argument in self.arguments:
if index >= len(args):
# Check for default value if there is no value specified
- if argument.optional:
- if argument.default:
- result[argument.name] = argument.default
- else:
- raise LookupError(argument.name + ' value missed')
+ if argument.default or argument.default == '':
+ result[argument.name] = argument.type(argument.default)
+ elif not argument.optional:
+ raise LookupError('Argument "%s" missed' % argument.name)
else:
if argument.nargs > 1:
ni = index + argument.nargs
View
42 tests/optparse.py
@@ -0,0 +1,42 @@
+'''Test case for command line parsing
+'''
+import unittest
+
+from lighty.utils import CommandParser
+
+
+class OptParseTestCase(unittest.TestCase):
+ '''Test case used to check command line parser
+ '''
+
+ def setUp(self):
+ self.parser = CommandParser(program='test', description='parser test')
+
+ def testFlagOption(self):
+ '''Test option parser with one flag option
+ '''
+ self.parser.add_option('h', optional=True, flag=True, help='help')
+ args = self.parser._parse([])
+ assert args == {}, 'Error parsing empty args: %s except {}' % args
+ args = self.parser.parse(['-h'])
+ assert args == {'h': ''}, 'Error parsing one arg: %s' % args
+
+ def testValueOption(self):
+ '''Test option parser with values option
+ '''
+ self.parser.add_option('h', optional=True, flag=False, help='help')
+ args = self.parser._parse([])
+ assert args == {}, 'Error parsing empty args: %s except {}' % args
+ args = self.parser.parse(['-h=test'])
+ assert args == {'h': 'test'}, 'Error parsing one arg: %s' % args
+ args = self.parser.parse(['-h', 'test'])
+ assert args == {'h': 'test'}, 'Error parsing one arg: %s' % args
+ args = self.parser.parse(['-h', 'test', 'test'])
+ assert args == {'h': 'test'}, 'Error parsing two arg: %s' % args
+
+
+def test():
+ suite = unittest.TestSuite()
+ suite.addTest(OptParseTestCase('testFlagOption'))
+ suite.addTest(OptParseTestCase('testValueOption'))
+ return suite
Please sign in to comment.
Something went wrong with that request. Please try again.