2021-04-04 07:14:58 +02:00
|
|
|
use clap::{App, Arg};
|
2019-06-01 19:00:42 +02:00
|
|
|
use log::LevelFilter;
|
2021-04-04 07:14:58 +02:00
|
|
|
use nu_cli::{create_default_context, NuScript, Options};
|
2021-01-12 05:59:53 +01:00
|
|
|
use nu_command::utils::test_bins as binaries;
|
2021-04-04 07:14:58 +02:00
|
|
|
use std::error::Error;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-06-03 05:48:58 +02:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-03-15 08:26:30 +01:00
|
|
|
let mut options = Options::new();
|
|
|
|
|
2019-07-20 03:12:04 +02:00
|
|
|
let matches = App::new("nushell")
|
2019-08-24 17:25:04 +02:00
|
|
|
.version(clap::crate_version!())
|
2021-03-15 08:26:30 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("config-file")
|
|
|
|
.long("config-file")
|
|
|
|
.help("custom configuration source file")
|
|
|
|
.hidden(true)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2021-03-31 07:52:34 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("no-history")
|
|
|
|
.hidden(true)
|
|
|
|
.long("no-history")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
2019-06-01 19:00:42 +02: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 16:07:02 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("skip-plugins")
|
|
|
|
.hidden(true)
|
|
|
|
.long("skip-plugins")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
2020-05-18 05:52:56 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("testbin")
|
|
|
|
.hidden(true)
|
|
|
|
.long("testbin")
|
|
|
|
.value_name("TESTBIN")
|
2021-03-31 07:52:34 +02:00
|
|
|
.possible_values(&[
|
|
|
|
"echo_env", "cococo", "iecho", "fail", "nonu", "chop", "repeater",
|
|
|
|
])
|
2020-05-18 05:52:56 +02:00
|
|
|
.takes_value(true),
|
|
|
|
)
|
2020-02-09 03:24:33 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("commands")
|
|
|
|
.short("c")
|
|
|
|
.long("commands")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2019-06-01 19:00:42 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("develop")
|
|
|
|
.long("develop")
|
|
|
|
.multiple(true)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2019-10-28 15:46:50 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("debug")
|
|
|
|
.long("debug")
|
|
|
|
.multiple(true)
|
|
|
|
.takes_value(true),
|
|
|
|
)
|
2020-02-10 07:55:07 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("stdin")
|
|
|
|
.long("stdin")
|
|
|
|
.multiple(false)
|
|
|
|
.takes_value(false),
|
|
|
|
)
|
2020-02-10 17:49:45 +01:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("script")
|
|
|
|
.help("the nu script to run")
|
|
|
|
.index(1),
|
|
|
|
)
|
2020-05-25 01:26:27 +02:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("args")
|
|
|
|
.help("positional args (used by --testbin)")
|
|
|
|
.index(2)
|
|
|
|
.multiple(true),
|
|
|
|
)
|
2019-06-01 19:00:42 +02:00
|
|
|
.get_matches();
|
|
|
|
|
2020-05-18 05:52:56 +02:00
|
|
|
if let Some(bin) = matches.value_of("testbin") {
|
|
|
|
match bin {
|
2021-03-31 07:52:34 +02:00
|
|
|
"echo_env" => binaries::echo_env(),
|
2020-05-18 05:52:56 +02:00
|
|
|
"cococo" => binaries::cococo(),
|
|
|
|
"iecho" => binaries::iecho(),
|
|
|
|
"fail" => binaries::fail(),
|
|
|
|
"nonu" => binaries::nonu(),
|
|
|
|
"chop" => binaries::chop(),
|
2021-03-10 23:35:15 +01:00
|
|
|
"repeater" => binaries::repeater(),
|
2020-05-18 05:52:56 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-03-15 08:26:30 +01:00
|
|
|
options.config = matches
|
|
|
|
.value_of("config-file")
|
|
|
|
.map(std::ffi::OsString::from);
|
|
|
|
options.stdin = matches.is_present("stdin");
|
2021-03-27 06:08:03 +01:00
|
|
|
options.save_history = !matches.is_present("no-history");
|
2021-03-15 08:26:30 +01:00
|
|
|
|
2019-06-01 19:00:42 +02: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 23:15:21 +02:00
|
|
|
Some(values) => {
|
|
|
|
for item in values {
|
2020-03-05 11:18:53 +01:00
|
|
|
builder.filter_module(&format!("nu::{}", item), LevelFilter::Trace);
|
2019-06-01 23:15:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 19:00:42 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:46:50 +01:00
|
|
|
match matches.values_of("debug") {
|
|
|
|
None => {}
|
|
|
|
Some(values) => {
|
|
|
|
for item in values {
|
2020-03-05 11:18:53 +01:00
|
|
|
builder.filter_module(&format!("nu::{}", item), LevelFilter::Debug);
|
2019-10-28 15:46:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:49:45 +01:00
|
|
|
builder.try_init()?;
|
|
|
|
|
2020-02-09 03:24:33 +01:00
|
|
|
match matches.values_of("commands") {
|
|
|
|
None => {}
|
|
|
|
Some(values) => {
|
2021-04-04 07:14:58 +02:00
|
|
|
options.scripts = vec![NuScript::code(values)?];
|
|
|
|
|
2021-04-14 20:21:50 +02:00
|
|
|
let context = create_default_context(false)?;
|
|
|
|
nu_cli::run_script_file(context, options)?;
|
2020-02-09 03:24:33 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:49:45 +01:00
|
|
|
match matches.value_of("script") {
|
2021-03-15 08:26:30 +01:00
|
|
|
Some(filepath) => {
|
2021-04-04 07:14:58 +02:00
|
|
|
let filepath = std::ffi::OsString::from(filepath);
|
|
|
|
|
|
|
|
options.scripts = vec![NuScript::source_file(filepath.as_os_str())?];
|
|
|
|
|
2021-04-14 20:21:50 +02:00
|
|
|
let context = create_default_context(false)?;
|
|
|
|
nu_cli::run_script_file(context, options)?;
|
2020-02-10 17:49:45 +01:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
None => {
|
2021-04-04 07:14:58 +02:00
|
|
|
let context = create_default_context(true)?;
|
2020-09-14 16:07:02 +02:00
|
|
|
|
|
|
|
if !matches.is_present("skip-plugins") {
|
2021-03-31 07:52:34 +02:00
|
|
|
let _ = nu_cli::register_plugins(&context);
|
2020-09-14 16:07:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 08:02:30 +02:00
|
|
|
#[cfg(feature = "rustyline-support")]
|
|
|
|
{
|
2021-04-06 18:19:43 +02:00
|
|
|
nu_cli::cli(context, options)?;
|
2020-09-17 08:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "rustyline-support"))]
|
|
|
|
{
|
|
|
|
println!("Nushell needs the 'rustyline-support' feature for CLI support");
|
|
|
|
}
|
2020-02-10 17:49:45 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 19:00:42 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
Ok(())
|
2019-05-15 18:12:38 +02:00
|
|
|
}
|