2019-06-01 17:00:42 +00:00
|
|
|
use clap::{App, Arg};
|
|
|
|
use log::LevelFilter;
|
2020-09-16 23:22:58 +00:00
|
|
|
use nu_cli::create_default_context;
|
2020-05-18 03:52:56 +00:00
|
|
|
use nu_cli::utils::test_bins as binaries;
|
2019-05-10 16:59:12 +00:00
|
|
|
use std::error::Error;
|
2020-02-10 16:49:45 +00:00
|
|
|
use std::fs::File;
|
2020-12-18 07:53:49 +00:00
|
|
|
use std::io::prelude::*;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-06-03 03:48:58 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2019-07-20 01:12:04 +00:00
|
|
|
let matches = App::new("nushell")
|
2019-08-24 15:25:04 +00:00
|
|
|
.version(clap::crate_version!())
|
2019-06-01 17:00:42 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("loglevel")
|
|
|
|
.short("l")
|
|
|
|
.long("loglevel")
|
|
|
|
.value_name("LEVEL")
|
|
|
|
.possible_values(&["error", "warn", "info", "debug", "trace"])
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2020-09-14 14:07:02 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("skip-plugins")
|
|
|
|
.hidden(true)
|
|
|
|
.long("skip-plugins")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
2020-05-18 03:52:56 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("testbin")
|
|
|
|
.hidden(true)
|
|
|
|
.long("testbin")
|
|
|
|
.value_name("TESTBIN")
|
|
|
|
.possible_values(&["cococo", "iecho", "fail", "nonu", "chop"])
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2020-02-09 02:24:33 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("commands")
|
|
|
|
.short("c")
|
|
|
|
.long("commands")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2019-06-01 17:00:42 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("develop")
|
|
|
|
.long("develop")
|
|
|
|
.multiple(true)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2019-10-28 14:46:50 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("debug")
|
|
|
|
.long("debug")
|
|
|
|
.multiple(true)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2020-02-10 06:55:07 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("stdin")
|
|
|
|
.long("stdin")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
2020-02-10 16:49:45 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("script")
|
|
|
|
.help("the nu script to run")
|
|
|
|
.index(1),
|
|
|
|
)
|
2020-05-24 23:26:27 +00:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("args")
|
|
|
|
.help("positional args (used by --testbin)")
|
|
|
|
.index(2)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
2019-06-01 17:00:42 +00:00
|
|
|
.get_matches();
|
|
|
|
|
2020-05-18 03:52:56 +00:00
|
|
|
if let Some(bin) = matches.value_of("testbin") {
|
|
|
|
match bin {
|
|
|
|
"cococo" => binaries::cococo(),
|
|
|
|
"iecho" => binaries::iecho(),
|
|
|
|
"fail" => binaries::fail(),
|
|
|
|
"nonu" => binaries::nonu(),
|
|
|
|
"chop" => binaries::chop(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
let loglevel = match matches.value_of("loglevel") {
|
|
|
|
None => LevelFilter::Warn,
|
|
|
|
Some("error") => LevelFilter::Error,
|
|
|
|
Some("warn") => LevelFilter::Warn,
|
|
|
|
Some("info") => LevelFilter::Info,
|
|
|
|
Some("debug") => LevelFilter::Debug,
|
|
|
|
Some("trace") => LevelFilter::Trace,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut builder = pretty_env_logger::formatted_builder();
|
|
|
|
|
|
|
|
if let Ok(s) = std::env::var("RUST_LOG") {
|
|
|
|
builder.parse_filters(&s);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.filter_module("nu", loglevel);
|
|
|
|
|
|
|
|
match matches.values_of("develop") {
|
|
|
|
None => {}
|
2019-06-01 21:15:21 +00:00
|
|
|
Some(values) => {
|
|
|
|
for item in values {
|
2020-03-05 10:18:53 +00:00
|
|
|
builder.filter_module(&format!("nu::{}", item), LevelFilter::Trace);
|
2019-06-01 21:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 17:00:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 14:46:50 +00:00
|
|
|
match matches.values_of("debug") {
|
|
|
|
None => {}
|
|
|
|
Some(values) => {
|
|
|
|
for item in values {
|
2020-03-05 10:18:53 +00:00
|
|
|
builder.filter_module(&format!("nu::{}", item), LevelFilter::Debug);
|
2019-10-28 14:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 16:49:45 +00:00
|
|
|
builder.try_init()?;
|
|
|
|
|
2020-02-09 02:24:33 +00:00
|
|
|
match matches.values_of("commands") {
|
|
|
|
None => {}
|
|
|
|
Some(values) => {
|
2020-12-18 07:53:49 +00:00
|
|
|
let script_text: String = values
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
|
|
|
futures::executor::block_on(nu_cli::run_script_file(
|
|
|
|
script_text,
|
2020-04-15 17:50:35 +00:00
|
|
|
matches.is_present("stdin"),
|
|
|
|
))?;
|
2020-02-09 02:24:33 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 16:49:45 +00:00
|
|
|
match matches.value_of("script") {
|
|
|
|
Some(script) => {
|
2020-12-18 07:53:49 +00:00
|
|
|
let mut file = File::open(script)?;
|
|
|
|
let mut buffer = String::new();
|
|
|
|
file.read_to_string(&mut buffer)?;
|
|
|
|
|
|
|
|
futures::executor::block_on(nu_cli::run_script_file(
|
|
|
|
buffer,
|
2020-04-15 17:50:35 +00:00
|
|
|
matches.is_present("stdin"),
|
|
|
|
))?;
|
2020-02-10 16:49:45 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
2020-09-16 23:22:58 +00:00
|
|
|
let mut context = create_default_context(true)?;
|
2020-09-14 14:07:02 +00:00
|
|
|
|
|
|
|
if !matches.is_present("skip-plugins") {
|
|
|
|
let _ = nu_cli::register_plugins(&mut context);
|
|
|
|
}
|
|
|
|
|
2020-09-17 06:02:30 +00:00
|
|
|
#[cfg(feature = "rustyline-support")]
|
|
|
|
{
|
|
|
|
futures::executor::block_on(nu_cli::cli(context))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "rustyline-support"))]
|
|
|
|
{
|
|
|
|
println!("Nushell needs the 'rustyline-support' feature for CLI support");
|
|
|
|
}
|
2020-02-10 16:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 17:00:42 +00:00
|
|
|
|
2019-05-23 07:23:06 +00:00
|
|
|
Ok(())
|
2019-05-15 16:12:38 +00:00
|
|
|
}
|