use std::{
ffi::OsString,
fmt::Debug,
panic::{RefUnwindSafe, UnwindSafe},
};
pub(crate) use crate::flags::{
complete::{
bash::generate as generate_complete_bash,
fish::generate as generate_complete_fish,
powershell::generate as generate_complete_powershell,
zsh::generate as generate_complete_zsh,
},
doc::{
help::{
generate_long as generate_help_long,
generate_short as generate_help_short,
},
man::generate as generate_man_page,
version::{
generate_long as generate_version_long,
generate_pcre2 as generate_version_pcre2,
generate_short as generate_version_short,
},
},
hiargs::HiArgs,
lowargs::{GenerateMode, Mode, SearchMode, SpecialMode},
parse::{ParseResult, parse},
};
mod complete;
mod config;
mod defs;
mod doc;
mod hiargs;
mod lowargs;
mod parse;
trait Flag: Debug + Send + Sync + UnwindSafe + RefUnwindSafe + 'static {
fn is_switch(&self) -> bool;
fn name_short(&self) -> Option<u8> {
None
}
fn name_long(&self) -> &'static str;
fn aliases(&self) -> &'static [&'static str] {
&[]
}
fn name_negated(&self) -> Option<&'static str> {
None
}
fn doc_variable(&self) -> Option<&'static str> {
None
}
fn doc_category(&self) -> Category;
fn doc_short(&self) -> &'static str;
fn doc_long(&self) -> &'static str;
fn doc_choices(&self) -> &'static [&'static str] {
&[]
}
fn completion_type(&self) -> CompletionType {
CompletionType::Other
}
fn update(
&self,
value: FlagValue,
args: &mut crate::flags::lowargs::LowArgs,
) -> anyhow::Result<()>;
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
enum Category {
Input,
Search,
Filter,
Output,
OutputModes,
Logging,
OtherBehaviors,
}
impl Category {
fn as_str(&self) -> &'static str {
match *self {
Category::Input => "input",
Category::Search => "search",
Category::Filter => "filter",
Category::Output => "output",
Category::OutputModes => "output-modes",
Category::Logging => "logging",
Category::OtherBehaviors => "other-behaviors",
}
}
}
#[derive(Clone, Copy, Debug)]
enum CompletionType {
Other,
Filename,
Executable,
Filetype,
Encoding,
}
#[derive(Debug)]
enum FlagValue {
Switch(bool),
Value(OsString),
}
impl FlagValue {
fn unwrap_switch(self) -> bool {
match self {
FlagValue::Switch(yes) => yes,
FlagValue::Value(_) => {
unreachable!("got flag value but expected switch")
}
}
}
fn unwrap_value(self) -> OsString {
match self {
FlagValue::Switch(_) => {
unreachable!("got switch but expected flag value")
}
FlagValue::Value(v) => v,
}
}
}