Skip to content

Commit

Permalink
Changes to mach1 - breaking API
Browse files Browse the repository at this point in the history
 mach1 didn't allow parametes before.
 Now we can add more options.
 Per default we can control whether help is show
 or not when no other args are given
  • Loading branch information
oz123 committed Jul 30, 2018
1 parent 5c4e662 commit 0e99b8f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -40,7 +40,7 @@ You can make command line application using the decorator ``mach1``:
from mach import mach1
@mach1
@mach1()
class Calculator:
def add(self, int: a, int: b):
Expand Down
6 changes: 3 additions & 3 deletions docs/usage.rst
Expand Up @@ -16,7 +16,7 @@ Example ``mach1``
from mach import mach1
@mach1
@mach1()
class Hello:
default = 'greet'
Expand Down Expand Up @@ -300,7 +300,7 @@ line interface:
def bar(self):
pass
@mach1
@mach1()
class Baz(Foo)
def do(self):
pass
Expand All @@ -318,7 +318,7 @@ with a leading underscore `_`:
def _bar(self):
pass
@mach1
@mach1()
class Baz(Foo)
def do(self):
self._foo()
Expand Down
3 changes: 1 addition & 2 deletions examples/calc.py
Expand Up @@ -13,7 +13,7 @@
from mach import mach1


@mach1
@mach1()
class Calculator:

def add(self, a: int, b: int):
Expand All @@ -25,7 +25,6 @@ def div(self, a: int, b: int):
print("%s / %s => %d" % (a, b, int(a) // int(b)))



if __name__ == '__main__':

calc = Calculator()
Expand Down
2 changes: 1 addition & 1 deletion examples/greet.py
Expand Up @@ -35,7 +35,7 @@
from mach import mach1


@mach1
@mach1()
class Hello:

default = 'greet'
Expand Down
2 changes: 1 addition & 1 deletion examples/uftpd.py
Expand Up @@ -30,7 +30,7 @@
from mach import mach1


@mach1
@mach1()
class uFTPD:

default = 'server'
Expand Down
22 changes: 17 additions & 5 deletions mach.py
Expand Up @@ -190,7 +190,7 @@ def not_private(x):
return False


def _mach(kls, add_do=False, explicit=False):
def _mach(kls, add_do=False, explicit=True, auto_help=True):

if hasattr(kls, 'default'):
parser = DefaultSubcommandArgParse(
Expand Down Expand Up @@ -226,6 +226,7 @@ def _mach(kls, add_do=False, explicit=False):
if add_do:
kls = do_kls

parser.auto_help = auto_help
kls.parser = parser
return kls

Expand Down Expand Up @@ -254,17 +255,28 @@ def _run1(inst, args=None):
func(*(getattr(p, arg) for arg in args))
return True

elif inst.parser.auto_help:
inst.parser.print_help()


def _run2(inst): # pragma: no coverage
if not inst._run1() and (
'--shell' not in inst.parser._option_string_actions):
inst.cmdloop()


def mach1(kls): # pragma: no coverage
kls = _mach(kls)
kls.run = _run1
return kls
def mach1(auto_help=True): # pragma: no coverage

def real_decorator(callable_, *args, **kwargs):

def wrapper(*args, **kwargs):
kls = _mach(callable_, explicit=False, auto_help=auto_help)
kls.run = _run1
return kls(*args, **kwargs)

return wrapper

return real_decorator


def mach2(explicit=False):
Expand Down

0 comments on commit 0e99b8f

Please sign in to comment.