Skip to content

Commit

Permalink
Add option help_on_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
igrek51 committed Jun 14, 2020
1 parent 58b19ce commit 9f8b5e4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ python3 setup.py develop
Running tests:
```bash
pip3 install -r requirements.txt -r requirements-dev.txt
./test.sh
./pytest.sh
```

## CliBuilder
Expand Down
19 changes: 13 additions & 6 deletions cliglue/builder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self,
usage_onerror: bool = True,
reraise_error: bool = False,
hide_internal: bool = True,
on_empty: Optional[Callable[..., None]] = None,
help_on_empty: bool = False,
):
"""
A builder for Command Line Interface specification
Expand All @@ -45,14 +45,14 @@ def __init__(self,
self.__version: str = version
self.__help: str = help
self.__subrules: List[CliRule] = []
self.__on_empty: Optional[Callable[..., None]] = on_empty

if run:
self.has(default_action(run))

self.__usage_onerror: bool = usage_onerror
self.__reraise_error: bool = reraise_error
self.__hide_internal: bool = hide_internal
self.__help_on_empty: bool = help_on_empty
if with_defaults:
self.__add_default_rules()

Expand All @@ -76,10 +76,8 @@ def run(self):

def run_with_args(self, args: List[str]):
try:
if not args and self.__on_empty is not None:
Parser([default_action(self.__on_empty)]).parse_args(args)
return
Parser(self.__subrules).parse_args(args)
parser = self.__create_parser(args)
parser.parse_args(args)
except CliDefinitionError as e:
error(f'CLI Definition error: {e}')
raise e
Expand All @@ -90,6 +88,15 @@ def run_with_args(self, args: List[str]):
if self.__reraise_error:
raise e

def __create_parser(self, args: List[str]) -> Parser:
if not args and self.__help_on_empty:
def __print_root_help():
self.print_help([])

return Parser([default_action(__print_root_help)])

return Parser(self.__subrules)

def print_help(self, subcommands: List[str]):
print_help(self.__subrules, self.__name, self.__version, self.__help, subcommands, self.__hide_internal)

Expand Down
2 changes: 1 addition & 1 deletion cliglue/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.9"
__version__ = "1.0.10"
6 changes: 3 additions & 3 deletions tests/parser/test_default_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def test_parent_default_action():

def test_on_empty_action_overriden():
with MockIO() as mockio:
CliBuilder(run=print_bad, on_empty=print_ok).has(
CliBuilder(run=print_bad, help_on_empty=True).has(
flag('any')
).run()
assert mockio.output() == 'ok\n'
assert 'Usage' in mockio.output()
with MockIO('--any') as mockio:
CliBuilder(run=print_ok, on_empty=print_bad).has(
CliBuilder(run=print_ok, help_on_empty=True).has(
flag('any')
).run()
assert mockio.output() == 'ok\n'

0 comments on commit 9f8b5e4

Please sign in to comment.