Skip to content

Commit

Permalink
Print recipe signature if missing arguments (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladysamantha authored and casey committed Nov 3, 2018
1 parent 6430d38 commit af97f3f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
31 changes: 31 additions & 0 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl<'a> Justfile<'a> where {
if !argument_range.range_contains(argument_count) {
return Err(RuntimeError::ArgumentCountMismatch {
recipe: recipe.name,
parameters: recipe.parameters.iter().collect(),
found: tail.len(),
min: recipe.min_arguments(),
max: recipe.max_arguments(),
Expand Down Expand Up @@ -307,11 +308,17 @@ a return code:
{
ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
} => {
let param_names = parameters
.iter()
.map(|p| p.name)
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 2);
assert_eq!(min, 3);
assert_eq!(max, 3);
Expand All @@ -328,11 +335,17 @@ a return code:
{
ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
} => {
let param_names = parameters
.iter()
.map(|p| p.name)
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 2);
assert_eq!(min, 3);
assert_eq!(max, usize::MAX - 1);
Expand All @@ -349,11 +362,17 @@ a return code:
{
ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
} => {
let param_names = parameters
.iter()
.map(|p| p.name)
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 0);
assert_eq!(min, 3);
assert_eq!(max, 3);
Expand All @@ -370,11 +389,17 @@ a return code:
{
ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
} => {
let param_names = parameters
.iter()
.map(|p| p.name)
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 1);
assert_eq!(min, 2);
assert_eq!(max, 3);
Expand All @@ -391,11 +416,17 @@ a return code:
{
ArgumentCountMismatch {
recipe,
parameters,
found,
min,
max,
} => {
let param_names = parameters
.iter()
.map(|p| p.name)
.collect::<Vec<&str>>();
assert_eq!(recipe, "a");
assert_eq!(param_names, ["b", "c", "d"]);
assert_eq!(found, 0);
assert_eq!(min, 1);
assert_eq!(max, 3);
Expand Down
12 changes: 10 additions & 2 deletions src/runtime_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn write_token_error_context(f: &mut fmt::Formatter, token: &Token) -> Result<()

#[derive(Debug)]
pub enum RuntimeError<'a> {
ArgumentCountMismatch{recipe: &'a str, found: usize, min: usize, max: usize},
ArgumentCountMismatch{recipe: &'a str, parameters: Vec<&'a Parameter<'a>>, found: usize, min: usize, max: usize},
Backtick{token: Token<'a>, output_error: OutputError},
Code{recipe: &'a str, line_number: Option<usize>, code: i32},
Cygpath{recipe: &'a str, output_error: OutputError},
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> Display for RuntimeError<'a> {
maybe_s(overrides.len()),
And(&overrides.iter().map(Tick).collect::<Vec<_>>()))?;
},
ArgumentCountMismatch{recipe, found, min, max} => {
ArgumentCountMismatch{recipe, ref parameters, found, min, max} => {
if min == max {
let expected = min;
write!(f, "Recipe `{}` got {} argument{} but {}takes {}",
Expand All @@ -84,6 +84,14 @@ impl<'a> Display for RuntimeError<'a> {
write!(f, "Recipe `{}` got {} argument{} but takes at most {}",
recipe, found, maybe_s(found), max)?;
}
write!(f, "\nusage:\n just {}", recipe)?;
for param in parameters {
if color.stderr().active() {
write!(f, " {:#}", param)?;
} else {
write!(f, " {}", param)?;
}
}
},
Code{recipe, line_number, code} => {
if let Some(n) = line_number {
Expand Down
6 changes: 3 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ foo A B:
",
args: ("foo", "ONE"),
stdout: "",
stderr: "error: Recipe `foo` got 1 argument but takes 2\n",
stderr: "error: Recipe `foo` got 1 argument but takes 2\nusage:\n just foo A B\n",
status: EXIT_FAILURE,
}

Expand All @@ -803,7 +803,7 @@ foo A B C='C':
",
args: ("foo", "bar"),
stdout: "",
stderr: "error: Recipe `foo` got 1 argument but takes at least 2\n",
stderr: "error: Recipe `foo` got 1 argument but takes at least 2\nusage:\n just foo A B C='C'\n",
status: EXIT_FAILURE,
}

Expand Down Expand Up @@ -1670,7 +1670,7 @@ a x y +z:
",
args: ("a", "0", "1"),
stdout: "",
stderr: "error: Recipe `a` got 2 arguments but takes at least 3\n",
stderr: "error: Recipe `a` got 2 arguments but takes at least 3\nusage:\n just a x y +z\n",
status: EXIT_FAILURE,
}

Expand Down

0 comments on commit af97f3f

Please sign in to comment.