Skip to content

Commit

Permalink
This makes action=append with --arg 1 --arg 2 work
Browse files Browse the repository at this point in the history
  • Loading branch information
sontek authored and ask committed Feb 12, 2015
1 parent 9482c78 commit 6e8388c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
18 changes: 16 additions & 2 deletions celery/bin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ def process_cmdline_config(self, argv):
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 = {}
Expand All @@ -519,13 +527,19 @@ def preparse_options(self, args, options):
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
Expand Down
16 changes: 16 additions & 0 deletions celery/tests/bin/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,19 @@ def test_parse_preload_options_shortopt(self):
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']})

0 comments on commit 6e8388c

Please sign in to comment.