Skip to content

Commit

Permalink
Add command line args parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rustomax committed Jan 15, 2024
1 parent cacd0cb commit 9015d8e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ local-ip-address = "0.5.6"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
clap = { version = "4.4.16", features = ["derive"] }
argh = "0.1.12"
45 changes: 21 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
use serde_json::Value;
use std::thread;
use std::time::Duration;
use clap::{Arg, Command};
use argh::FromArgs;

#[allow(dead_code)]
fn generate_sysinfo() {
Expand Down Expand Up @@ -111,36 +111,33 @@ fn generate_user_list() {
println!("{}", payload.to_string());
}

fn main() {

let matches = Command::new("sysinfo-rs")
.version("0.1.0")
.author("Max Skybin <[email protected]>")
.about("Small helper to get system info and print it to stdout in JSON format")
.arg(Arg::new("processes")
.short('p')
.long("processes")
.required(false)
.help("Print list of running processes"))
.arg(Arg::new("users")
.short('u')
.long("users")
.required(false)
.help("Print list of users on this system"))
.get_matches();

let processes = matches.get_one::<bool>("processes").unwrap_or(&false);
let users = matches.get_one::<bool>("users").unwrap_or(&false);
#[derive(FromArgs)]
/// Small helper to get system info and print it to stdout in JSON format
struct Args {
/// print list of running processes
#[argh(switch, short = 'p')]
processes: bool,

/// print list of users on this system
#[argh(switch, short = 'u')]
users: bool,

/// how often to print updated info, in seconds
#[argh(option, short = 'i', default = "300")]
interval: u64,
}
fn main() {
loop {
let args: Args = argh::from_env();

generate_sysinfo();
if *processes {
if args.processes {
generate_process_list();
}
if *users {
if args.users {
generate_user_list();
}

thread::sleep(Duration::from_secs(300));
thread::sleep(Duration::from_secs(args.interval));
}
}

0 comments on commit 9015d8e

Please sign in to comment.