Skip to content

Commit 2c79436

Browse files
committed
add named-parameters setting
1 parent 6f55121 commit 2c79436

File tree

9 files changed

+72
-1
lines changed

9 files changed

+72
-1
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@ foo:
10191019
| `export` | boolean | `false` | Export all variables as environment variables. |
10201020
| `fallback` | boolean | `false` | Search `justfile` in parent directory if the first recipe on the command line is not found. |
10211021
| `ignore-comments` | boolean | `false` | Ignore recipe lines beginning with `#`. |
1022+
| `named-parameters` | boolean | `false` | Using named-parameters feature for all recipes in `justfile`. |
10221023
| `positional-arguments` | boolean | `false` | Pass positional arguments. |
10231024
| `quiet` | boolean | `false` | Disable echoing recipe lines before executing. |
10241025
| `script-interpreter`<sup>1.33.0</sup> | `[COMMAND, ARGS…]` | `['sh', '-eu']` | Set command used to invoke recipes with empty `[script]` attribute. |
@@ -2117,6 +2118,7 @@ change their behavior.
21172118
| `[linux]`<sup>1.8.0</sup> | recipe | Enable recipe on Linux. |
21182119
| `[macos]`<sup>1.8.0</sup> | recipe | Enable recipe on MacOS. |
21192120
| `[metadata(METADATA)]`<sup>1.42.0</sup> | recipe | Attach `METADATA` to recipe. |
2121+
| `[named-parameters]`<sup>1.44.0<sup> | recipe | Turn on [named_parameters](#named_parameters) for this recipe. |
21202122
| `[no-cd]`<sup>1.9.0</sup> | recipe | Don't change directory before executing recipe. |
21212123
| `[no-exit-message]`<sup>1.7.0</sup> | recipe | Don't print an error message if recipe fails. |
21222124
| `[no-quiet]`<sup>1.23.0</sup> | recipe | Override globally quiet recipes and always echo out the recipe. |
@@ -3960,6 +3962,29 @@ This preserves `just`'s ability to catch variable name typos before running,
39603962
for example if you were to write `{{argument}}`, but will not do what you want
39613963
if the value of `argument` contains single quotes.
39623964

3965+
#### Named Parameters
3966+
3967+
The `named-arguments` setting enables python like keyword arguments for recipe calls.
3968+
Take for instance the following
3969+
3970+
``` just
3971+
set named-parameters
3972+
3973+
foo default1="changes_often" default2="changes_sometimes" default3="changes_rarely":
3974+
echo {{ default1 }} {{ default2 }} {{ default3 }}
3975+
```
3976+
3977+
Calling `just foo default2="x"` would normally result in stdout of
3978+
`default2=x changes_sometimes changes_rarely`. while with `named-arguments`
3979+
one would get `changes_often x changes_rarely`. Notice it is still possible
3980+
to have positional parameters such as the recipe:
3981+
3982+
``` just
3983+
foo a b="default" c=b:
3984+
echo {{ a }} {{ b }} {{ c }}
3985+
```
3986+
3987+
39633988
#### Positional Arguments
39643989

39653990
The `positional-arguments` setting causes all arguments to be passed as

src/justfile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ impl<'src> Justfile<'src> {
334334

335335
let use_named_parameters = recipe
336336
.attributes
337-
.contains(AttributeDiscriminant::NamedParameters);
337+
.contains(AttributeDiscriminant::NamedParameters)
338+
|| module.settings.named_parameters;
338339

339340
let (outer, positional) = Evaluator::evaluate_parameters(
340341
&context,

src/keyword.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) enum Keyword {
2020
IgnoreComments,
2121
Import,
2222
Mod,
23+
NamedParameters,
2324
NoExitMessage,
2425
PositionalArguments,
2526
Quiet,

src/node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ impl<'src> Node<'src> for Set<'src> {
308308
| Setting::Export(value)
309309
| Setting::Fallback(value)
310310
| Setting::NoExitMessage(value)
311+
| Setting::NamedParameters(value)
311312
| Setting::PositionalArguments(value)
312313
| Setting::Quiet(value)
313314
| Setting::Unstable(value)

src/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,9 @@ impl<'run, 'src> Parser<'run, 'src> {
11311131
Keyword::Quiet => Some(Setting::Quiet(self.parse_set_bool()?)),
11321132
Keyword::Unstable => Some(Setting::Unstable(self.parse_set_bool()?)),
11331133
Keyword::WindowsPowershell => Some(Setting::WindowsPowerShell(self.parse_set_bool()?)),
1134+
Keyword::NamedParameters => {
1135+
Some(Setting::NamedParameters(self.parse_set_bool()?))
1136+
}
11341137
_ => None,
11351138
};
11361139

src/setting.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub(crate) enum Setting<'src> {
1212
Export(bool),
1313
Fallback(bool),
1414
IgnoreComments(bool),
15+
NamedParameters(bool),
1516
NoExitMessage(bool),
1617
PositionalArguments(bool),
1718
Quiet(bool),
@@ -35,6 +36,7 @@ impl Display for Setting<'_> {
3536
| Self::Export(value)
3637
| Self::Fallback(value)
3738
| Self::IgnoreComments(value)
39+
| Self::NamedParameters(value)
3840
| Self::NoExitMessage(value)
3941
| Self::PositionalArguments(value)
4042
| Self::Quiet(value)

src/settings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub(crate) struct Settings<'src> {
1717
pub(crate) export: bool,
1818
pub(crate) fallback: bool,
1919
pub(crate) ignore_comments: bool,
20+
pub(crate) named_parameters: bool,
2021
pub(crate) no_exit_message: bool,
2122
pub(crate) positional_arguments: bool,
2223
pub(crate) quiet: bool,
@@ -66,6 +67,9 @@ impl<'src> Settings<'src> {
6667
Setting::IgnoreComments(ignore_comments) => {
6768
settings.ignore_comments = ignore_comments;
6869
}
70+
Setting::NamedParameters(named_parameters) => {
71+
settings.named_parameters = named_parameters;
72+
}
6973
Setting::NoExitMessage(no_exit_message) => {
7074
settings.no_exit_message = no_exit_message;
7175
}

tests/json.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct Settings<'a> {
9494
windows_powershell: bool,
9595
windows_shell: Option<&'a str>,
9696
working_directory: Option<&'a str>,
97+
named_parameters: bool,
9798
}
9899

99100
#[track_caller]

tests/named_parameters.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,36 @@ fn fail_on_unknown_named_param() {
144144
.status(EXIT_FAILURE)
145145
.run();
146146
}
147+
148+
#[test]
149+
fn named_parameters_setting() {
150+
Test::new()
151+
.arg("foo")
152+
.arg("a=1")
153+
.arg("c=3")
154+
.justfile(
155+
r#"
156+
set named-parameters
157+
158+
foo a="a" b="b" c="c":
159+
echo {{ a }}
160+
echo {{ b }}
161+
echo {{ c }}
162+
"#,
163+
)
164+
.stdout(
165+
"
166+
1
167+
b
168+
3
169+
",
170+
)
171+
.stderr(
172+
r#"
173+
echo 1
174+
echo b
175+
echo 3
176+
"#,
177+
)
178+
.run();
179+
}

0 commit comments

Comments
 (0)