mirror of
https://github.com/beetbox/beets.git
synced 2025-12-24 01:25:47 +01:00
Add vararg_callback utility function to beets.ui
Optparse doesn't support argparse nargs='+' style arguments. This patch adds a callback utility function that allows that idiom. The function is taken from the page at http://docs.python.org/2/library/optparse.html#callback-example-6-variable-arguments. Here's an example of how to use it: from beets.ui import vararg_callback parser.add_option("-c", "--callback", dest="vararg_attr", action="callback", callback=vararg_callback)
This commit is contained in:
parent
aad0449f56
commit
7299ed24c6
3 changed files with 33 additions and 0 deletions
|
|
@ -695,6 +695,33 @@ class SubcommandsOptionParser(optparse.OptionParser):
|
|||
return options, subcommand, suboptions, subargs
|
||||
|
||||
|
||||
# callback for an option with variable arguments
|
||||
# parser.add_option("-c", "--callback", dest="vararg_attr",
|
||||
# action="callback", callback=vararg_callback)
|
||||
|
||||
def vararg_callback(option, opt_str, value, parser):
|
||||
assert value is None
|
||||
value = []
|
||||
|
||||
def floatable(str):
|
||||
try:
|
||||
float(str)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
for arg in parser.rargs:
|
||||
# stop on --foo like options
|
||||
if arg[:2] == "--" and len(arg) > 2:
|
||||
break
|
||||
# stop on -a, but not on -3 or -3.0
|
||||
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
|
||||
break
|
||||
value.append(arg)
|
||||
|
||||
del parser.rargs[:len(value)]
|
||||
setattr(parser.values, option.dest, value)
|
||||
|
||||
# The root parser and its main function.
|
||||
|
||||
def _raw_main(args):
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@ Changelog
|
|||
|
||||
New features:
|
||||
|
||||
* :ref:`add_subcommands`: Added ``optparse`` callback utility function, allowing
|
||||
plugin developers to add options with ``action=callback`` and
|
||||
``callback=beets.ui.varargs_callback`` and a variable number of arguments.
|
||||
|
||||
* :doc:`/plugins/duplicates`: The new ``keys`` option allows you to specify
|
||||
arbitrary fields over which to consider potential duplicates.
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,8 @@ containing your plugin).
|
|||
|
||||
.. _virtualenv: http://pypi.python.org/pypi/virtualenv
|
||||
|
||||
.. _add_subcommands:
|
||||
|
||||
Add Commands to the CLI
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue