Skip to content
Snippets Groups Projects
Commit 03fa1a63 authored by ulif's avatar ulif
Browse files

Allow plugins to work with the argparser.

If plugins want to work with their own commandline args, we allow
this by calling their `update_argparser` methods. This has to be a
classmethod, if it exists.
parent 29f16960
No related branches found
No related tags found
No related merge requests found
......@@ -79,7 +79,8 @@ def get_random_sources():
def handle_options(args):
"""Handle commandline options.
"""
random_sources = get_random_sources().keys()
plugins = get_random_sources()
random_sources = plugins.keys()
wordlist_names = get_wordlist_names()
defaults = get_config_dict()
parser = argparse.ArgumentParser(
......@@ -127,6 +128,9 @@ def handle_options(args):
'--version', action='store_true',
help='output version information and exit.',
)
for plugin in plugins.values():
if hasattr(plugin, "update_argparser"):
parser = plugin.update_argparser(parser)
parser.set_defaults(**defaults)
args = parser.parse_args(args)
return args
......
......@@ -147,6 +147,18 @@ class TestHandleOptions(object):
assert options.delimiter == "my-delim"
assert options.caps is False
def test_handle_options_allows_plugins_updating(self, monkeypatch):
# we allow plugins to update our argparser, before being used
import diceware
class FakePlugin(object):
@classmethod
def update_argparser(cls, parser):
parser.add_argument('--foo', default=2, type=int)
return parser
monkeypatch.setattr(diceware, 'get_random_sources', lambda: dict(foo=FakePlugin))
options = handle_options([])
assert options.foo == 2
class TestDicewareModule(object):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment