use clap::{builder::styling, Parser};
use std::io::IsTerminal;
use crate::{events, productinfo};
const SHORT_DESCRIPTION: &str = "Bo[u]rn[e] RUsty SHell";
const LONG_DESCRIPTION: &str = r"
brush is a Rust-implemented, POSIX-style shell that aims to be compatible with bash.
brush is a work in progress. If you encounter any issues or discrepancies in behavior from bash, please report them at https://github.com/reubeno/brush.
";
const VERSION: &str = const_format::concatcp!(
productinfo::PRODUCT_VERSION,
" (",
productinfo::PRODUCT_GIT_VERSION,
")"
);
#[derive(Clone, clap::ValueEnum)]
pub enum InputBackend {
Rustyline,
Basic,
}
#[derive(Parser)]
#[clap(name = productinfo::PRODUCT_NAME,
version = VERSION,
about = SHORT_DESCRIPTION,
long_about = LONG_DESCRIPTION,
author,
disable_help_flag = true,
disable_version_flag = true,
styles = brush_help_styles())]
#[allow(clippy::module_name_repetitions)]
pub struct CommandLineArgs {
#[clap(long = "help", action = clap::ArgAction::HelpLong)]
pub help: Option<bool>,
#[clap(long = "version", action = clap::ArgAction::Version)]
pub version: Option<bool>,
#[arg(short = 'c', value_name = "COMMAND")]
pub command: Option<String>,
#[clap(short = 'i')]
pub interactive: bool,
#[clap(short = 'l', long = "login")]
pub login: bool,
#[clap(short = 'n')]
pub do_not_execute_commands: bool,
#[clap(long = "noediting")]
pub no_editing: bool,
#[clap(long = "noprofile")]
pub no_profile: bool,
#[clap(long = "norc")]
pub no_rc: bool,
#[clap(short = 'O', value_name = "OPTION")]
pub enabled_shopt_options: Vec<String>,
#[clap(long = "+O", hide = true)]
pub disabled_shopt_options: Vec<String>,
#[clap(long = "posix")]
pub posix: bool,
#[clap(short = 's')]
pub read_commands_from_stdin: bool,
#[clap(long = "sh")]
pub sh_mode: bool,
#[clap(short = 'v', long = "verbose")]
pub verbose: bool,
#[clap(short = 'x')]
pub print_commands_and_arguments: bool,
#[clap(long = "disable-bracketed-paste")]
pub disable_bracketed_paste: bool,
#[clap(long = "input-backend")]
pub input_backend: Option<InputBackend>,
#[clap(long = "log-enable", value_name = "EVENT")]
pub enabled_log_events: Vec<events::TraceEvent>,
#[clap(allow_hyphen_values = true)]
pub script_path: Option<String>,
#[clap(allow_hyphen_values = true, num_args=1..)]
pub script_args: Vec<String>,
}
impl CommandLineArgs {
pub fn is_interactive(&self) -> bool {
if self.interactive {
return true;
}
if self.command.is_some() || self.script_path.is_some() {
return false;
}
if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
return false;
}
true
}
}
#[doc(hidden)]
fn brush_help_styles() -> clap::builder::Styles {
styling::Styles::styled()
.header(
styling::AnsiColor::Yellow.on_default()
| styling::Effects::BOLD
| styling::Effects::UNDERLINE,
)
.usage(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.literal(styling::AnsiColor::Magenta.on_default() | styling::Effects::BOLD)
.placeholder(styling::AnsiColor::Cyan.on_default())
}