aboutsummaryrefslogtreecommitdiffstats
path: root/task
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2023-05-09 21:10:21 -0400
committerGitHub <noreply@github.com>2023-05-09 21:10:21 -0400
commitc71631e9835700175ee14aef53f80899aa88d568 (patch)
treebc8c5f9987b47d63bbc37a6cbe7889d15047ada8 /task
parentFix CI Badge in Readme (diff)
parentRemove extra comment (diff)
downloadtaskipy2-c71631e9835700175ee14aef53f80899aa88d568.zip
Merge pull request #60 from taskipy/feature/gh-32/add-poetry-plugin
GH-32: Rework `main` for poetry plugin
Diffstat (limited to '')
-rwxr-xr-xtaskipy/cli.py34
-rw-r--r--taskipy/list.py16
-rw-r--r--taskipy/py.typed0
3 files changed, 34 insertions, 16 deletions
diff --git a/taskipy/cli.py b/taskipy/cli.py
index 67baa0c..204880e 100755
--- a/taskipy/cli.py
+++ b/taskipy/cli.py
@@ -2,12 +2,28 @@
import argparse
import sys
from pathlib import Path
+from typing import List, Union
from taskipy.exceptions import TaskipyError, InvalidUsageError
from taskipy.task_runner import TaskRunner
def main():
+ exit_code = run(sys.argv[1:])
+ sys.exit(exit_code)
+
+
+def run(args: List[str], cwd: Union[str, Path, None] = None) -> int:
+ """Run the taskipy CLI programmatically.
+
+ Args:
+ args: The arguments passed to the taskipy CLI.
+ cwd: The working directory to run the task in. If not
+ provided, defaults to the current working directory.
+
+ Returns:
+ 0 on success; > 0 for an error.
+ """
parser = argparse.ArgumentParser(
prog='task',
description='runs a task specified in your pyproject.toml under [tool.taskipy.tasks]',
@@ -15,26 +31,26 @@ def main():
parser.add_argument('-l', '--list', help='show list of available tasks', action='store_true')
parser.add_argument('name', help='name of the task', nargs='?')
parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task')
- args = parser.parse_args()
+ parsed_args = parser.parse_args(args=args)
try:
- runner = TaskRunner(Path.cwd())
+ cwd = Path(cwd).resolve() if cwd is not None else Path.cwd()
+ runner = TaskRunner(cwd)
- if args.list:
+ if parsed_args.list:
runner.list()
- sys.exit(0)
+ return 0
- if args.name is None:
+ if parsed_args.name is None:
raise InvalidUsageError(parser)
- exit_code = runner.run(args.name, args.args)
- sys.exit(exit_code)
+ return runner.run(parsed_args.name, parsed_args.args)
except TaskipyError as e:
print(e)
- sys.exit(e.exit_code)
+ return e.exit_code
except Exception as e:
print(e)
- sys.exit(1)
+ return 1
if __name__ == '__main__':
diff --git a/taskipy/list.py b/taskipy/list.py
index 50e95aa..04d683b 100644
--- a/taskipy/list.py
+++ b/taskipy/list.py
@@ -1,21 +1,23 @@
import shutil
import textwrap
-import colorama # type: ignore
-from typing import List
+import colorama # type: ignore
+from typing import Iterable, Optional
from taskipy.task import Task
from taskipy.exceptions import EmptyTasksSectionError
class TasksListFormatter:
- def __init__(self, tasks: List[Task]):
- self.__tasks = tasks
+ def __init__(self, tasks: Iterable[Task]):
+ if not tasks:
+ raise EmptyTasksSectionError()
- def print(self, line_width=shutil.get_terminal_size().columns):
+ self.__tasks = tasks
colorama.init()
- if not self.__tasks:
- raise EmptyTasksSectionError()
+ def print(self, line_width: Optional[int] = None):
+ if not line_width:
+ line_width = shutil.get_terminal_size().columns
tasks_col = [task.name for task in self.__tasks]
longest_item_in_tasks_col = len(max(tasks_col, key=len))
diff --git a/taskipy/py.typed b/taskipy/py.typed
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/taskipy/py.typed