47 lines
1.6 KiB
Rust
47 lines
1.6 KiB
Rust
use rust_util::XResult;
|
|
use argparse::{ArgumentParser, StoreTrue, Store, List};
|
|
|
|
pub struct Options {
|
|
pub version: bool,
|
|
pub verbose: bool,
|
|
pub ignore_errors: bool,
|
|
pub algorithm: String,
|
|
pub file_name_list: Vec<String>,
|
|
pub blake_len: usize,
|
|
}
|
|
|
|
impl Options {
|
|
pub fn new() -> Options {
|
|
Options {
|
|
version: false,
|
|
verbose: false,
|
|
ignore_errors: false,
|
|
algorithm: "SHA256".to_string(),
|
|
file_name_list: Vec::new(),
|
|
blake_len: 0_usize,
|
|
}
|
|
}
|
|
|
|
pub fn parse_args(&mut self) -> XResult<()> {
|
|
{
|
|
let mut ap = ArgumentParser::new();
|
|
ap.set_description("digest - command line digest tool.");
|
|
ap.refer(&mut self.algorithm).add_option(&["-a", "--algorithm"], Store, "Algorithm, e.g. SHA256, SM3");
|
|
ap.refer(&mut self.blake_len).add_option(&["-l", "--blake-len"], Store, "Blake2s/b length, 1~32/64");
|
|
ap.refer(&mut self.version).add_option(&["-V", "--version"], StoreTrue, "Print version");
|
|
ap.refer(&mut self.verbose).add_option(&["-v", "--verbose"], StoreTrue, "Verbose");
|
|
ap.refer(&mut self.ignore_errors).add_option(&["--ignore-errors"], StoreTrue, "Ignore errors");
|
|
ap.refer(&mut self.file_name_list).add_argument("File names", List, "File names to be digested");
|
|
ap.parse_args_or_exit();
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn new_and_parse_args() -> XResult<Options> {
|
|
let mut options = Options::new();
|
|
options.parse_args()?;
|
|
Ok(options)
|
|
}
|
|
}
|