ソースを参照

Merge pull request #2435 from sontek/allow_append_args

This makes action=append with --arg 1 --arg 2 work
Ask Solem Hoel 10 年 前
コミット
e65c6e1b4a
2 ファイル変更32 行追加2 行削除
  1. 16 2
      celery/bin/base.py
  2. 16 0
      celery/tests/bin/test_base.py

+ 16 - 2
celery/bin/base.py

@@ -505,6 +505,14 @@ class Command(object):
     def parse_preload_options(self, args):
         return self.preparse_options(args, self.preload_options)
 
+    def add_append_opt(self, acc, opt, value):
+        default = opt.default or []
+
+        if opt.dest not in acc:
+           acc[opt.dest] = default
+
+        acc[opt.dest].append(value)
+
     def preparse_options(self, args, options):
         acc = {}
         opts = {}
@@ -520,13 +528,19 @@ class Command(object):
                     key, value = arg.split('=', 1)
                     opt = opts.get(key)
                     if opt:
-                        acc[opt.dest] = value
+                        if opt.action == 'append':
+                            self.add_append_opt(acc, opt, value)
+                        else:
+                            acc[opt.dest] = value
                 else:
                     opt = opts.get(arg)
                     if opt and opt.takes_value():
                         # optparse also supports ['--opt', 'value']
                         # (Issue #1668)
-                        acc[opt.dest] = args[index + 1]
+                        if opt.action == 'append':
+                            self.add_append_opt(acc, opt, args[index + 1])
+                        else:
+                            acc[opt.dest] = args[index + 1]
                         index += 1
                     elif opt and opt.action == 'store_true':
                         acc[opt.dest] = True

+ 16 - 0
celery/tests/bin/test_base.py

@@ -314,3 +314,19 @@ class test_Command(AppCase):
         cmd.preload_options = (Option('-s', action='store', dest='silent'), )
         acc = cmd.parse_preload_options(['-s', 'yes'])
         self.assertEqual(acc.get('silent'), 'yes')
+
+    def test_parse_preload_options_with_equals_and_append(self):
+        cmd = Command()
+        opt = Option('--zoom', action='append', default=[])
+        cmd.preload_options = (opt,)
+        acc = cmd.parse_preload_options(['--zoom=1', '--zoom=2'])
+
+        self.assertEqual(acc, {'zoom': ['1', '2']})
+
+    def test_parse_preload_options_without_equals_and_append(self):
+        cmd = Command()
+        opt = Option('--zoom', action='append', default=[])
+        cmd.preload_options = (opt,)
+        acc = cmd.parse_preload_options(['--zoom', '1', '--zoom', '2'])
+
+        self.assertEqual(acc, {'zoom': ['1', '2']})