Revision 8d427cb9

b/kamaki/cli/argument/__init__.py
289 289
    :syntax: --<arg> key1=value1 --<arg> key2=value2 ...
290 290
    """
291 291

  
292
    def __init__(self, help='', parsed_name=None, default=[]):
292
    def __init__(self, help='', parsed_name=None, default={}):
293 293
        super(KeyValueArgument, self).__init__(-1, help, parsed_name, default)
294 294

  
295 295
    @property
296 296
    def value(self):
297 297
        """
298
        :input: key=value
299
        :output: {'key1':'value1', 'key2':'value2', ...}
298
        :returns: (dict) {key1: val1, key2: val2, ...}
300 299
        """
301 300
        return super(KeyValueArgument, self).value
302 301

  
303 302
    @value.setter
304 303
    def value(self, keyvalue_pairs):
304
        """
305
        :param keyvalue_pairs: (str) ['key1=val1', 'key2=val2', ...]
306
        """
305 307
        self._value = {}
306 308
        for pair in keyvalue_pairs:
307 309
            key, sep, val = pair.partition('=')
......
309 311
                raiseCLIError(
310 312
                    CLISyntaxError('Argument syntax error '),
311 313
                    details='%s is missing a "=" (usage: key1=val1 )\n' % pair)
312
            self._value[key.strip()] = val.strip()
314
            self._value[key] = val
313 315

  
314 316

  
315 317
class ProgressBarArgument(FlagArgument):
b/kamaki/cli/argument/test.py
41 41
from kamaki.cli.config import Config
42 42

  
43 43

  
44
def assert_dicts_are_equal(test_case, d1, d2):
45
    for k, v in d1.items():
46
        test_case.assertTrue(k in d2)
47
        if isinstance(v, dict):
48
            test_case.assert_dicts_are_equal(v, d2[k])
49
        else:
50
            test_case.assertEqual(unicode(v), unicode(d2[k]))
51

  
52

  
44 53
cnf_path = 'kamaki.cli.config.Config'
45 54

  
46 55

  
......
272 281
            self.assertRaises(err, da.format_date, datestr)
273 282

  
274 283

  
284
class VersionArgument(TestCase):
285

  
286
    def test_value(self):
287
        va = argument.VersionArgument(parsed_name='--version')
288
        self.assertTrue(va, argument.VersionArgument)
289
        va.value = 'some value'
290
        self.assertEqual(va.value, 'some value')
291

  
292

  
293
class KeyValueArgument(TestCase):
294

  
295
    @patch('kamaki.cli.argument.Argument.__init__')
296
    def test___init__(self, init):
297
        help, pname, default = 'help', 'pname', 'default'
298
        kva = argument.KeyValueArgument(help, pname, default)
299
        self.assertTrue(isinstance(kva, argument.KeyValueArgument))
300
        self.assertEqual(init.mock_calls[-1], call(-1, help, pname, default))
301

  
302
    def test_value(self):
303
        kva = argument.KeyValueArgument(parsed_name='--keyval')
304
        self.assertEqual(kva.value, None)
305
        for kvpairs in (
306
                'strval', 'key=val', 2.8, 42, None,
307
                ('key', 'val'), ('key val'), ['=val', 'key=val'],
308
                ['key1=val1', 'key2 val2'], ('key1 = val1', )):
309
            self.assertRaises(errors.CLIError, kva.value, kvpairs)
310
        for kvpairs, exp in (
311
                (('key=val', ), {'key': 'val'}),
312
                (['key1=val1', 'key2=val2'], {'key1': 'val1', 'key2': 'val2'}),
313
                (
314
                    ('k1=v1 v2', 'k3=', 'k 4=v4'),
315
                    {'k1': 'v1 v2', 'k3': '', 'k 4': 'v4'}),
316
                (('k=v1', 'k=v2', 'k=v3'), {'k': 'v3'})
317
            ):
318
            kva.value = kvpairs
319
            assert_dicts_are_equal(self, kva.value, exp)
320

  
321

  
275 322
if __name__ == '__main__':
276 323
    from sys import argv
277 324
    from kamaki.cli.test import runTestCase
......
282 329
    runTestCase(FlagArgument, 'ValueArgument', argv[1:])
283 330
    runTestCase(IntArgument, 'IntArgument', argv[1:])
284 331
    runTestCase(DateArgument, 'DateArgument', argv[1:])
332
    runTestCase(VersionArgument, 'VersionArgument', argv[1:])
333
    runTestCase(KeyValueArgument, 'KeyValueArgument', argv[1:])
b/kamaki/cli/test.py
37 37
from kamaki.cli.command_tree.test import Command, CommandTree
38 38
from kamaki.cli.argument.test import (
39 39
    Argument, ConfigArgument, RuntimeConfigArgument, FlagArgument,
40
    ValueArgument, IntArgument, DateArgument)
40
    ValueArgument, IntArgument, DateArgument, VersionArgument,
41
    KeyValueArgument)
41 42

  
42 43

  
43 44
#  TestCase auxiliary methods

Also available in: Unified diff