Skip to content

Commit

Permalink
Fix handling of command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm committed Mar 5, 2018
1 parent 1cb8382 commit 31e2e33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions pycinga/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class which encapsulates a single plugin. This is the class
which should be subclassed when creating new plugins.
"""

import sys
from argparse import ArgumentError, ArgumentParser
from copy import copy

Expand Down Expand Up @@ -88,8 +87,15 @@ class Plugin(with_metaclass(PluginMeta, object)):
# TODO: Still missing version

responses = []
_options = None

def __init__(self, args=sys.argv):
@property
def options(self):
if self._options is None:
self._options = self._option_parser.parse_args(self.args)
return self._options

def __init__(self, args=None):
"""
Instantiates a plugin, setting up the options and arguments state.
Initialization by itself shouldn't do much, since the plugin should run
Expand Down Expand Up @@ -127,7 +133,7 @@ class MyPlugin(Plugin):
argument being available in ``options.your_name``.
"""
# Parse the given arguments to set the options
self.options = self._option_parser.parse_args(args)
self.args = args

def check(self):
"""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def test_plugin_parses_sys_argv(self, monkeypatch):
"""
# Update the argv in-place since the default arguments
# are evaluated at "compile" time of Python
del sys.argv[:]
if len(sys.argv) > 1:
del sys.argv[1:]
sys.argv.extend(["-H", "foo.com"])

plugin = self.Klass()
Expand Down

0 comments on commit 31e2e33

Please sign in to comment.