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

Adds just::run_with_args to provide arguments from Rust code #2172

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions examples/run-with-args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Example showing how to run just commands from Rust code:

fn main() {
let args = vec!["-f", "kitchen-sink.just", "foo"];
if let Err(code) = just::run_with_args(args) {
std::process::exit(code);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) use {
pub(crate) use crate::{node::Node, tree::Tree};

pub use crate::run::run;
pub use crate::run::run_with_args;

// Used in integration tests.
#[doc(hidden)]
Expand Down
35 changes: 32 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
use super::*;

/// Main entry point into just binary.
#[allow(clippy::missing_errors_doc)]
/// ArgSource controls where the program arguments are received from.
enum ArgSource {
OS,
Is(Vec<String>),
}

/// Main entry point into just binary, taking arguments provided by the OS.
pub fn run() -> Result<(), i32> {
run_with_arg_source(ArgSource::OS)
}

/// Main entry point into just library, taking arguments provided by Vec<&str>
pub fn run_with_args(args: Vec<&str>) -> Result<(), i32> {
let args: Vec<String> = Vec::from_iter(
vec!["just".to_string()].into_iter().chain(
args
.iter()
.map(|&s| s.into())
.collect::<Vec<String>>()
.iter()
.cloned(),
),
);
run_with_arg_source(ArgSource::Is(args))
}

/// Main entry point which takes arguments from a configurable source.
#[allow(clippy::missing_errors_doc)]
fn run_with_arg_source(arg_source: ArgSource) -> Result<(), i32> {
#[cfg(windows)]
ansi_term::enable_ansi_support().ok();

Expand All @@ -17,7 +43,10 @@ pub fn run() -> Result<(), i32> {
let app = Config::app();

info!("Parsing command line arguments…");
let matches = app.get_matches();
let matches = match arg_source {
ArgSource::OS => app.get_matches(),
ArgSource::Is(value) => app.get_matches_from(value),
};

let config = Config::from_matches(&matches).map_err(Error::from);

Expand Down
Loading