Skip to content

Commit 672c14b

Browse files
committed
added vars to readme
1 parent edde602 commit 672c14b

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44

55
</p>
66

7+
- [General](#general)
8+
- [Requirements](#requirements)
9+
- [Usage](#usage)
10+
* [Installation](#installation)
11+
* [Adding Tasks](#adding-tasks)
12+
* [Running Tasks](#running-tasks)
13+
* [Passing Command Line Args to Tasks](#passing-command-line-args-to-tasks)
14+
* [Composing Tasks](#composing-tasks)
15+
* [Using Variables](#using-variables)
16+
* [Using Taskipy Without Poetry](#using-taskipy-without-poetry)
17+
* [Advanced Use Cases](#advanced-use-cases)
18+
- [Maintainers 🚧](#maintainers---)
19+
- [Contributors ✨](#contributors--)
20+
721
## General
822
[![pypi](https://img.shields.io/pypi/v/taskipy?style=flat-square)](https://pypi.org/project/taskipy/)
923
[![ci](https://img.shields.io/github/workflow/status/illberoy/taskipy/Taskipy%20Test%20CI?style=flat-square)](https://github.com/illBeRoy/taskipy/actions?query=workflow%3A%22Taskipy+Test+CI%22)
@@ -159,6 +173,53 @@ lint = "pylint tests taskipy"
159173

160174
The posttask hook looks for `post_<task_name>` task for a given `task_name`. It will run it after running the task itself. If the task failed, then taskipy will not run the posttask hook.
161175

176+
### Using Variables
177+
In some cases, you might find yourself passing the same arguments over and over again. Let us take a look at the following tasks:
178+
179+
```toml
180+
[tool.taskipy.tasks]
181+
lint = "pylint path/to/my_module"
182+
black = "black path/to/my_module"
183+
```
184+
185+
As you can see, we provide the same path argument to both `pylint` and `black`.
186+
187+
In order to encourage DRY and improve your ability to change these values later on, taskipy actually lets you declare and reuse variables in your tasks:
188+
189+
```toml
190+
[tool.taskipy.variables]
191+
path = "path/to/my_module"
192+
193+
[tool.taskipy.tasks]
194+
lint = { cmd = "pylint {path}", use_vars = true }
195+
black = { cmd = "pylint {path}", use_vars = true }
196+
```
197+
198+
We have made the following changes to our configuration:
199+
1. We declared variables under `tool.taskipy.variables`
200+
2. We flagged the relevant task using `use_vars` to note that they should use the variables
201+
3. We replaced the repeating path with a `{path}` variable
202+
203+
#### String Formatting
204+
The formatting of the task commands uses python's own `string.format` method, and therefore supports everything that python's [formatted string literals](https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals) let you do.
205+
206+
#### Always Use Variables
207+
Using variables is opt-in, which means that by default commands do **not** use them, and you will have to turn them on a task to task basis.
208+
209+
If you want to turn on `use_vars` globally, all you need to do is to declare that under taskipy's **settings** table:
210+
211+
```toml
212+
[tool.taskipy.settings]
213+
use_vars = true
214+
215+
[tool.taskipy.variables]
216+
path = "path/to/my_module"
217+
218+
[tool.taskipy.tasks]
219+
lint = "pylint {path}"
220+
black = "black {path}"
221+
```
222+
162223
### Using Taskipy Without Poetry
163224
Taskipy was created with poetry projects in mind, but actually only requires a valid `pyproject.toml` file in your project's directory. As a result, you can use it even eithout poetry:
164225

tests/test_taskipy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def test_use_vars_working(self):
438438
name = "John Doe"
439439
440440
[tool.taskipy.tasks]
441-
echo = { cmd = "echo hello {name:<10}:", use_vars=true }
441+
echo = { cmd = "echo hello {name:<10}:", use_vars = true }
442442
'''
443443
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
444444
exit_code, stdout, _ = self.run_task('echo', cwd=cwd)
@@ -464,7 +464,7 @@ def test_use_vars_param_disabled(self):
464464
name = "John Doe"
465465
466466
[tool.taskipy.tasks]
467-
echo = { cmd = "echo hello {name}", use_vars=false}
467+
echo = { cmd = "echo hello {name}", use_vars = false }
468468
'''
469469
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
470470
exit_code, stdout, _ = self.run_task('echo', cwd=cwd)
@@ -490,7 +490,7 @@ def test_use_vars_param_malformed(self):
490490
name = "John Doe"
491491
492492
[tool.taskipy.tasks]
493-
echo = { cmd = "echo hello {name}", use_vars=1}
493+
echo = { cmd = "echo hello {name}", use_vars = 1 }
494494
'''
495495
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
496496
exit_code, stdout, _ = self.run_task('echo', cwd=cwd)
@@ -500,7 +500,7 @@ def test_use_vars_param_malformed(self):
500500
def test_use_vars_missing_var(self):
501501
py_project_toml = '''
502502
[tool.taskipy.tasks]
503-
echo = { cmd = "echo hello {name}", use_vars=true}
503+
echo = { cmd = "echo hello {name}", use_vars = true }
504504
'''
505505
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
506506
exit_code, stdout, _ = self.run_task('echo', cwd=cwd)

0 commit comments

Comments
 (0)