From 3168633077dc297878ef72b7e4feaa0c365cb73c Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Thu, 19 Feb 2015 22:01:11 +0100 Subject: lib-general/formats/command: special "magic_split" flavor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... that allows specifying multi-argument option as a single-argument one with '::' being a logical separator between such args. This is needed, for instance, when multi-argument option is specified last to differentiate between such option argument and proper command argument (only a single argument for an option is normally expected). Also add a respective unit test. Signed-off-by: Jan Pokorný --- ext-plugins/lib-general/formats/command.py | 3 +++ tests/formats/command.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/ext-plugins/lib-general/formats/command.py b/ext-plugins/lib-general/formats/command.py index 3774cba..04a49ad 100644 --- a/ext-plugins/lib-general/formats/command.py +++ b/ext-plugins/lib-general/formats/command.py @@ -42,6 +42,9 @@ class command(SimpleFormat): if acc: ret.append(tuple(acc)) acc = [] if i is None else [i] + elif self._dict.get('magic_split', False): + acc.extend(i.split('::')) # magic "::"-split + merged.append(None) else: acc.append(i) # expect that, by convention, option takes at most a single argument diff --git a/tests/formats/command.py b/tests/formats/command.py index 46982a8..1223556 100644 --- a/tests/formats/command.py +++ b/tests/formats/command.py @@ -7,6 +7,10 @@ __author__ = "Jan Pokorný " from os.path import join, dirname as d; execfile(join(d(d((__file__))), '_go')) +try: + from collections import OrderedDict +except ImportError: + from ordereddict import OrderedDict from unittest import TestCase from .format_manager import FormatManager @@ -58,4 +62,15 @@ class FormatsCommandTestCase(TestCase): ['cut', '-f', '1', '-d', '@', 'emails.txt']) + def testMagicBytestringToDict(self): + c = command('bytestring', + 'gpg -k 60BCBB4F5CD7F9EF::~/.gnupg/pubring.gpg', + magic_split=True) + #print c('merged') + self.assertEqual(c.DICT(), + OrderedDict([('__cmd__', ['gpg']), + ('-k', [('60BCBB4F5CD7F9EF', + '~/.gnupg/pubring.gpg')])])) + + from os.path import join, dirname as d; execfile(join(d(d(__file__)), '_gone')) -- cgit