feat(args): add --quiet flag for not displaying variables after set

This commit is contained in:
Orhun Parmaksız 2021-12-07 20:22:21 +03:00
parent 082af7b458
commit 762c25a84d
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 13 additions and 2 deletions

View file

@ -24,6 +24,8 @@ pub struct Config {
/// Sysctl configuration.
#[derive(Debug)]
pub struct AppConfig {
/// Whether if the quiet mode is enabled.
pub quiet: bool,
/// Whether if the colors are disabled.
pub no_color: bool,
/// Whether if the pager is disabled.
@ -39,6 +41,7 @@ pub struct AppConfig {
impl Default for AppConfig {
fn default() -> Self {
Self {
quiet: false,
no_color: false,
no_pager: false,
section_colors: map! {

View file

@ -144,6 +144,9 @@ impl Parameter {
let ctl = Ctl::new(&self.name)?;
let new_value = ctl.set_value_string(new_value)?;
self.value = new_value;
self.display_value(config, output)
if !config.quiet {
self.display_value(config, output)?;
}
Ok(())
}
}

View file

@ -19,6 +19,8 @@ For more details see {bin}(8)."#;
pub struct Args {
/// Whether if the verbose logging is enabled.
pub verbose: bool,
/// Whether if the quiet mode is enabled.
pub quiet: bool,
/// Path of the Linux kernel documentation.
pub kernel_docs: Option<PathBuf>,
/// Display type of the variables.
@ -52,6 +54,7 @@ impl Args {
"select setting that match expression",
"<expression>",
);
opts.optflag("q", "quiet", "do not echo variable set");
opts.optopt(
"E",
"explain",
@ -112,6 +115,7 @@ impl Args {
};
Some(Args {
verbose: matches.opt_present("v"),
quiet: matches.opt_present("q"),
kernel_docs: matches.opt_str("d").map(PathBuf::from),
display_type,
ignore_errors: matches.opt_present("e"),

View file

@ -19,9 +19,10 @@ pub fn run(args: Args) -> Result<()> {
let mut config = Config::default();
config.sysctl.verbose = args.verbose;
config.sysctl.ignore_errors = args.ignore_errors;
config.app.display_type = args.display_type;
config.app.quiet = args.quiet;
config.app.no_color = env::var("NO_COLOR").is_ok();
config.app.no_pager = args.no_pager;
config.app.display_type = args.display_type;
let mut sysctl = Sysctl::init(config.sysctl)?;
let mut app = App::new(&mut sysctl, &config.app)?;