use crate::add::Style;
use bpaf::short;
use bpaf::Bpaf;
use bpaf::Parser;
use bpaf::ShellComp;
use camino::Utf8PathBuf;
use itertools::Itertools;
use strum::EnumMessage;
use strum::IntoEnumIterator;
fn style() -> impl Parser<Style> {
const DEFAULT: Style = Style::Auto;
let iter = Style::iter().map(|x| -> String {
if x == DEFAULT {
format!("{x} (default)")
} else {
x.to_string()
}
});
let help_msg = format!(
"Style of newly generated modify script [{}]",
Itertools::intersperse(iter, ", ".to_string()).collect::<String>()
);
fn complete_fn(input: &String) -> Vec<(&'static str, Option<&'static str>)> {
Style::iter()
.map(|x| {
(
<Style as Into<&'static str>>::into(x),
x.get_documentation(),
)
})
.filter(|(name, _)| name.starts_with(input))
.collect()
}
short('t')
.long("style")
.help(help_msg.as_str())
.argument::<String>("STYLE")
.complete(complete_fn)
.parse(|x| x.parse())
.fallback(DEFAULT)
}
#[derive(Debug, Bpaf)]
#[bpaf(options, version)]
pub enum ChmmArgs {
Process(#[bpaf(positional("FILE"), complete_shell(ShellComp::File{mask: None}))] Utf8PathBuf),
Add {
#[bpaf(short('a'), long("add"))]
_a: (),
#[bpaf(external)]
style: Style,
#[bpaf(positional("FILE"), complete_shell(ShellComp::File{mask: None}))]
files: Vec<Utf8PathBuf>,
},
Smart {
#[bpaf(short('s'), long("smart-add"))]
_a: (),
#[bpaf(positional("FILE"), complete_shell(ShellComp::File{mask: None}))]
files: Vec<Utf8PathBuf>,
},
HelpSyntax {
#[bpaf(long("help-syntax"))]
_a: (),
},
HelpTransforms {
#[bpaf(long("help-transforms"))]
_a: (),
},
Doctor {
#[bpaf(long("doctor"))]
_a: (),
},
Update {
#[bpaf(short('u'), long("upgrade"))]
_a: (),
#[bpaf(long("no-confirm"))]
no_confirm: bool,
},
}
fn footer() -> bpaf::Doc {
let mut doc = bpaf::Doc::default();
doc.text("The --style flag controls how the script that --add generates looks:\n \n");
for style in Style::iter() {
let mut text = format!(" * {}: {}", style, style.get_documentation().unwrap());
if !text.ends_with('\n') {
text.push('\n');
}
doc.text(&text);
}
doc
}
pub fn parse_args() -> ChmmArgs {
chmm_args().footer(footer()).run()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_options() {
chmm_args().check_invariants(false);
}
}