mirror of
https://github.com/jht5945/prettyjson.git
synced 2025-12-29 18:30:05 +08:00
add opt.rs
This commit is contained in:
34
src/main.rs
34
src/main.rs
@@ -3,6 +3,8 @@ extern crate term;
|
|||||||
extern crate json;
|
extern crate json;
|
||||||
extern crate rust_util;
|
extern crate rust_util;
|
||||||
|
|
||||||
|
mod opt;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{
|
io::{
|
||||||
@@ -10,11 +12,11 @@ use std::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
use argparse::{ArgumentParser, StoreTrue, Store};
|
|
||||||
use rust_util::{
|
use rust_util::{
|
||||||
XResult,
|
XResult,
|
||||||
util_msg::*,
|
util_msg::*,
|
||||||
};
|
};
|
||||||
|
use opt::*;
|
||||||
|
|
||||||
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");
|
||||||
@@ -34,36 +36,6 @@ fn read_to_string(read: &mut dyn Read) -> XResult<String> {
|
|||||||
Ok(buffer)
|
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() {
|
fn main() {
|
||||||
let mut options = Options::new();
|
let mut options = Options::new();
|
||||||
|
|||||||
33
src/opt.rs
Normal file
33
src/opt.rs
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user