Skip to content

Commit d5228d9

Browse files
authored
Upgrade clap version to 4 (#101)
* feat: upgrade clap version to 4 * fix: runtime error
1 parent 1dc383e commit d5228d9

File tree

12 files changed

+106
-72
lines changed

12 files changed

+106
-72
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: 15 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,11 @@ 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
55+
.get_one::<String>("id")
56+
.map(|s| s.as_str())
57+
.ok_or(Error::NoneError)?
58+
.parse()?;
5559
let cache = Cache::new()?;
5660
let problem = cache.get_problem(id)?;
5761
let mut conf = cache.to_owned().0.conf;
@@ -61,7 +65,11 @@ impl Command for EditCommand {
6165
let p_desc_comment = problem.desc_comment(&conf);
6266
// condition language
6367
if m.contains_id("lang") {
64-
conf.code.lang = m.value_of("lang").ok_or(Error::NoneError)?.to_string();
68+
conf.code.lang = m
69+
.get_one::<String>("lang")
70+
.map(|s| s.as_str())
71+
.ok_or(Error::NoneError)?
72+
.to_string();
6573
conf.sync()?;
6674
}
6775

src/cmds/exec.rs

Lines changed: 8 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,11 @@ 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
45+
.get_one::<String>("id")
46+
.map(|s| s.as_str())
47+
.ok_or(Error::NoneError)?
48+
.parse()?;
4549
let cache = Cache::new()?;
4650
let res = cache.exec_problem(id, Run::Submit, None).await?;
4751

src/cmds/list.rs

Lines changed: 30 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,34 @@ 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::<String>("tag").map(|s| s.as_str()).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::<String>("category")
169+
.map(|s| s.as_str())
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
177+
.get_one::<String>("query")
178+
.map(|s| s.as_str())
179+
.ok_or(Error::NoneError)?;
172180
crate::helper::filter(&mut ps, query.to_string());
173181
}
174182

175183
// filter range
176184
if m.contains_id("range") {
177185
let num_range: Vec<i32> = m
178-
.values_of("range")
186+
.get_many::<String>("range")
179187
.ok_or(Error::NoneError)?
180188
.into_iter()
181189
.map(|x| x.parse::<i32>().unwrap_or(0))
@@ -184,7 +192,7 @@ impl Command for ListCommand {
184192
}
185193

186194
// retain if keyword exists
187-
if let Some(keyword) = m.value_of("keyword") {
195+
if let Some(keyword) = m.get_one::<String>("keyword").map(|s| s.as_str()) {
188196
let lowercase_kw = keyword.to_lowercase();
189197
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
190198
}

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: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use super::Command;
33
use crate::err::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
/// Abstract pick command
77
///
88
/// ```sh
@@ -43,38 +43,38 @@ 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)
77-
.help("Pick today's daily challenge"),
76+
.help("Pick today's daily challenge")
77+
.action(ArgAction::SetTrue),
7878
)
7979
}
8080

@@ -96,7 +96,11 @@ 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(
100+
m.get_one::<String>("plan")
101+
.map(|s| s.as_str())
102+
.unwrap_or(""),
103+
)?;
100104
crate::helper::squash(&mut problems, ids)?;
101105
}
102106
}
@@ -105,14 +109,17 @@ impl Command for PickCommand {
105109
if m.contains_id("tag") {
106110
let ids = cache
107111
.clone()
108-
.get_tagged_questions(m.value_of("tag").unwrap_or(""))
112+
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
109113
.await?;
110114
crate::helper::squash(&mut problems, ids)?;
111115
}
112116

113117
// query filter
114118
if m.contains_id("query") {
115-
let query = m.value_of("query").ok_or(Error::NoneError)?;
119+
let query = m
120+
.get_one::<String>("query")
121+
.map(|s| s.as_str())
122+
.ok_or(Error::NoneError)?;
116123
crate::helper::filter(&mut problems, query.to_string());
117124
}
118125

@@ -123,7 +130,8 @@ impl Command for PickCommand {
123130
};
124131

125132
let fid = m
126-
.value_of("id")
133+
.get_one::<String>("id")
134+
.map(|s| s.as_str())
127135
.and_then(|id| id.parse::<i32>().ok())
128136
.or(daily_id)
129137
.unwrap_or_else(|| {

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