Skip to content

Commit 40cf72c

Browse files
Add usage printout when no task is given (#31)
* Add usage printout when no task is given This addresses issue #30 where 'could not find task "None"' would be outputted when simply `task` is ran without an task name. * Simplify InvalidUsageError's __str__ method * Ignore pyenv's local python version file * Update dependencies in lockfile a.k.a. ran `poetry update` * Disable W0707 in pylint
1 parent 432f112 commit 40cf72c

File tree

6 files changed

+192
-154
lines changed

6 files changed

+192
-154
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ share/python-wheels/
2323
MANIFEST
2424
.mypy_cache
2525
.idea
26+
.python-version

.pylintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ disable=
66
C0411,
77
R0903,
88
R0915,
9-
W0703
9+
W0703,
10+
W0707
1011

1112
[FORMAT]
1213
max-line-length=1000

poetry.lock

Lines changed: 158 additions & 152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

taskipy/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from pathlib import Path
55

6-
from taskipy.exceptions import TaskipyError
6+
from taskipy.exceptions import TaskipyError, InvalidUsageError
77
from taskipy.task_runner import TaskRunner
88

99

@@ -16,13 +16,17 @@ def main():
1616
parser.add_argument('name', help='name of the task', nargs='?')
1717
parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task')
1818
args = parser.parse_args()
19+
1920
try:
2021
runner = TaskRunner(Path.cwd())
2122

2223
if args.list:
2324
runner.list()
2425
sys.exit(0)
2526

27+
if args.name is None:
28+
raise InvalidUsageError(parser)
29+
2630
exit_code = runner.run(args.name, args.args)
2731
sys.exit(exit_code)
2832
except TaskipyError as e:

taskipy/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from argparse import ArgumentParser
2+
13
class TaskipyError(Exception):
24
exit_code = 1
35

@@ -30,6 +32,15 @@ def __init__(self, task_name: str):
3032
def __str__(self):
3133
return f'could not find task "{self.task}"'
3234

35+
class InvalidUsageError(TaskipyError):
36+
exit_code = 127
37+
38+
def __init__(self, parser: ArgumentParser):
39+
super().__init__()
40+
self.__parser = parser
41+
42+
def __str__(self):
43+
return self.__parser.format_usage().strip('\n')
3344

3445
class MissingTaskipySettingsSectionError(TaskipyError):
3546
exit_code = 127

tests/test_taskipy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ def test_exiting_with_code_127_and_printing_if_task_not_found(self):
222222
self.assertSubstr('could not find task "task_that_does_not_exist"', stdout)
223223
self.assertEqual(exit_code, 127)
224224

225+
def test_exiting_with_code_127_and_printing_if_no_arg_is_passed(self):
226+
cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks')
227+
executable_path = path.abspath('task')
228+
proc = subprocess.Popen(
229+
executable_path,
230+
stdin=subprocess.PIPE,
231+
stdout=subprocess.PIPE,
232+
stderr=subprocess.PIPE,
233+
cwd=cwd
234+
)
235+
stdout, _ = proc.communicate()
236+
237+
self.assertSubstr('usage: task', stdout.decode())
238+
self.assertEqual(proc.returncode, 127)
239+
225240
def test_exiting_with_code_127_and_printing_if_no_tasks_section(self):
226241
cwd = self.create_test_dir_from_fixture('project_with_pyproject_without_tasks_section')
227242
exit_code, stdout, _ = self.run_task('task_that_does_not_exist', cwd=cwd)

0 commit comments

Comments
 (0)