add opt.rs
This commit is contained in:
43
src/main.rs
43
src/main.rs
@@ -1,3 +1,5 @@
|
|||||||
|
mod opt;
|
||||||
|
|
||||||
use rust_util::{
|
use rust_util::{
|
||||||
XResult,
|
XResult,
|
||||||
new_box_ioerror,
|
new_box_ioerror,
|
||||||
@@ -30,8 +32,10 @@ use indicatif::{
|
|||||||
ProgressBar,
|
ProgressBar,
|
||||||
ProgressStyle
|
ProgressStyle
|
||||||
};
|
};
|
||||||
use argparse::{ArgumentParser, StoreTrue, Store, List};
|
|
||||||
use libsm::sm3::hash::Sm3Hash;
|
use libsm::sm3::hash::Sm3Hash;
|
||||||
|
use opt::{
|
||||||
|
Options,
|
||||||
|
};
|
||||||
|
|
||||||
const FILE_SIZE_1GB: u64 = 1024 * 1024 * 1024;
|
const FILE_SIZE_1GB: u64 = 1024 * 1024 * 1024;
|
||||||
const BUFF_SIZE: usize = 512 * 1024;
|
const BUFF_SIZE: usize = 512 * 1024;
|
||||||
@@ -41,42 +45,6 @@ const PB_TEMPLATE: &str = "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/bl
|
|||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const GIT_HASH: &str = env!("GIT_HASH");
|
const GIT_HASH: &str = env!("GIT_HASH");
|
||||||
|
|
||||||
|
|
||||||
pub struct Options {
|
|
||||||
pub version: bool,
|
|
||||||
pub algorithm: String,
|
|
||||||
pub file_name_list: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Options {
|
|
||||||
pub fn new() -> Options {
|
|
||||||
Options {
|
|
||||||
version: false,
|
|
||||||
algorithm: "SHA256".to_string(),
|
|
||||||
file_name_list: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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.version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
let options = Options::new_and_parse_args()?;
|
let options = Options::new_and_parse_args()?;
|
||||||
|
|
||||||
@@ -206,7 +174,6 @@ fn calc_file_digest(digest: &mut dyn Digest, file_name: &str) -> XResult<String>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn read_full_stdin() -> XResult<Vec<u8>> {
|
fn read_full_stdin() -> XResult<Vec<u8>> {
|
||||||
let mut buf: [u8; BUFF_SIZE] = [0u8; BUFF_SIZE];
|
let mut buf: [u8; BUFF_SIZE] = [0u8; BUFF_SIZE];
|
||||||
let mut ret: Vec<u8> = Vec::new();
|
let mut ret: Vec<u8> = Vec::new();
|
||||||
|
|||||||
37
src/opt.rs
Normal file
37
src/opt.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use rust_util::XResult;
|
||||||
|
use argparse::{ArgumentParser, StoreTrue, Store, List};
|
||||||
|
|
||||||
|
pub struct Options {
|
||||||
|
pub version: bool,
|
||||||
|
pub algorithm: String,
|
||||||
|
pub file_name_list: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Options {
|
||||||
|
pub fn new() -> Options {
|
||||||
|
Options {
|
||||||
|
version: false,
|
||||||
|
algorithm: "SHA256".to_string(),
|
||||||
|
file_name_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user