mirror of
https://github.com/jht5945/prettyjson.git
synced 2025-12-29 18:30:05 +08:00
use Options
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1,3 +1,5 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "argon2rs"
|
name = "argon2rs"
|
||||||
version = "0.2.5"
|
version = "0.2.5"
|
||||||
|
|||||||
34
src/main.rs
34
src/main.rs
@@ -30,34 +30,42 @@ fn read_to_string(read: &mut Read) -> XResult<String> {
|
|||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Options {
|
||||||
|
version: bool,
|
||||||
|
tab_width: u16,
|
||||||
|
file: String,
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut version = false;
|
let mut options = Options {
|
||||||
let mut tab_width = 4u16;
|
version: false,
|
||||||
let mut file = String::new();
|
tab_width: 4u16,
|
||||||
|
file: String::new(),
|
||||||
|
};
|
||||||
{
|
{
|
||||||
let mut ap = ArgumentParser::new();
|
let mut ap = ArgumentParser::new();
|
||||||
ap.set_description("prettyjson - command line JSON pretty tool.");
|
ap.set_description("prettyjson - command line JSON pretty tool.");
|
||||||
ap.refer(&mut tab_width).add_option(&["-w", "--tab-width"], Store, "Tab width, default 4");
|
ap.refer(&mut options.tab_width).add_option(&["-w", "--tab-width"], Store, "Tab width, default 4");
|
||||||
ap.refer(&mut version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
ap.refer(&mut options.version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
||||||
ap.refer(&mut file).add_argument("FILE", Store, "FILE");
|
ap.refer(&mut options.file).add_argument("FILE", Store, "FILE");
|
||||||
ap.parse_args_or_exit();
|
ap.parse_args_or_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if version {
|
if options.version {
|
||||||
print_version();
|
print_version();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if tab_width > 100 {
|
if options.tab_width > 100 {
|
||||||
print_message(MessageType::ERROR, &format!("Tab width is invalid: {}", tab_width));
|
print_message(MessageType::ERROR, &format!("Tab width is invalid: {}", options.tab_width));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let read: XResult<String> = match file.len() {
|
let read: XResult<String> = match options.file.len() {
|
||||||
0 => read_to_string(&mut io::stdin()),
|
0 => read_to_string(&mut io::stdin()),
|
||||||
_ => match File::open(&file) {
|
_ => match File::open(&options.file) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
print_message(MessageType::ERROR, &format!("Open file: {}, failed: {}", &file, err));
|
print_message(MessageType::ERROR, &format!("Open file: {}, failed: {}", &options.file, err));
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
Ok(mut f) => read_to_string(&mut f),
|
Ok(mut f) => read_to_string(&mut f),
|
||||||
@@ -78,5 +86,5 @@ fn main() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{}", json::stringify_pretty(json_object, tab_width));
|
println!("{}", json::stringify_pretty(json_object, options.tab_width));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user