use std::io::BufRead;
#[cfg(feature = "regex")]
use regex::Regex;
use ncase::Words;
#[derive(argh::FromArgs)]
struct Cli {
#[cfg(feature = "regex")]
#[argh(option, short = 'F')]
separator: Option<Regex>,
#[argh(switch, short = 'c')]
camel: bool,
#[argh(switch, short = 'P')]
pascal: bool,
#[argh(switch, short = 'k')]
kebab: bool,
#[argh(switch, short = 'K')]
screaming_kebab: bool,
#[argh(switch, short = 'l')]
lower: bool,
#[argh(switch, short = 'U')]
upper: bool,
#[argh(switch, short = 's')]
snake: bool,
#[argh(switch, short = 'S')]
screaming_snake: bool,
#[argh(switch, short = 't')]
title: bool,
#[allow(dead_code)]
#[argh(switch, short = 'T')]
toggle: bool,
#[cfg(feature = "rand")]
#[argh(switch, short = 'r')]
_random: bool,
#[argh(positional)]
args: Vec<String>,
}
fn main() -> anyhow::Result<()> {
let cli: Cli = argh::from_env();
if !cli.args.is_empty() {
println!("{}", ncase(&cli.args.join(" "), &cli));
} else {
for line_res in std::io::stdin().lock().lines() {
println!("{}", ncase(&line_res?, &cli));
}
}
Ok(())
}
fn ncase(s: &str, cli: &Cli) -> String {
#[cfg(feature = "regex")]
let w = match &cli.separator {
Some(sep) => Words::with_separator(s, sep),
None => Words::from(s),
};
#[cfg(not(feature = "regex"))]
let w = Words::from(s);
#[allow(clippy::suspicious_else_formatting)]
#[rustfmt::skip]
let res =
if cli.camel { w.camel() } else
if cli.pascal { w.pascal() } else
if cli.kebab { w.kebab() } else
if cli.screaming_kebab { w.screaming_kebab() } else
if cli.lower { w.lower() } else
if cli.upper { w.upper() } else
if cli.snake { w.snake() } else
if cli.screaming_snake { w.screaming_snake() } else
if cli.title { w.title() } else
{
#[cfg(feature = "rand")]
let res =
if cli.toggle { w.toggle() } else
{ w.random() };
#[cfg(not(feature = "rand"))]
let res = w.toggle();
res
};
res
}