Skip to content

Commit

Permalink
add pretty print flag
Browse files Browse the repository at this point in the history
  • Loading branch information
say4n committed Apr 27, 2024
1 parent 9fa1324 commit ba87a9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ $ echo '{"foo": {"bar": ["baz"]}}' | jf
```
-f, --filename <path_to_file.json>
-s, --separator <SEPARATOR> [default: .]
-p, --pretty
-h, --help Print help
-V, --version Print version
```
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::exit,
};

use clap::{command, CommandFactory, Parser};
use clap::{command, ArgAction, CommandFactory, Parser};
use jf::flatten;

#[derive(Parser, Debug)]
Expand All @@ -28,6 +28,9 @@ struct Args {

#[arg(short, long, default_value=".")]
separator: String,

#[arg(short, long, action=ArgAction::SetTrue)]
pretty: bool,
}

fn main() -> io::Result<()> {
Expand Down Expand Up @@ -55,6 +58,10 @@ fn main() -> io::Result<()> {
let json_tree = serde_json::from_slice(&buffer)?;
let flat_json_tree = flatten(json_tree, &args.separator);

println!("{}", &flat_json_tree);
if !args.pretty {
println!("{}", &flat_json_tree);
} else {
println!("{}", serde_json::to_string_pretty(&flat_json_tree)?);
}
Ok(())
}

0 comments on commit ba87a9c

Please sign in to comment.