Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable using script and expression cli arguments at the same time #356

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ struct Args {
short,
long,
value_name = "CODE",
conflicts_with = "file",
action = clap::ArgAction::Append
)]
expression: Option<Vec<String>>,
Expand Down Expand Up @@ -201,35 +200,44 @@ impl Cli {
.load_currency_module_on_demand(true);
}

let code_and_source: Option<(String, CodeSource)> = if let Some(ref path) = self.file {
Some((
fs::read_to_string(path).context(format!(
let mut code_and_source = Vec::new();

if let Some(ref path) = self.file {
code_and_source.push((
(fs::read_to_string(path).context(format!(
"Could not load source file '{}'",
path.to_string_lossy()
))?,
))?),
CodeSource::File(path.clone()),
))
} else {
self.expression
.as_ref()
.map(|exprs| (exprs.iter().join("\n"), CodeSource::Text))
));
};

if let Some((code, code_source)) = code_and_source {
let result = self.parse_and_evaluate(
&code,
code_source,
ExecutionMode::Normal,
self.config.pretty_print,
);
if let Some(expressions) = &self.expression {
code_and_source.push((expressions.iter().join("\n"), CodeSource::Text));
}

match result {
std::ops::ControlFlow::Continue(()) => Ok(()),
std::ops::ControlFlow::Break(ExitStatus::Success) => Ok(()),
std::ops::ControlFlow::Break(ExitStatus::Error) => {
bail!("Interpreter stopped due to error")
}
if !code_and_source.is_empty() {
let mut code_result = Ok(());

for (code, code_source) in code_and_source {
let result = self.parse_and_evaluate(
&code,
code_source,
ExecutionMode::Normal,
self.config.pretty_print,
);

let result_status = match result {
std::ops::ControlFlow::Continue(()) => Ok(()),
std::ops::ControlFlow::Break(_) => {
bail!("Interpreter stopped")
}
};

code_result = code_result.and(result_status);
}

code_result
} else {
let mut currency_fetch_thread = if self.config.load_prelude
&& self.config.exchange_rates.fetching_policy
Expand Down
18 changes: 18 additions & 0 deletions numbat-cli/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ fn read_code_from_file() {
.stderr(predicates::str::contains("while parsing"));
}

#[test]
fn process_code_from_file_and_cli_expression() {
numbat()
.arg("tests/examples/pendulum.nbt")
.arg("--expression")
.arg("oscillation_time(20cm)")
.assert()
.success();

numbat()
.arg("tests/examples/pendulum.nbt")
.arg("--expression")
.arg("oscillation_time(20kg)")
.assert()
.failure()
.stderr(predicates::str::contains("while type checking"));
}

#[test]
fn print_calls() {
numbat()
Expand Down
Loading