summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Wong <mark@2ndQuadrant.com>2020-10-12 00:23:16 +0000
committerMark Wong <markwkm@gmail.com>2021-01-07 16:37:11 -0800
commit5f72f99d086567b1ccb6b820224c83959c834b95 (patch)
tree72cbd7274f177a6742de828dbdc99f77695bd27f
parentb5f7772370c4cd7453405029bebc3c594d086ba0 (diff)
Use clap for argument parsing
-rw-r--r--Cargo.toml5
-rw-r--r--src/pg_top.rs37
2 files changed, 42 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 500b879..6c47745 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,3 +7,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+clap = "2"
+
+[[bin]]
+name = "pg_top"
+path = "src/pg_top.rs"
diff --git a/src/pg_top.rs b/src/pg_top.rs
new file mode 100644
index 0000000..8b632d0
--- /dev/null
+++ b/src/pg_top.rs
@@ -0,0 +1,37 @@
+extern crate clap;
+use clap::{App, Arg};
+use std::thread;
+use std::time::Duration;
+
+fn main() {
+ let matches = App::new("pg_top")
+ .version("5.0.0")
+ .about("'top' for PostgreSQL")
+ .arg(
+ Arg::with_name("DELAY")
+ .help("set delay between screen updates")
+ .default_value("1")
+ .takes_value(true),
+ )
+ .arg(
+ Arg::with_name("COUNT")
+ .help("change number of displays to show")
+ .takes_value(true),
+ )
+ .get_matches();
+
+ let delay = matches.value_of("DELAY").unwrap().parse::<u64>().unwrap();
+ let mut count = if matches.value_of("COUNT").is_none() {
+ 1
+ } else {
+ matches.value_of("COUNT").unwrap().parse::<u64>().unwrap()
+ };
+
+ loop {
+ count = count - 1;
+ if count == 0 {
+ break;
+ }
+ thread::sleep(Duration::from_secs(delay));
+ }
+}