Permalink
Browse files

Allow commands to be defined declaratively

  • Loading branch information...
1 parent aef4221 commit cb7d7976ee187e920c52b7d27a186c3d6beb659a @DasIch committed Apr 1, 2012
Showing with 25 additions and 0 deletions.
  1. +11 −0 awwparse/__init__.py
  2. +14 −0 awwparse/testsuite/init.py
View
@@ -6,8 +6,11 @@
:copyright: 2012 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details
"""
+from __future__ import absolute_import
import sys
import textwrap
+from types import MethodType
+from functools import partial
from collections import deque
from six import u
@@ -194,10 +197,18 @@ def __init__(self):
force_list(self.__class__.arguments)
)
self.parent = None
+
signature = Signature.from_method(self.main)
if signature.annotations:
self._populate_from_signature(self, signature)
+ for name in dir(self):
+ attribute = getattr(self, name)
+ if isinstance(attribute, Command):
+ if not isinstance(attribute.main, MethodType):
+ attribute.main = partial(attribute.main, self)
+ self.add_command(name, attribute)
+
@property
def option_prefixes(self):
"""
View
@@ -186,6 +186,20 @@ def main(self, a: Integer(), b: Integer()):
self.assert_equal(Foo().run(["1", "1"]), 2)
"""
+ def test_declarative(self):
+ results = []
+ class Foo(Command):
+ arguments = Argument(Integer(), "a")
+
+ def main(self, a):
+ results.append(a)
+
+ class Bar(Command):
+ foo = Foo()
+
+ Bar().run(["foo", "1"])
+ self.assert_equal(results, [1])
+
def test_option_shorts_and_longs(self):
command = Command()
command.add_option("foo", Option("a", String()))

0 comments on commit cb7d797

Please sign in to comment.