use clap::{ArgEnum, CommandFactory, ErrorKind, Parser};
use std::path::PathBuf;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum ColorMode {
Never,
Auto,
Always,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum EscapeMode {
No,
Yes,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum KeyOrder {
Asc,
Desc,
}
#[cfg(debugger_helper)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum DebuggerHelper {
Gdb,
}
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
#[clap(short = '0', long)]
pub null: bool,
#[cfg(remote_env)]
#[clap(long, required = false)]
pub pid: Option<u32>,
#[cfg(debugger_helper)]
#[clap(long, arg_enum, required = false)]
pub debugger_helper: Option<DebuggerHelper>,
#[clap(long, arg_enum, required = false)]
pub color: Option<ColorMode>,
#[clap(long, arg_enum, required = false)]
pub key_order: Option<KeyOrder>,
#[clap(long, arg_enum, required = false)]
pub escape: Option<EscapeMode>,
#[clap(long, parse(from_os_str), required = false)]
pub load: Option<PathBuf>,
#[clap(long)]
pub json: bool,
#[clap(required = false)]
pub variables: Vec<String>,
}
pub fn parse() -> Args {
let args = Args::parse();
if args.color == Some(ColorMode::Never) {
colored::control::set_override(false);
}
if args.null && (args.color == Some(ColorMode::Always) || args.escape == Some(EscapeMode::Yes))
{
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Null mode cannot be used together with rich format output, such as color mode or escape mode",
).exit();
}
if !args.variables.is_empty() && args.key_order.is_some() {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Providing VARIABLES does not work with key-order mode",
)
.exit();
}
#[cfg(debugger_helper)]
#[allow(clippy::collapsible_if)]
if args.debugger_helper.is_some() {
if args.null
|| (args.color == Some(ColorMode::Always) || args.escape == Some(EscapeMode::Yes))
|| !args.variables.is_empty()
|| args.key_order.is_some()
|| args.json
{
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"--debugger-helper does not work with other arguments",
)
.exit();
}
}
#[cfg(remote_env)]
if args.pid.is_some() && args.load.is_some() {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"--pid and --load cannot be used together",
)
.exit();
}
if args.null && args.load.is_some() {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"--null and --load cannot be used together",
)
.exit();
}
if args.json {
if args.null {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"--null and --json cannot be used together",
)
.exit();
}
if args.load.is_some() {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"--load and --json cannot be used together",
)
.exit();
}
if args.color == Some(ColorMode::Always) || args.escape == Some(EscapeMode::Yes) {
let mut cmd = Args::command();
cmd.error(
ErrorKind::ArgumentConflict,
"JSON mode cannot be used together with rich format output, such as color mode or escape mode",
)
.exit();
}
}
args
}