Skip to content

Upgrade clap version to 4 #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ readme = './README.md'
[dependencies]
async-trait = "0.1.56"
tokio = { version = "1.19.2", features = ["full"] }
clap = { version = "3.2.10", features = ["cargo"] }
clap = { version = "4", features = ["cargo"] }
colored = "2.0.0"
dirs = "4.0.0"
env_logger = "0.9.0"
Expand Down
2 changes: 1 addition & 1 deletion src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Cache {
}

pub fn update_after_ac(self, rid: i32) -> Result<(), Error> {
let c = conn((&self.0.conf.storage.cache()?).to_owned());
let c = conn(self.0.conf.storage.cache()?);
let target = problems.filter(id.eq(rid));
diesel::update(target).set(status.eq("ac")).execute(&c)?;
Ok(())
Expand Down
18 changes: 10 additions & 8 deletions src/cmds/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::Command;
use crate::{cache::Cache, helper::Digit, Error};
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
use colored::Colorize;

/// Abstract `data` command
Expand All @@ -25,23 +25,25 @@ pub struct DataCommand;
#[async_trait]
impl Command for DataCommand {
/// `data` command usage
fn usage<'a>() -> ClapCommand<'a> {
fn usage() -> ClapCommand {
ClapCommand::new("data")
.about("Manage Cache")
.visible_alias("d")
.arg(
Arg::with_name("delete")
Arg::new("delete")
.display_order(1)
.short('d')
.long("delete")
.help("Delete cache"),
.help("Delete cache")
.action(ArgAction::SetTrue),
)
.arg(
Arg::with_name("update")
Arg::new("update")
.display_order(2)
.short('u')
.long("update")
.help("Update cache"),
.help("Update cache")
.action(ArgAction::SetTrue),
)
}

Expand Down Expand Up @@ -73,13 +75,13 @@ impl Command for DataCommand {
title.push_str(&"-".repeat(65));

let mut flags = 0;
if m.contains_id("delete") {
if m.get_flag("delete") {
flags += 1;
cache.clean()?;
println!("{}", "ok!".bright_green());
}

if m.contains_id("update") {
if m.get_flag("update") {
flags += 1;
cache.update().await?;
println!("{}", "ok!".bright_green());
Expand Down
22 changes: 15 additions & 7 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ pub struct EditCommand;
#[async_trait]
impl Command for EditCommand {
/// `edit` usage
fn usage<'a>() -> ClapCommand<'a> {
fn usage() -> ClapCommand {
ClapCommand::new("edit")
.about("Edit question by id")
.visible_alias("e")
.arg(
Arg::with_name("lang")
Arg::new("lang")
.short('l')
.long("lang")
.takes_value(true)
.num_args(1)
.help("Edit with specific language"),
)
.arg(
Arg::with_name("id")
.takes_value(true)
Arg::new("id")
.num_args(1)
.required(true)
.help("question id"),
)
Expand All @@ -51,7 +51,11 @@ impl Command for EditCommand {
use std::io::Write;
use std::path::Path;

let id: i32 = m.value_of("id").ok_or(Error::NoneError)?.parse()?;
let id: i32 = m
.get_one::<String>("id")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.parse()?;
let cache = Cache::new()?;
let problem = cache.get_problem(id)?;
let mut conf = cache.to_owned().0.conf;
Expand All @@ -61,7 +65,11 @@ impl Command for EditCommand {
let p_desc_comment = problem.desc_comment(&conf);
// condition language
if m.contains_id("lang") {
conf.code.lang = m.value_of("lang").ok_or(Error::NoneError)?.to_string();
conf.code.lang = m
.get_one::<String>("lang")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.to_string();
conf.sync()?;
}

Expand Down
12 changes: 8 additions & 4 deletions src/cmds/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ pub struct ExecCommand;
#[async_trait]
impl Command for ExecCommand {
/// `exec` usage
fn usage<'a>() -> ClapCommand<'a> {
fn usage() -> ClapCommand {
ClapCommand::new("exec")
.about("Submit solution")
.visible_alias("x")
.arg(
Arg::with_name("id")
.takes_value(true)
Arg::new("id")
.num_args(1)
.required(true)
.help("question id"),
)
Expand All @@ -41,7 +41,11 @@ impl Command for ExecCommand {
async fn handler(m: &ArgMatches) -> Result<(), crate::Error> {
use crate::cache::{Cache, Run};

let id: i32 = m.value_of("id").ok_or(Error::NoneError)?.parse()?;
let id: i32 = m
.get_one::<String>("id")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?
.parse()?;
let cache = Cache::new()?;
let res = cache.exec_problem(id, Run::Submit, None).await?;

Expand Down
52 changes: 30 additions & 22 deletions src/cmds/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
use super::Command;
use crate::{cache::Cache, err::Error, helper::Digit};
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
/// Abstract `list` command
///
/// ## handler
Expand Down Expand Up @@ -72,56 +72,56 @@ static LIST_AFTER_HELP: &str = r#"EXAMPLES:
#[async_trait]
impl Command for ListCommand {
/// `list` command usage
fn usage<'a>() -> ClapCommand<'a> {
fn usage() -> ClapCommand {
ClapCommand::new("list")
.about("List problems")
.visible_alias("l")
.arg(
Arg::with_name("category")
Arg::new("category")
.short('c')
.long("category")
.takes_value(true)
.num_args(1)
.help(CATEGORY_HELP),
)
.arg(
Arg::with_name("plan")
Arg::new("plan")
.short('p')
.long("plan")
.takes_value(true)
.num_args(1)
.help("Invoking python scripts to filter questions"),
)
.arg(
Arg::with_name("query")
Arg::new("query")
.short('q')
.long("query")
.takes_value(true)
.num_args(1)
.help(QUERY_HELP),
)
.arg(
Arg::with_name("range")
Arg::new("range")
.short('r')
.long("range")
.takes_value(true)
.min_values(2)
.num_args(2..)
.help("Filter questions by id range"),
)
.after_help(LIST_AFTER_HELP)
.arg(
Arg::with_name("stat")
Arg::new("stat")
.short('s')
.long("stat")
.help("Show statistics of listed problems"),
.help("Show statistics of listed problems")
.action(ArgAction::SetTrue),
)
.arg(
Arg::with_name("tag")
Arg::new("tag")
.short('t')
.long("tag")
.takes_value(true)
.num_args(1)
.help("Filter questions by tag"),
)
.arg(
Arg::with_name("keyword")
.takes_value(true)
Arg::new("keyword")
.num_args(1)
.help("Keyword in select query"),
)
}
Expand Down Expand Up @@ -156,26 +156,34 @@ impl Command for ListCommand {
// filter tag
if m.contains_id("tag") {
let ids = cache
.get_tagged_questions(m.value_of("tag").unwrap_or(""))
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
.await?;
crate::helper::squash(&mut ps, ids)?;
}

// filter category
if m.contains_id("category") {
ps.retain(|x| x.category == m.value_of("category").unwrap_or("algorithms"));
ps.retain(|x| {
x.category
== m.get_one::<String>("category")
.map(|s| s.as_str())
.unwrap_or("algorithms")
});
}

// filter query
if m.contains_id("query") {
let query = m.value_of("query").ok_or(Error::NoneError)?;
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?;
crate::helper::filter(&mut ps, query.to_string());
}

// filter range
if m.contains_id("range") {
let num_range: Vec<i32> = m
.values_of("range")
.get_many::<String>("range")
.ok_or(Error::NoneError)?
.into_iter()
.map(|x| x.parse::<i32>().unwrap_or(0))
Expand All @@ -184,7 +192,7 @@ impl Command for ListCommand {
}

// retain if keyword exists
if let Some(keyword) = m.value_of("keyword") {
if let Some(keyword) = m.get_one::<String>("keyword").map(|s| s.as_str()) {
let lowercase_kw = keyword.to_lowercase();
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use clap::{ArgMatches, Command as ClapCommand};
#[async_trait]
pub trait Command {
/// Usage of the specific command
fn usage<'a>() -> ClapCommand<'a>;
fn usage() -> ClapCommand;

/// The handler will deal [args, options,...] from the command-line
async fn handler(m: &ArgMatches) -> Result<(), Error>;
Expand Down
40 changes: 24 additions & 16 deletions src/cmds/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::Command;
use crate::err::Error;
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
/// Abstract pick command
///
/// ```sh
Expand Down Expand Up @@ -43,38 +43,38 @@ s = starred S = not starred"#;
#[async_trait]
impl Command for PickCommand {
/// `pick` usage
fn usage<'a>() -> ClapCommand<'a> {
fn usage() -> ClapCommand {
ClapCommand::new("pick")
.about("Pick a problem")
.visible_alias("p")
.arg(Arg::with_name("id").help("Problem id").takes_value(true))
.arg(Arg::new("id").help("Problem id").num_args(1))
.arg(
Arg::with_name("plan")
Arg::new("plan")
.short('p')
.long("plan")
.takes_value(true)
.num_args(1)
.help("Invoking python scripts to filter questions"),
)
.arg(
Arg::with_name("query")
Arg::new("query")
.short('q')
.long("query")
.takes_value(true)
.num_args(1)
.help(QUERY_HELP),
)
.arg(
Arg::with_name("tag")
Arg::new("tag")
.short('t')
.long("tag")
.takes_value(true)
.num_args(1)
.help("Filter questions by tag"),
)
.arg(
Arg::with_name("daily")
Arg::new("daily")
.short('d')
.long("daily")
.takes_value(false)
.help("Pick today's daily challenge"),
.help("Pick today's daily challenge")
.action(ArgAction::SetTrue),
)
}

Expand All @@ -96,7 +96,11 @@ impl Command for PickCommand {
#[cfg(feature = "pym")]
{
if m.contains_id("plan") {
let ids = crate::pym::exec(m.value_of("plan").unwrap_or(""))?;
let ids = crate::pym::exec(
m.get_one::<String>("plan")
.map(|s| s.as_str())
.unwrap_or(""),
)?;
crate::helper::squash(&mut problems, ids)?;
}
}
Expand All @@ -105,14 +109,17 @@ impl Command for PickCommand {
if m.contains_id("tag") {
let ids = cache
.clone()
.get_tagged_questions(m.value_of("tag").unwrap_or(""))
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
.await?;
crate::helper::squash(&mut problems, ids)?;
}

// query filter
if m.contains_id("query") {
let query = m.value_of("query").ok_or(Error::NoneError)?;
let query = m
.get_one::<String>("query")
.map(|s| s.as_str())
.ok_or(Error::NoneError)?;
crate::helper::filter(&mut problems, query.to_string());
}

Expand All @@ -123,7 +130,8 @@ impl Command for PickCommand {
};

let fid = m
.value_of("id")
.get_one::<String>("id")
.map(|s| s.as_str())
.and_then(|id| id.parse::<i32>().ok())
.or(daily_id)
.unwrap_or_else(|| {
Expand Down
Loading