You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
3.1 KiB

//! Argument and option hanlding
use super::*;
use std::{
env::args,
str::FromStr,
};
use lazy_static::lazy_static;
/// Program executable name
#[inline]
pub fn program() -> &'static str
{
lazy_static! {
static ref NAME: &'static str = Box::leak(args().next().unwrap().into_boxed_str());
}
&NAME
}
/// Print usage and exit with exit code 1.
pub fn usage() -> !
{
println!("naes: native file encryptor (prog. ver: {}, format ver: {})", env!("CARGO_PKG_VERSION"), crate::CURRENT_VERSION);
println!(" made by {} with <3", env!("CARGO_PKG_AUTHORS"));
println!(" licensed with GPL v3");
#[cfg(feature="threads")] println!(" (compiled with threaded support)");
println!();
println!("Usage: {} [OPTIONS...] [-e|-d|-v] [--] <files...>", program());
println!("Usage: {} --keygen RSA [--verbose] [--cycle <old private>] [--format TEXT|BIN|PEM] [--private-only|--public-only] [--password] <output> [<public output>]", program());
println!("Usage: {} --keygen AES [--verbose] [--cycle <old key>] [--format TEXT|BIN] [--password] <output>", program());
println!("Usage: {} --help", program());
println!();
println!("Options:");
println!(" --rsa[=keys,...] [<key>]\tEncrypt with this (or these) RSA keys.");
println!(" --sign[=keys,...] [<key>]\tSign with this (or these) RSA keys.");
println!(" --auto-sign\t\t\tWhen RSA encryption keys are provided, also sign with them.");
println!(" --aes[=keys,...] [<key>]\tEncrypt with this (or these) AES keys.");
println!(" --translate=[<in>,<out>] [<in> <out>]\tSpecify input and output filenames.");
println!(" --in-place\t\t\tDo not add/remove `.nenc` extension to/from output file.");
println!(" --verbose\t\t\tVerbose logging.");
#[cfg(feature="threads")]
println!(" --sync\t\t\tDo not process files concurrently.");
println!();
println!("Additional options:");
println!(" -e\t\tForce encryption mode. Default is autodetect.");
println!(" -d\t\tForce decryption mode. Default is autodetect.");
println!(" -v\t\tVerification mode. Checks integrity of encrypted files and validates signatures with provided signing keys.");
println!(" --\t\tStop reading args.");
println!();
println!("Keygen only options:");
println!(" --cycle <old key>\t\tRead <old key> and write out to output instead of generating new key.");
println!(" --format TEXT|BIN|PEM\t\tOutput key format. PEM is only valid for RSA outputs. Default is BIN.");
println!(" --private-only\t\tOnly valid for RSA outputs. Do not re-output public key.");
println!(" --public-only\t\t\tOnly valid for RSA outputs. Only output public key. Only useful with `--cycle`.");
println!(" --password[=text]\t\tPassword-protect this key.");
std::process::exit(1)
}
pub mod error;
use error::*;
mod data;
pub use data::*;
mod parse;
mod validate;
mod spec;
/// Parse the command line arguments passed.
pub async fn parse_args() -> Result<Operation, Error>
{
let mut args = std::env::args();
let _ = args.next();
parse::parse(&mut args).await?.try_into_validated()
}