mirror of
https://github.com/Noratrieb/jsonformat.git
synced 2026-01-14 22:25:01 +01:00
options
This commit is contained in:
parent
d0733b1a7c
commit
9b7260660e
5 changed files with 320 additions and 30 deletions
26
src/main.rs
26
src/main.rs
|
|
@ -1,12 +1,21 @@
|
|||
use clap::clap_app;
|
||||
use jsonformat::format_json;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let filename = std::env::args().skip(1).next();
|
||||
let matches = clap_app!(jsonformat =>
|
||||
(version: "1.0")
|
||||
(author: "nilstrieb <nilstrieb@gmail.com>")
|
||||
(about: "Formats json")
|
||||
(@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)")
|
||||
(@arg output: -o --output +takes_value "The output file for the formatted json")
|
||||
(@arg input: "The input file to format")
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let str = match filename {
|
||||
let str = match matches.value_of("input") {
|
||||
Some(path) => fs::read_to_string(path)?,
|
||||
None => {
|
||||
let mut buf = String::new();
|
||||
|
|
@ -16,7 +25,18 @@ fn main() -> Result<(), io::Error> {
|
|||
}
|
||||
};
|
||||
|
||||
println!("{}", format_json(&str, " "));
|
||||
let replaced_indent = matches
|
||||
.value_of("indentation")
|
||||
.map(|value| value.replace("s", " ").replace("t", "\t"));
|
||||
|
||||
let formatted = format_json(&str, replaced_indent.as_deref());
|
||||
|
||||
match matches.value_of("output") {
|
||||
Some(file) => {
|
||||
fs::write(file, formatted)?;
|
||||
}
|
||||
None => println!("{}", formatted),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue