Skip to content

Commit

Permalink
Replace structopt with Clap 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 6, 2020
1 parent d33ea2c commit c2313b3
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 79 deletions.
82 changes: 42 additions & 40 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ migration = ["criner/migration"]
doctest = false

[dependencies]
clap = {version = "2", features = ["color"]}
structopt = "0.3.9"
clap = { version = "3.0.0-beta.1", features = ["color"] }
humantime = "2.0.0"
time = "0.2.9"
criner = { version = "0.2.0", path = "./criner" }
Expand Down
8 changes: 1 addition & 7 deletions criner/src/engine/run.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use crate::{
engine::stage,
error::Result,
model,
persistence::Db,
utils::*,
};
use crate::{engine::stage, error::Result, model, persistence::Db, utils::*};
use futures_util::{
future::{Either, FutureExt},
stream::StreamExt,
Expand Down
57 changes: 30 additions & 27 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
use clap::Clap;
use std::path::PathBuf;
use structopt::StructOpt;

fn parse_local_time(src: &str) -> Result<time::Time, time::ParseError> {
time::parse(src, "%R")
}

#[derive(Debug, StructOpt)]
#[structopt(about = "Interact with crates.io from the command-line")]
#[structopt(settings = &[clap::AppSettings::ColoredHelp, clap::AppSettings::ColorAuto])]
pub struct Parsed {
#[structopt(subcommand)]
#[derive(Debug, Clap)]
#[clap(about = "Interact with crates.io from the command-line")]
#[clap(setting = clap::AppSettings::ColoredHelp)]
#[clap(setting = clap::AppSettings::ColorAuto)]
pub struct Args {
#[clap(subcommand)]
pub sub: Option<SubCommands>,
}

#[derive(Debug, StructOpt)]
#[derive(Debug, Clap)]
pub enum SubCommands {
/// Mine crates.io in an incorruptible and resumable fashion
#[structopt(display_order = 0)]
#[clap(display_order = 0)]
#[clap(setting = clap::AppSettings::DisableVersion)]
Mine {
/// If set, no gui will be presented. Best with RUST_LOG=info to see basic information.
#[structopt(long)]
#[clap(long)]
no_gui: bool,

/// The amount of frames to show per second
#[structopt(long, name = "frames-per-second", default_value = "6.0")]
#[clap(long, name = "frames-per-second", default_value = "6.0")]
fps: f32,

/// The amount of progress messages to keep in a ring buffer.
#[structopt(short = "s", long, default_value = "100")]
#[clap(short = "s", long, default_value = "100")]
progress_message_scrollback_buffer_size: usize,

/// The amount of IO-bound processors to run concurrently.
Expand All @@ -36,7 +38,7 @@ pub enum SubCommands {
/// Depending on that number, one should experiment with an amount of processors that saturate
/// either input or output.
/// Most commonly, these are bound to the input, as it is the network.
#[structopt(long, alias = "io", value_name = "io", default_value = "10")]
#[clap(long, alias = "io", value_name = "io", default_value = "10")]
io_bound_processors: u32,

/// The amount of CPU- and Output-bound processors to run concurrently.
Expand All @@ -46,64 +48,64 @@ pub enum SubCommands {
/// It's recommended to adjust that number to whatever can saturate the speed of writing to disk,
/// as these processors will yield when writing, allowing other processors to compute.
/// Computes are relatively inexpensive compared to the writes.
#[structopt(long, alias = "cpu-o", value_name = "cpu-o", default_value = "20")]
#[clap(long, alias = "cpu-o", value_name = "cpu-o", default_value = "20")]
cpu_o_bound_processors: u32,

/// The amount of CPU-bound processors to run concurrently.
///
/// One can assume that one of these can occupy one core of a CPU.
/// However, they will not use a lot of IO, nor will they use much memory.
#[structopt(long, alias = "cpu", value_name = "cpu", default_value = "4")]
#[clap(long, alias = "cpu", value_name = "cpu", default_value = "4")]
cpu_bound_processors: u32,

/// Path to the possibly existing crates.io repository clone. If unset, it will be cloned to a temporary spot.
#[structopt(short = "c", long, name = "REPO")]
#[clap(short = "c", long, name = "REPO")]
repository: Option<PathBuf>,

/// The amount of time we can take for the computation. Specified in humantime, like 10s, 5min, or 2h, or '3h 2min 2s'
#[structopt(long, short = "t")]
#[clap(long, short = "t")]
time_limit: Option<humantime::Duration>,

/// The time between each fetch operation, specified in humantime, like 10s, 5min, or 2h, or '3h 2min 2s'
#[structopt(long, short = "f", default_value = "5min")]
#[clap(long, short = "f", default_value = "5min")]
fetch_every: humantime::Duration,

/// If set, the amount of times the fetch stage will run. If set to 0, it will never run.
#[structopt(long, short = "F")]
#[clap(long, short = "F")]
fetch_at_most: Option<usize>,

/// The time between each processing run, specified in humantime, like 10s, 5min, or 2h, or '3h 2min 2s'
#[structopt(long, short = "p", default_value = "5min")]
#[clap(long, short = "p", default_value = "5min")]
process_every: humantime::Duration,

/// If set, the amount of times the process stage will run. If set to 0, they will never run.
#[structopt(long, short = "P")]
#[clap(long, short = "P")]
process_at_most: Option<usize>,

/// The time between each reporting and processing run, specified in humantime, like 10s, 5min, or 2h, or '3h 2min 2s'
#[structopt(long, short = "r", default_value = "5min")]
#[clap(long, short = "r", default_value = "5min")]
report_every: humantime::Duration,

/// If set, the amount of times the reporting stage will run. If set to 0, they will never run.
#[structopt(long, short = "R")]
#[clap(long, short = "R")]
report_at_most: Option<usize>,

/// If set, declare at which local time to download the crates.io database and digest it.
///
/// This job runs every 24h, as the database is updated that often.
/// If unset, the job starts right away.
/// Format is HH:MM, e.g. '14:30' for 2:30 pm or 03:15 for quarter past 3 in the morning.
#[structopt(long, short = "d", parse(try_from_str = parse_local_time))]
#[clap(long, short = "d", parse(try_from_str = parse_local_time))]
download_crates_io_database_every_24_hours_starting_at: Option<time::Time>,

/// If set, the reporting stage will only iterate over crates that match the given standard unix glob.
///
/// moz* would match only crates starting with 'moz' for example.
#[structopt(long, short = "g")]
#[clap(long, short = "g")]
glob: Option<String>,

/// Path to the possibly existing database. It's used to persist all mining results.
#[structopt(default_value = "criner.db")]
#[clap(default_value = "criner.db")]
db_path: PathBuf,
},
/// Export all Criner data into a format friendly for exploration via SQL, best viewed with https://sqlitebrowser.org
Expand All @@ -113,7 +115,8 @@ pub enum SubCommands {
/// tables with each column being a field. Foreign key relations are set accordingly to allow joins.
/// Use this to get an overview of what's available, and possibly contribute a report generator which implements
/// a query using raw data and writes it into reports.
#[structopt(display_order = 1)]
#[clap(display_order = 1)]
#[clap(setting = clap::AppSettings::DisableVersion)]
Export {
/// The path to the source database in sqlite format
input_db_path: PathBuf,
Expand All @@ -123,7 +126,7 @@ pub enum SubCommands {
},
#[cfg(feature = "migration")]
/// A special purpose command only to be executed in special circumstances
#[structopt(display_order = 9)]
#[clap(display_order = 9)]
Migrate,
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod args;
pub mod error;
pub use args::*;

pub fn run_blocking(args: Parsed) -> criner::error::Result<()> {
pub fn run_blocking(args: Args) -> criner::error::Result<()> {
use SubCommands::*;
let cmd = args.sub.unwrap_or_default();
match cmd {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use structopt::StructOpt;
use clap::Clap;

fn main() -> criner::error::Result<()> {
let args = criner_cli::Parsed::from_args();
let args = criner_cli::Args::parse();
if let Some(criner_cli::SubCommands::Mine { no_gui, .. }) = args.sub {
if no_gui {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
Expand Down

0 comments on commit c2313b3

Please sign in to comment.