From 15ba6a7e45c2d3f433ad9d54ba75236d95622abe Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 20 May 2021 03:50:38 +0000 Subject: feat(cli): conditional file specification --- README.rst | 10 ++++++--- src/cli.rs | 68 ++++++++++++++++++++++++++++++++++++++-------------------- src/nitrous.rs | 1 + 3 files changed, 53 insertions(+), 26 deletions(-) diff --git a/README.rst b/README.rst index 4e9dd92..30924d7 100644 --- a/README.rst +++ b/README.rst @@ -39,11 +39,15 @@ Checking Codes .. code-block:: shell - $ nitrous check + $ nitrous check -The path to your codes.txt may be any specified path as long as the file exists. +The previous command (without any specified codes file) will run the check +routine on a default file value of `./nitrous/codes.txt`. If you would like to +override this behaviour, specify your file after the subcommand; -The +.. code-block:: shell + + $ nitrous check /path/to/codes.txt Notes ----- diff --git a/src/cli.rs b/src/cli.rs index 259c0e2..61d8a9c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -32,11 +32,21 @@ impl Cli { ); } else if matches.is_present("check") { Nitrous::check( - matches - .subcommand_matches("check") - .unwrap() - .value_of("file") - .unwrap(), + { + let argument = matches + .subcommand_matches("check") + .unwrap() + .value_of("file"); + if argument.is_some() { + argument.unwrap() + } else { + if std::fs::File::open("nitrous/codes.txt").is_err() { + panic!("cannot open nitrous generated codes.txt"); + } else { + "nitrous/codes.txt" + } + } + }, debug, ) .await; @@ -50,25 +60,37 @@ impl Cli { .author(env!("CARGO_PKG_AUTHORS")) .setting(clap::AppSettings::SubcommandRequiredElseHelp) .subcommands(vec![ - SubCommand::with_name("generate").alias("gen").arg( - Arg::with_name("amount") - .required(true) - .index(1) - .takes_value(true), - ), - SubCommand::with_name("check").arg( - Arg::with_name("file") - .required(true) - .takes_value(true) - .index(1), - ), + SubCommand::with_name("generate") + .alias("gen") + .about("Generate a specified number Discord Nitro codes") + .arg( + Arg::with_name("amount") + .required(true) + .index(1) + .takes_value(true), + ), + SubCommand::with_name("check") + .about("Check a file of Discord Nitro codes for valid/ invalid codes") + .long_about( + "Check a file of Discord Nitro codes for valid/ invalid codes.\n\nIf a codes file is \ + not explicitly specified, the check routine will run on a default file value of \ + `./nitrous/codes.txt`. If you would like to override this behaviour, specify your \ + file after the subcommand.", + ) + .arg( + Arg::with_name("file") + .required(false) + .takes_value(true) + .index(1), + ), ]) - .arg(Arg::with_name("debug") - .long("debug") - .short("d") - .takes_value(false) - .multiple(false) - .global(true) + .arg( + Arg::with_name("debug") + .long("debug") + .short("d") + .takes_value(false) + .multiple(false) + .global(true), ) } } diff --git a/src/nitrous.rs b/src/nitrous.rs index 8264c05..900964e 100644 --- a/src/nitrous.rs +++ b/src/nitrous.rs @@ -17,6 +17,7 @@ impl Nitrous { // Logging pretty_env_logger::init(); + human_panic::setup_panic!(); crate::cli::Cli::execute().await; } -- cgit v1.2.3