aboutsummaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorCarlos Mateus <carlosmatateumateus@gmail.com>2024-06-16 16:26:57 +0200
committerGitHub <noreply@github.com>2024-06-16 17:26:57 +0300
commitb52519437fa214624f6a85debcfefc79e032294f (patch)
treeaca9fa0c9d00dd574af8d67a05d07f077f442638 /task
parentrelease script now opens a pr (diff)
downloadtaskipy2-b52519437fa214624f6a85debcfefc79e032294f.zip
Add feature to suggest similar tasks when a task is not found (#78)
* Add feature to suggest similar tasks when a task is not found * Add test for feature to suggest similar tasks when a task is not found * Add a test for when a task is not found and there are no suggestions * Fix lint
Diffstat (limited to '')
-rw-r--r--taskipy/exceptions.py5
-rw-r--r--taskipy/task_runner.py8
2 files changed, 11 insertions, 2 deletions
diff --git a/taskipy/exceptions.py b/taskipy/exceptions.py
index affdd4e..ba0d24a 100644
--- a/taskipy/exceptions.py
+++ b/taskipy/exceptions.py
@@ -35,11 +35,14 @@ class MalformedPyProjectError(TaskipyError):
class TaskNotFoundError(TaskipyError):
exit_code = 127
- def __init__(self, task_name: str):
+ def __init__(self, task_name: str, suggestion: Optional[str] = None):
super().__init__()
self.task = task_name
+ self.suggestion = suggestion
def __str__(self):
+ if self.suggestion:
+ return f'could not find task "{self.task}", did you mean "{self.suggestion}"?'
return f'could not find task "{self.task}"'
class MalformedTaskError(TaskipyError):
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py
index 561427e..ab81012 100644
--- a/taskipy/task_runner.py
+++ b/taskipy/task_runner.py
@@ -2,6 +2,7 @@ import sys
import platform
import signal
import subprocess
+from difflib import get_close_matches
from pathlib import Path
from types import FrameType
from typing import Callable, Dict, List, Tuple, Union, Optional
@@ -80,7 +81,12 @@ class TaskRunner:
try:
task = self.__project.tasks[task_name]
except KeyError:
- raise TaskNotFoundError(task_name)
+ suggestion = None
+ closest_match = get_close_matches(task_name, self.__project.tasks, n=1, cutoff=0.5)
+
+ if closest_match:
+ suggestion = closest_match[0]
+ raise TaskNotFoundError(task_name, suggestion)
return pre_task, task, post_task