use clap::{Arg, ArgMatches};
use log::LevelFilter;
fn build_output_toggles<'a>() -> Vec<Arg<'a, 'a>> {
let mut toggles = Vec::new();
if cfg!(feature = "out-pgpfone") {
toggles.push(
Arg::with_name("no-pgpfone")
.long("no-pgpfone")
.help("Disable emission of PGPfone mnemonic words."),
);
}
if cfg!(feature = "out-skeyotp") {
toggles.push(
Arg::with_name("no-skeyotp")
.long("no-skeyotp")
.help("Disable emission of S/KEY OTP mnemonic words."),
);
}
if cfg!(feature = "out-qrcode") {
toggles.push(
Arg::with_name("no-qrcode")
.long("no-qrcode")
.help("Disable emission of QR codes."),
);
}
if cfg!(feature = "out-bip39") {
toggles.push(
Arg::with_name("no-bip39")
.long("no-bip39")
.help("Disable emission of BIP-39 mnemonic words."),
);
}
if cfg!(feature = "out-diceware") {
toggles.push(
Arg::with_name("no-diceware")
.long("no-diceware")
.help("Disable emission of EFF Large Diceware mnemonic words."),
);
}
toggles
}
pub fn configs<'a>() -> Vec<Arg<'a, 'a>> {
let mut args = build_output_toggles();
args.push(
Arg::with_name("input")
.short("i")
.long("input")
.takes_value(true)
.value_name("FILE")
.help("Input file to encode (default: stdin)."),
);
args.push(
Arg::with_name("font-dir")
.long("font-dir")
.takes_value(true)
.value_name("DIR")
.help("Directory containing a monospace font family for PDF output."),
);
args.push(
Arg::with_name("font-name")
.long("font-name")
.takes_value(true)
.value_name("NAME")
.help("Font family name to load for PDF output (e.g. ProggyCleanNerdFontMono)."),
);
args.push(
Arg::with_name("template")
.long("template")
.takes_value(true)
.value_name("FILE")
.help("HTML template file to use for .html output (see doc/html-templates.md)."),
);
args.push(
Arg::with_name("debug")
.long("debug")
.help("Enable debug mode to emit more details."),
);
args.push(
Arg::with_name("FILE_NAME")
.required(true)
.index(1)
.help("The output path/file; extension determines format (.txt or .pdf)."),
);
args
}
pub struct HardCopyConfig {
debug: bool,
pub out_file: String,
pub input_file: Option<String>,
pub pgpfone_enabled: bool,
pub skeyotp_enabled: bool,
pub qrcode_enabled: bool,
pub bip39_enabled: bool,
pub diceware_enabled: bool,
pub font_dir: Option<String>,
pub font_name: Option<String>,
pub html_template: Option<String>,
}
impl HardCopyConfig {
pub fn get_log_level(&self) -> LevelFilter {
if self.debug {
LevelFilter::Debug
} else {
LevelFilter::Warn
}
}
}
impl<'a> From<ArgMatches<'a>> for HardCopyConfig {
fn from(matches: ArgMatches<'a>) -> Self {
let debug = matches.is_present("debug");
let out_file = matches
.value_of("FILE_NAME")
.expect("Output file name is required, clap should enforce this.")
.to_string();
let input_file = matches.value_of("input").map(str::to_string);
let font_dir = matches.value_of("font-dir").map(str::to_string);
let font_name = matches.value_of("font-name").map(str::to_string);
let pgpfone_enabled = !matches.is_present("no-pgpfone");
let skeyotp_enabled = !matches.is_present("no-skeyotp");
let qrcode_enabled = !matches.is_present("no-qrcode");
let bip39_enabled = !matches.is_present("no-bip39");
let diceware_enabled = !matches.is_present("no-diceware");
let html_template = matches.value_of("template").map(str::to_string);
HardCopyConfig {
debug,
out_file,
input_file,
pgpfone_enabled,
skeyotp_enabled,
qrcode_enabled,
bip39_enabled,
diceware_enabled,
font_dir,
font_name,
html_template,
}
}
}