diff options
author | Teddy Wing | 2021-06-05 13:11:03 +0200 |
---|---|---|
committer | Teddy Wing | 2021-06-05 13:11:03 +0200 |
commit | 2a05b269344ab324aa73a634256a0b82fa79f201 (patch) | |
tree | f88049c86dc293a6f5423893b23183a569fa2012 /src/main.rs | |
parent | 3964a27da7cf61b540992387e8553382dffecb46 (diff) | |
download | reflectub-2a05b269344ab324aa73a634256a0b82fa79f201.tar.bz2 |
Add command line option parsing
Define the options we want to take. Not using them yet.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index e20e860..f21f00a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,55 @@ use anyhow::{self, Context}; use chrono::DateTime; +use exitcode; use filetime; +use getopts::Options; use sqlx; use tokio; use reflectub::{database, git, github}; +use std::env; use std::fs; use std::io; use std::path::{Path, PathBuf}; +use std::process; #[tokio::main] async fn main() { + let args: Vec<String> = env::args().collect(); + + let mut opts = Options::new(); + + opts.optopt("d", "database", "SQLite database file path (required)", "DATABASE_FILE"); + opts.optopt("", "cgitrc", "base cgitrc file to copy to mirrored repositories", "CGITRC_FILE"); + opts.optflag("h", "help", "print this help menu"); + opts.optflag("V", "version", "show the program version"); + + let opt_matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => { + eprintln!("error: {}", e); + + process::exit(exitcode::SOFTWARE); + }, + }; + + if opt_matches.opt_present("h") { + print_usage(&opts); + process::exit(exitcode::USAGE); + } + + if opt_matches.opt_present("V") { + println!("{}", env!("CARGO_PKG_VERSION")); + process::exit(exitcode::OK); + } + + if opt_matches.free.len() != 2 { + print_usage(&opts); + process::exit(exitcode::USAGE); + } + // let repos = github::fetch_repos("teddywing").await.unwrap(); // // dbg!(&repos); @@ -29,6 +66,13 @@ async fn main() { run().await.unwrap(); } +fn print_usage(opts: &Options) { + print!( + "{}", + opts.usage("usage: reflectub [options] -d DATABASE <github_username> <repository_path>"), + ); +} + async fn run() -> anyhow::Result<()> { let test_repos = vec![ github::Repo { |