add silent flag (#74)

This commit is contained in:
Clément DOUIN
2021-04-08 18:42:34 +02:00
parent 8e6740db65
commit ebf1b854be
7 changed files with 43 additions and 27 deletions
+14 -8
View File
@@ -1,11 +1,17 @@
use clap::Arg;
pub fn output_arg<'a>() -> Arg<'a, 'a> {
Arg::with_name("output")
.long("output")
.short("o")
.help("Defines the output format")
.value_name("STRING")
.possible_values(&["plain", "json"])
.default_value("plain")
pub fn output_args<'a>() -> Vec<Arg<'a, 'a>> {
vec![
Arg::with_name("output")
.long("output")
.short("o")
.help("Defines the output format")
.value_name("STRING")
.possible_values(&["plain", "json"])
.default_value("plain"),
Arg::with_name("silent")
.long("silent")
.short("s")
.help("Disables any output"),
]
}
+9 -7
View File
@@ -41,13 +41,15 @@ pub fn run_cmd(cmd: &str) -> Result<String> {
Ok(String::from_utf8(output.stdout)?)
}
pub fn print<T: fmt::Display + Serialize>(output_type: &str, item: T) -> Result<()> {
match output_type {
"json" => print!(
"{}",
serde_json::to_string(&item).chain_err(|| "Could not decode JSON")?
),
"text" | _ => println!("{}", item.to_string()),
pub fn print<T: fmt::Display + Serialize>(output_type: &str, silent: &bool, item: T) -> Result<()> {
if silent == &false {
match output_type {
"json" => print!(
"{}",
serde_json::to_string(&item).chain_err(|| "Could not decode JSON")?
),
"text" | _ => println!("{}", item.to_string()),
}
}
Ok(())