Skip to content

Commit 42061d0

Browse files
committed
feat: upgrade clap version to 4
1 parent 1dc383e commit 42061d0

File tree

12 files changed

+80
-70
lines changed

12 files changed

+80
-70
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ readme = './README.md'
1818
[dependencies]
1919
async-trait = "0.1.56"
2020
tokio = { version = "1.19.2", features = ["full"] }
21-
clap = { version = "3.2.10", features = ["cargo"] }
21+
clap = { version = "4", features = ["cargo"] }
2222
colored = "2.0.0"
2323
dirs = "4.0.0"
2424
env_logger = "0.9.0"

src/cache/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Cache {
5555
}
5656

5757
pub fn update_after_ac(self, rid: i32) -> Result<(), Error> {
58-
let c = conn((&self.0.conf.storage.cache()?).to_owned());
58+
let c = conn(self.0.conf.storage.cache()?);
5959
let target = problems.filter(id.eq(rid));
6060
diesel::update(target).set(status.eq("ac")).execute(&c)?;
6161
Ok(())

src/cmds/data.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use super::Command;
33
use crate::{cache::Cache, helper::Digit, Error};
44
use async_trait::async_trait;
5-
use clap::{Arg, ArgMatches, Command as ClapCommand};
5+
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
66
use colored::Colorize;
77

88
/// Abstract `data` command
@@ -25,23 +25,25 @@ pub struct DataCommand;
2525
#[async_trait]
2626
impl Command for DataCommand {
2727
/// `data` command usage
28-
fn usage<'a>() -> ClapCommand<'a> {
28+
fn usage() -> ClapCommand {
2929
ClapCommand::new("data")
3030
.about("Manage Cache")
3131
.visible_alias("d")
3232
.arg(
33-
Arg::with_name("delete")
33+
Arg::new("delete")
3434
.display_order(1)
3535
.short('d')
3636
.long("delete")
37-
.help("Delete cache"),
37+
.help("Delete cache")
38+
.action(ArgAction::SetTrue),
3839
)
3940
.arg(
40-
Arg::with_name("update")
41+
Arg::new("update")
4142
.display_order(2)
4243
.short('u')
4344
.long("update")
44-
.help("Update cache"),
45+
.help("Update cache")
46+
.action(ArgAction::SetTrue),
4547
)
4648
}
4749

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

7577
let mut flags = 0;
76-
if m.contains_id("delete") {
78+
if m.get_flag("delete") {
7779
flags += 1;
7880
cache.clean()?;
7981
println!("{}", "ok!".bright_green());
8082
}
8183

82-
if m.contains_id("update") {
84+
if m.get_flag("update") {
8385
flags += 1;
8486
cache.update().await?;
8587
println!("{}", "ok!".bright_green());

src/cmds/edit.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ pub struct EditCommand;
2525
#[async_trait]
2626
impl Command for EditCommand {
2727
/// `edit` usage
28-
fn usage<'a>() -> ClapCommand<'a> {
28+
fn usage() -> ClapCommand {
2929
ClapCommand::new("edit")
3030
.about("Edit question by id")
3131
.visible_alias("e")
3232
.arg(
33-
Arg::with_name("lang")
33+
Arg::new("lang")
3434
.short('l')
3535
.long("lang")
36-
.takes_value(true)
36+
.num_args(1)
3737
.help("Edit with specific language"),
3838
)
3939
.arg(
40-
Arg::with_name("id")
41-
.takes_value(true)
40+
Arg::new("id")
41+
.num_args(1)
4242
.required(true)
4343
.help("question id"),
4444
)
@@ -51,7 +51,7 @@ impl Command for EditCommand {
5151
use std::io::Write;
5252
use std::path::Path;
5353

54-
let id: i32 = m.value_of("id").ok_or(Error::NoneError)?.parse()?;
54+
let id: i32 = m.get_one::<&str>("id").ok_or(Error::NoneError)?.parse()?;
5555
let cache = Cache::new()?;
5656
let problem = cache.get_problem(id)?;
5757
let mut conf = cache.to_owned().0.conf;
@@ -61,7 +61,10 @@ impl Command for EditCommand {
6161
let p_desc_comment = problem.desc_comment(&conf);
6262
// condition language
6363
if m.contains_id("lang") {
64-
conf.code.lang = m.value_of("lang").ok_or(Error::NoneError)?.to_string();
64+
conf.code.lang = m
65+
.get_one::<&str>("lang")
66+
.ok_or(Error::NoneError)?
67+
.to_string();
6568
conf.sync()?;
6669
}
6770

src/cmds/exec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ pub struct ExecCommand;
2525
#[async_trait]
2626
impl Command for ExecCommand {
2727
/// `exec` usage
28-
fn usage<'a>() -> ClapCommand<'a> {
28+
fn usage() -> ClapCommand {
2929
ClapCommand::new("exec")
3030
.about("Submit solution")
3131
.visible_alias("x")
3232
.arg(
33-
Arg::with_name("id")
34-
.takes_value(true)
33+
Arg::new("id")
34+
.num_args(1)
3535
.required(true)
3636
.help("question id"),
3737
)
@@ -41,7 +41,7 @@ impl Command for ExecCommand {
4141
async fn handler(m: &ArgMatches) -> Result<(), crate::Error> {
4242
use crate::cache::{Cache, Run};
4343

44-
let id: i32 = m.value_of("id").ok_or(Error::NoneError)?.parse()?;
44+
let id: i32 = m.get_one::<&str>("id").ok_or(Error::NoneError)?.parse()?;
4545
let cache = Cache::new()?;
4646
let res = cache.exec_problem(id, Run::Submit, None).await?;
4747

src/cmds/list.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
use super::Command;
3737
use crate::{cache::Cache, err::Error, helper::Digit};
3838
use async_trait::async_trait;
39-
use clap::{Arg, ArgMatches, Command as ClapCommand};
39+
use clap::{Arg, ArgAction, ArgMatches, Command as ClapCommand};
4040
/// Abstract `list` command
4141
///
4242
/// ## handler
@@ -72,56 +72,56 @@ static LIST_AFTER_HELP: &str = r#"EXAMPLES:
7272
#[async_trait]
7373
impl Command for ListCommand {
7474
/// `list` command usage
75-
fn usage<'a>() -> ClapCommand<'a> {
75+
fn usage() -> ClapCommand {
7676
ClapCommand::new("list")
7777
.about("List problems")
7878
.visible_alias("l")
7979
.arg(
80-
Arg::with_name("category")
80+
Arg::new("category")
8181
.short('c')
8282
.long("category")
83-
.takes_value(true)
83+
.num_args(1)
8484
.help(CATEGORY_HELP),
8585
)
8686
.arg(
87-
Arg::with_name("plan")
87+
Arg::new("plan")
8888
.short('p')
8989
.long("plan")
90-
.takes_value(true)
90+
.num_args(1)
9191
.help("Invoking python scripts to filter questions"),
9292
)
9393
.arg(
94-
Arg::with_name("query")
94+
Arg::new("query")
9595
.short('q')
9696
.long("query")
97-
.takes_value(true)
97+
.num_args(1)
9898
.help(QUERY_HELP),
9999
)
100100
.arg(
101-
Arg::with_name("range")
101+
Arg::new("range")
102102
.short('r')
103103
.long("range")
104-
.takes_value(true)
105-
.min_values(2)
104+
.num_args(2..)
106105
.help("Filter questions by id range"),
107106
)
108107
.after_help(LIST_AFTER_HELP)
109108
.arg(
110-
Arg::with_name("stat")
109+
Arg::new("stat")
111110
.short('s')
112111
.long("stat")
113-
.help("Show statistics of listed problems"),
112+
.help("Show statistics of listed problems")
113+
.action(ArgAction::SetTrue),
114114
)
115115
.arg(
116-
Arg::with_name("tag")
116+
Arg::new("tag")
117117
.short('t')
118118
.long("tag")
119-
.takes_value(true)
119+
.num_args(1)
120120
.help("Filter questions by tag"),
121121
)
122122
.arg(
123-
Arg::with_name("keyword")
124-
.takes_value(true)
123+
Arg::new("keyword")
124+
.num_args(1)
125125
.help("Keyword in select query"),
126126
)
127127
}
@@ -156,26 +156,31 @@ impl Command for ListCommand {
156156
// filter tag
157157
if m.contains_id("tag") {
158158
let ids = cache
159-
.get_tagged_questions(m.value_of("tag").unwrap_or(""))
159+
.get_tagged_questions(m.get_one::<&str>("tag").copied().unwrap_or(""))
160160
.await?;
161161
crate::helper::squash(&mut ps, ids)?;
162162
}
163163

164164
// filter category
165165
if m.contains_id("category") {
166-
ps.retain(|x| x.category == m.value_of("category").unwrap_or("algorithms"));
166+
ps.retain(|x| {
167+
x.category
168+
== m.get_one::<&str>("category")
169+
.copied()
170+
.unwrap_or("algorithms")
171+
});
167172
}
168173

169174
// filter query
170175
if m.contains_id("query") {
171-
let query = m.value_of("query").ok_or(Error::NoneError)?;
176+
let query = m.get_one::<&str>("query").ok_or(Error::NoneError)?;
172177
crate::helper::filter(&mut ps, query.to_string());
173178
}
174179

175180
// filter range
176181
if m.contains_id("range") {
177182
let num_range: Vec<i32> = m
178-
.values_of("range")
183+
.get_many::<String>("range")
179184
.ok_or(Error::NoneError)?
180185
.into_iter()
181186
.map(|x| x.parse::<i32>().unwrap_or(0))
@@ -184,7 +189,7 @@ impl Command for ListCommand {
184189
}
185190

186191
// retain if keyword exists
187-
if let Some(keyword) = m.value_of("keyword") {
192+
if let Some(keyword) = m.get_one::<&str>("keyword") {
188193
let lowercase_kw = keyword.to_lowercase();
189194
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
190195
}

src/cmds/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use clap::{ArgMatches, Command as ClapCommand};
1818
#[async_trait]
1919
pub trait Command {
2020
/// Usage of the specific command
21-
fn usage<'a>() -> ClapCommand<'a>;
21+
fn usage() -> ClapCommand;
2222

2323
/// The handler will deal [args, options,...] from the command-line
2424
async fn handler(m: &ArgMatches) -> Result<(), Error>;

src/cmds/pick.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,37 @@ s = starred S = not starred"#;
4343
#[async_trait]
4444
impl Command for PickCommand {
4545
/// `pick` usage
46-
fn usage<'a>() -> ClapCommand<'a> {
46+
fn usage() -> ClapCommand {
4747
ClapCommand::new("pick")
4848
.about("Pick a problem")
4949
.visible_alias("p")
50-
.arg(Arg::with_name("id").help("Problem id").takes_value(true))
50+
.arg(Arg::new("id").help("Problem id").num_args(1))
5151
.arg(
52-
Arg::with_name("plan")
52+
Arg::new("plan")
5353
.short('p')
5454
.long("plan")
55-
.takes_value(true)
55+
.num_args(1)
5656
.help("Invoking python scripts to filter questions"),
5757
)
5858
.arg(
59-
Arg::with_name("query")
59+
Arg::new("query")
6060
.short('q')
6161
.long("query")
62-
.takes_value(true)
62+
.num_args(1)
6363
.help(QUERY_HELP),
6464
)
6565
.arg(
66-
Arg::with_name("tag")
66+
Arg::new("tag")
6767
.short('t')
6868
.long("tag")
69-
.takes_value(true)
69+
.num_args(1)
7070
.help("Filter questions by tag"),
7171
)
7272
.arg(
73-
Arg::with_name("daily")
73+
Arg::new("daily")
7474
.short('d')
7575
.long("daily")
76-
.takes_value(false)
76+
.num_args(1)
7777
.help("Pick today's daily challenge"),
7878
)
7979
}
@@ -96,7 +96,7 @@ impl Command for PickCommand {
9696
#[cfg(feature = "pym")]
9797
{
9898
if m.contains_id("plan") {
99-
let ids = crate::pym::exec(m.value_of("plan").unwrap_or(""))?;
99+
let ids = crate::pym::exec(m.get_one::<&str>("plan").unwrap_or(&""))?;
100100
crate::helper::squash(&mut problems, ids)?;
101101
}
102102
}
@@ -105,14 +105,14 @@ impl Command for PickCommand {
105105
if m.contains_id("tag") {
106106
let ids = cache
107107
.clone()
108-
.get_tagged_questions(m.value_of("tag").unwrap_or(""))
108+
.get_tagged_questions(m.get_one::<&str>("tag").unwrap_or(&""))
109109
.await?;
110110
crate::helper::squash(&mut problems, ids)?;
111111
}
112112

113113
// query filter
114114
if m.contains_id("query") {
115-
let query = m.value_of("query").ok_or(Error::NoneError)?;
115+
let query = m.get_one::<&str>("query").ok_or(Error::NoneError)?;
116116
crate::helper::filter(&mut problems, query.to_string());
117117
}
118118

@@ -123,7 +123,7 @@ impl Command for PickCommand {
123123
};
124124

125125
let fid = m
126-
.value_of("id")
126+
.get_one::<&str>("id")
127127
.and_then(|id| id.parse::<i32>().ok())
128128
.or(daily_id)
129129
.unwrap_or_else(|| {

src/cmds/stat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct StatCommand;
2222
#[async_trait]
2323
impl Command for StatCommand {
2424
/// `stat` usage
25-
fn usage<'a>() -> ClapCommand<'a> {
25+
fn usage() -> ClapCommand {
2626
ClapCommand::new("stat")
2727
.about("Show simple chart about submissions")
2828
.visible_alias("s")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy