Skip to content

Commit

Permalink
Allow commands to be defined declaratively
Browse files Browse the repository at this point in the history
  • Loading branch information
DasIch committed Apr 1, 2012
1 parent aef4221 commit cb7d797
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions awwparse/__init__.py
Expand Up @@ -6,8 +6,11 @@
:copyright: 2012 by Daniel Neuhäuser :copyright: 2012 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details :license: BSD, see LICENSE.rst for details
""" """
from __future__ import absolute_import
import sys import sys
import textwrap import textwrap
from types import MethodType
from functools import partial
from collections import deque from collections import deque


from six import u from six import u
Expand Down Expand Up @@ -194,10 +197,18 @@ def __init__(self):
force_list(self.__class__.arguments) force_list(self.__class__.arguments)
) )
self.parent = None self.parent = None

signature = Signature.from_method(self.main) signature = Signature.from_method(self.main)
if signature.annotations: if signature.annotations:
self._populate_from_signature(self, signature) 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 @property
def option_prefixes(self): def option_prefixes(self):
""" """
Expand Down
14 changes: 14 additions & 0 deletions awwparse/testsuite/init.py
Expand Up @@ -186,6 +186,20 @@ def main(self, a: Integer(), b: Integer()):
self.assert_equal(Foo().run(["1", "1"]), 2) 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): def test_option_shorts_and_longs(self):
command = Command() command = Command()
command.add_option("foo", Option("a", String())) command.add_option("foo", Option("a", String()))
Expand Down

0 comments on commit cb7d797

Please sign in to comment.