Skip to content

Commit

Permalink
feat: sub-command with a root context function
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Apr 13, 2020
1 parent b42392b commit 0c9d19d
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ Icon*
.settings
.idea/
.local/
*.log
2 changes: 1 addition & 1 deletion arger/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def run(self, *args) -> Any:
# populate sub-parsers
_add_parsers(self, self._command)

kwargs = vars(self.parse_args(args)) # type: Dict[str, Any]
kwargs = vars(self.parse_args(args or None)) # type: Dict[str, Any]

return self._command.run(**kwargs)

Expand Down
17 changes: 15 additions & 2 deletions arger/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,21 @@ def is_valid(self) -> bool:
return bool(self._fn or len(self._sub))

def run(self, command: Optional[str] = None, **kwargs):
fn = self._sub[command] if command else self
return fn(**kwargs)
args = set()
for k in self.args:
if 'dest' in k[1]:
args.add(k[1]['dest'])
else:
args.add(k[0][0])
root_kwargs = {k: v for k, v in kwargs.items() if k in args}
cmd_kwargs = {k: v for k, v in kwargs.items() if k not in args}

results = OrderedDict()
if root_kwargs and self.name:
results[self.name] = self(**root_kwargs)
if command:
results[command] = self._sub[command](**cmd_kwargs)
return results

def __call__(self, *args, **kwargs):
if self._fn:
Expand Down
3 changes: 2 additions & 1 deletion arger/typing_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable = W0212
import sys


Expand All @@ -6,7 +7,7 @@

def _origin(tp):
"""Return x.__origin__ or type(x) based on the Python version."""
if hasattr(tp, '_gorg'):
if hasattr(tp, "_gorg"):
return tp._gorg
if getattr(tp, "__origin__", None) is not None:
return tp.__origin__
Expand Down
103 changes: 66 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "arger"
version = "0.1.3"
version = "0.1.5"
description = "Create argparser automatically from functions"

license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ testpaths = tests
addopts =
--strict
--pdbcls=tests:Debugger
--doctest-modules
; --doctest-modules
-r sxX
--no-cov-on-fail
-s
Expand Down
43 changes: 41 additions & 2 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
from arger import Arger
import sh
from delegator import run

arger = Arger(description="Common set of tasks to run")


class Color:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

# reset
END = '\033[0m'


def prun(*cmd, **kwargs):
cmd = " ".join(cmd)
print(f'{Color.OKGREEN} $ {cmd}{Color.END}')
c = run(cmd, **kwargs)
print(c.out)
print(c.err)
return c


@arger.add_cmd
def release(type: str):
"""Bump version, tag and push them.
# todo: create an Enum
:param type: one of patch, minor, major, prepatch, preminor, premajor, prerelease. as supported by poetry version
"""
version = sh()
prun('poetry', 'version', type)
c = prun('poetry', 'version')
_, version = c.out.split()
prun('git status')
msg = 'chore: bump version'

answer = input(f'{msg}\nAdd to commit: [Y/n]?')
if answer.lower() in {'no', 'n'}:
return
prun('git add .')
prun(f'git commit -m {msg}')
prun(f'git tag v{version}')
prun('git push')
prun('git push --tags')


if __name__ == '__main__':
arger.run()
9 changes: 9 additions & 0 deletions tests/examples/3_nested_commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,30 @@ optional arguments:
1. ran create
```shell script
$ python 3_nested_commands.py create direcory
log (<class 'bool'>): False
log_file (<class 'NoneType'>): None
name (<class 'str'>): direcory
verbose (<class 'bool'>): False
```


1. ran remove
```shell script
$ python 3_nested_commands.py remove direcory
log (<class 'bool'>): False
log_file (<class 'NoneType'>): None
name (<class 'str'>): direcory
verbose (<class 'bool'>): False
```


1. ran list
```shell script
$ python 3_nested_commands.py list
container (<class 'list'>): []
log (<class 'bool'>): False
log_file (<class 'NoneType'>): None
verbose (<class 'bool'>): False
```

1. ran combined
Expand Down

0 comments on commit 0c9d19d

Please sign in to comment.