Skip to content

Commit c2af4ce

Browse files
committed
feat: switched from getopts to clap for aw-server-rust CLI options
1 parent a4588f7 commit c2af4ce

File tree

4 files changed

+45
-48
lines changed

4 files changed

+45
-48
lines changed

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aw-server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fern = { version = "0.6", features = ["colored"] }
2828
toml = "0.5"
2929
gethostname = "0.2"
3030
uuid = { version = "0.8", features = ["serde", "v4"] }
31-
getopts = "0.2"
31+
clap = "3.0.0-beta.2"
3232

3333
aw-datastore = { path = "../aw-datastore" }
3434
aw-models = { path = "../aw-models" }

aw-server/src/main.rs

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#[macro_use]
22
extern crate log;
3-
extern crate getopts;
4-
5-
use getopts::Options;
6-
use rocket::config::Environment;
73

84
use std::env;
95

6+
use clap::{AppSettings, Clap};
7+
use rocket::config::Environment;
8+
109
use aw_server::*;
1110

1211
#[cfg(all(target_os = "linux", target_arch = "x86"))]
@@ -15,36 +14,37 @@ extern crate jemallocator;
1514
#[global_allocator]
1615
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
1716

18-
fn print_usage(program: &str, opts: Options) {
19-
let brief = format!("Usage: {} FILE [options]", program);
20-
print!("{}", opts.usage(&brief));
17+
/// Rust server for ActivityWatch
18+
#[derive(Clap)]
19+
#[clap(version = "0.10", author = "Johan Bjäreholt, Erik Bjäreholt")]
20+
#[clap(setting = AppSettings::ColoredHelp)]
21+
struct Opts {
22+
/// Run in testing mode
23+
#[clap(long)]
24+
testing: bool,
25+
/// Address to listen to
26+
#[clap(long)]
27+
host: Option<String>,
28+
/// Port to listen on
29+
#[clap(long)]
30+
port: Option<String>,
31+
/// Path to database override
32+
#[clap(long)]
33+
dbpath: Option<String>,
34+
/// Device ID override
35+
#[clap(long)]
36+
device_id: Option<String>,
37+
/// Don't import from aw-server-python if no aw-server-rust db found
38+
#[clap(long)]
39+
no_legacy_import: bool,
2140
}
2241

2342
fn main() {
24-
use std::sync::Mutex;
25-
26-
let args: Vec<String> = env::args().collect();
27-
let program = args[0].clone();
28-
29-
let mut opts = Options::new();
30-
opts.optflag("", "testing", "run in testing mode");
31-
opts.optflag("h", "help", "print this help menu");
32-
opts.optopt("", "port", "port to listent to", "PORT");
33-
opts.optopt("", "dbpath", "path to database", "PATH");
34-
opts.optopt("", "device-id", "device ID override", "ID");
43+
let opts: Opts = Opts::parse();
3544

36-
opts.optflag("", "no-legacy-import", "don't import from aw-server-python");
37-
38-
let matches = match opts.parse(&args[1..]) {
39-
Ok(m) => m,
40-
Err(f) => panic!("{}", f.to_string()),
41-
};
42-
if matches.opt_present("h") {
43-
print_usage(&program, opts);
44-
return;
45-
}
45+
use std::sync::Mutex;
4646

47-
let mut testing = matches.opt_present("testing");
47+
let mut testing = opts.testing;
4848
// Always override environment if --testing is specified
4949
if !testing {
5050
let env = Environment::active().expect("Failed to get current environment");
@@ -59,13 +59,18 @@ fn main() {
5959

6060
let mut config = config::create_config(testing);
6161

62-
// Set port if overridden
63-
if let Ok(Some(port)) = matches.opt_get("port") {
64-
config.port = port;
62+
// set host if overridden
63+
if let Some(host) = opts.host {
64+
config.address = host;
65+
}
66+
67+
// set port if overridden
68+
if let Some(port) = opts.port {
69+
config.port = port.parse().unwrap();
6570
}
6671

6772
// Set db path if overridden
68-
let db_path: String = if let Ok(Some(dbpath)) = matches.opt_get("dbpath") {
73+
let db_path: String = if let Some(dbpath) = opts.dbpath {
6974
dbpath
7075
} else {
7176
dirs::db_path(testing)
@@ -79,9 +84,9 @@ fn main() {
7984
let asset_path = get_asset_path();
8085
info!("Using aw-webui assets at path {:?}", asset_path);
8186

82-
let legacy_import = !matches.opt_present("no-legacy-import");
87+
let legacy_import = !opts.no_legacy_import;
8388

84-
let device_id: String = if let Ok(Some(id)) = matches.opt_get("device-id") {
89+
let device_id: String = if let Some(id) = opts.device_id {
8590
id
8691
} else {
8792
device_id::get_device_id()

aw-sync/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ use aw_client_rust::AwClient;
1313
mod sync;
1414

1515
#[derive(Clap)]
16-
#[clap(version = "1.0", author = "Erik Bjäreholt")]
16+
#[clap(version = "0.1", author = "Erik Bjäreholt")]
1717
#[clap(setting = AppSettings::ColoredHelp)]
1818
struct Opts {
19-
/// Sets a custom config file. Could have been an Option<T> with no default too
19+
/// Host of instance to connect to
2020
#[clap(long, default_value = "127.0.0.1")]
2121
host: String,
22+
/// Port of instance to connect to
2223
#[clap(long, default_value = "5666")]
2324
port: String,
2425
}

0 commit comments

Comments
 (0)