Skip to content

Commit

Permalink
Add --one flag to forbid multiple recipes from being invoked on the…
Browse files Browse the repository at this point in the history
… command line (#2374)
  • Loading branch information
casey authored Sep 21, 2024
1 parent 19018d1 commit 4d865a1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,48 @@ $ just --list --list-heading ''
build
```

### Invoking Multiple Recipes

Multiple recipes may be invoked on the command line at once:

```just
build:
make web
serve:
python3 -m http.server -d out 8000
```

```sh
$ just build serve
make web
python3 -m http.server -d out 8000
```

Keep in mind that recipes with parameters will swallow arguments, even if they
match the names of other recipes:

```just
build project:
make {{project}}
serve:
python3 -m http.server -d out 8000
```

```sh
$ just build serve
make: *** No rule to make target `serve'. Stop.
```
The `--one` flag can be used to restrict command-line invocations to a single
recipe:
```sh
$ just --one build serve
error: Expected 1 command-line recipe invocation but found 2.
```
### Working Directory
By default, recipes run with the working directory set to the directory that
Expand Down
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) struct Config {
pub(crate) load_dotenv: bool,
pub(crate) no_aliases: bool,
pub(crate) no_dependencies: bool,
pub(crate) one: bool,
pub(crate) search_config: SearchConfig,
pub(crate) shell: Option<String>,
pub(crate) shell_args: Option<Vec<String>>,
Expand Down Expand Up @@ -100,6 +101,7 @@ mod arg {
pub(crate) const NO_DEPS: &str = "NO-DEPS";
pub(crate) const NO_DOTENV: &str = "NO-DOTENV";
pub(crate) const NO_HIGHLIGHT: &str = "NO-HIGHLIGHT";
pub(crate) const ONE: &str = "ONE";
pub(crate) const QUIET: &str = "QUIET";
pub(crate) const SET: &str = "SET";
pub(crate) const SHELL: &str = "SHELL";
Expand Down Expand Up @@ -297,6 +299,13 @@ impl Config {
.help("Don't highlight echoed recipe lines in bold")
.overrides_with(arg::HIGHLIGHT),
)
.arg(
Arg::new(arg::ONE)
.long("one")
.env("JUST_ONE")
.action(ArgAction::SetTrue)
.help("Forbid multiple recipes from being invoked on the command line"),
)
.arg(
Arg::new(arg::QUIET)
.short('q')
Expand Down Expand Up @@ -721,6 +730,7 @@ impl Config {
load_dotenv: !matches.get_flag(arg::NO_DOTENV),
no_aliases: matches.get_flag(arg::NO_ALIASES),
no_dependencies: matches.get_flag(arg::NO_DEPS),
one: matches.get_flag(arg::ONE),
search_config,
shell: matches.get_one::<String>(arg::SHELL).map(Into::into),
shell_args: if matches.get_flag(arg::CLEAR_SHELL_ARGS) {
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub(crate) enum Error<'src> {
variable: String,
suggestion: Option<Suggestion<'src>>,
},
ExcessInvocations {
invocations: usize,
},
ExpectedSubmoduleButFoundRecipe {
path: String,
},
Expand Down Expand Up @@ -373,6 +376,9 @@ impl<'src> ColorDisplay for Error<'src> {
write!(f, "\n{suggestion}")?;
}
}
ExcessInvocations { invocations } => {
write!(f, "Expected 1 command-line recipe invocation but found {invocations}.")?;
},
ExpectedSubmoduleButFoundRecipe { path } => {
write!(f, "Expected submodule at `{path}` but found recipe.")?;
},
Expand Down
6 changes: 6 additions & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ impl<'src> Justfile<'src> {
)?);
}

if config.one && invocations.len() > 1 {
return Err(Error::ExcessInvocations {
invocations: invocations.len(),
});
}

let mut ran = Ran::default();
for invocation in invocations {
let context = ExecutionContext {
Expand Down
37 changes: 31 additions & 6 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,40 @@ fn dont_run_duplicate_recipes() {
Test::new()
.justfile(
"
foo:
# foo
",
@foo:
echo foo
",
)
.args(["foo", "foo"])
.stderr(
.stdout("foo\n")
.run();
}

#[test]
fn one_flag_only_allows_one_invocation() {
Test::new()
.justfile(
"
# foo
",
@foo:
echo foo
",
)
.args(["--one", "foo"])
.stdout("foo\n")
.run();

Test::new()
.justfile(
"
@foo:
echo foo
@bar:
echo bar
",
)
.args(["--one", "foo", "bar"])
.stderr("error: Expected 1 command-line recipe invocation but found 2.\n")
.status(1)
.run();
}

0 comments on commit 4d865a1

Please sign in to comment.