From 60b051e49e6951bb1b1120d51be579f6dafaa238 Mon Sep 17 00:00:00 2001 From: "Hatter Jiang@Pixelbook" Date: Sat, 31 Aug 2019 14:57:10 +0800 Subject: [PATCH] add opt.rs --- src/main.rs | 34 +++------------------------------- src/opt.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/opt.rs diff --git a/src/main.rs b/src/main.rs index cf14492..0eb5ae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ extern crate term; extern crate json; extern crate rust_util; +mod opt; + use std::{ fs::{self, File}, io::{ @@ -10,11 +12,11 @@ use std::{ prelude::*, } }; -use argparse::{ArgumentParser, StoreTrue, Store}; use rust_util::{ XResult, util_msg::*, }; +use opt::*; const VERSION: &str = env!("CARGO_PKG_VERSION"); const GIT_HASH: &str = env!("GIT_HASH"); @@ -34,36 +36,6 @@ fn read_to_string(read: &mut dyn Read) -> XResult { Ok(buffer) } -struct Options { - version: bool, - verbose: bool, - replace_file: bool, - tab_width: u16, - file: String, -} - -impl Options { - pub fn new() -> Options { - Options { - version: false, - verbose: false, - replace_file: false, - tab_width: 4u16, - file: String::new(), - } - } - - pub fn parse_args(&mut self) { - let mut ap = ArgumentParser::new(); - ap.set_description("prettyjson - command line JSON pretty tool."); - ap.refer(&mut self.tab_width).add_option(&["-w", "--tab-width"], Store, "Tab width, default 4"); - 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.replace_file).add_option(&["-R", "--replace-file"], StoreTrue, "Replace file"); - ap.refer(&mut self.file).add_argument("FILE", Store, "FILE"); - ap.parse_args_or_exit(); - } -} fn main() { let mut options = Options::new(); diff --git a/src/opt.rs b/src/opt.rs new file mode 100644 index 0000000..57e9e6b --- /dev/null +++ b/src/opt.rs @@ -0,0 +1,33 @@ + +use argparse::{ArgumentParser, StoreTrue, Store}; + +pub struct Options { + pub version: bool, + pub verbose: bool, + pub replace_file: bool, + pub tab_width: u16, + pub file: String, +} + +impl Options { + pub fn new() -> Options { + Options { + version: false, + verbose: false, + replace_file: false, + tab_width: 4u16, + file: String::new(), + } + } + + pub fn parse_args(&mut self) { + let mut ap = ArgumentParser::new(); + ap.set_description("prettyjson - command line JSON pretty tool."); + ap.refer(&mut self.tab_width).add_option(&["-w", "--tab-width"], Store, "Tab width, default 4"); + 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.replace_file).add_option(&["-R", "--replace-file"], StoreTrue, "Replace file"); + ap.refer(&mut self.file).add_argument("FILE", Store, "FILE"); + ap.parse_args_or_exit(); + } +} \ No newline at end of file