Skip to content

Commit

Permalink
Added attribute setting and getting for options (Issue #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
thasso committed Jan 21, 2014
1 parent d5561c3 commit b822c93
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions jip/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,19 @@ def __getitem__(self, name):
return self.options[i]
return None

def __getattr__(self, name):
i = self.__index(name)
if i >= 0:
return self.options[i]
return object.__getattr__(self, name)

def __setattr__(self, name, value):
i = self.__index(name)
if i >= 0:
self.options[i].set(value)
else:
object.__setattr__(self, name, value)

def __setitem__(self, name, option):
i = self.__index(name)
if isinstance(option, Option):
Expand Down
28 changes: 28 additions & 0 deletions test/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,31 @@ def test_docopt_parser_with_tabs():
assert opts['cmd'] is not None
assert opts['cmd'].nargs == 1
assert opts['cmd'].required


def test_options_setting_as_property():
help_string = """\
Some Tool
Usage: tools --name <input>
Options:
-n, --name <name> The input
"""
opts = Options.from_docopt(help_string)
assert opts is not None

opts['name'].set("Test")
assert opts['name'].get() == "Test"
assert opts.name.get() == "Test"
assert opts.name == "Test"

opts['name'] = "Test2"
assert opts['name'].get() == "Test2"
assert opts.name.get() == "Test2"
assert opts.name == "Test2"

opts.name = 'Test3'
assert opts['name'].get() == "Test3"
assert opts.name.get() == "Test3"
assert opts.name == "Test3"

0 comments on commit b822c93

Please sign in to comment.