Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07c9474

Browse files
fix: list py plan crash, and opt the clap get_one
1 parent 4f8178a commit 07c9474

File tree

5 files changed

+28
-43
lines changed

5 files changed

+28
-43
lines changed

‎src/cmds/edit.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl Command for EditCommand {
4040
Arg::new("id")
4141
.num_args(1)
4242
.required(true)
43+
.value_parser(clap::value_parser!(i32))
4344
.help("question id"),
4445
)
4546
}
@@ -51,11 +52,7 @@ impl Command for EditCommand {
5152
use std::io::Write;
5253
use std::path::Path;
5354

54-
let id: i32 = m
55-
.get_one::<String>("id")
56-
.map(|s| s.as_str())
57-
.ok_or(Error::NoneError)?
58-
.parse()?;
55+
let id = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
5956
let cache = Cache::new()?;
6057
let problem = cache.get_problem(id)?;
6158
let mut conf = cache.to_owned().0.conf;
@@ -67,7 +64,6 @@ impl Command for EditCommand {
6764
if m.contains_id("lang") {
6865
conf.code.lang = m
6966
.get_one::<String>("lang")
70-
.map(|s| s.as_str())
7167
.ok_or(Error::NoneError)?
7268
.to_string();
7369
conf.sync()?;

‎src/cmds/exec.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Command for ExecCommand {
3333
Arg::new("id")
3434
.num_args(1)
3535
.required(true)
36+
.value_parser(clap::value_parser!(i32))
3637
.help("question id"),
3738
)
3839
}
@@ -41,11 +42,7 @@ impl Command for ExecCommand {
4142
async fn handler(m: &ArgMatches) -> Result<(), crate::Error> {
4243
use crate::cache::{Cache, Run};
4344

44-
let id: i32 = m
45-
.get_one::<String>("id")
46-
.map(|s| s.as_str())
47-
.ok_or(Error::NoneError)?
48-
.parse()?;
45+
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
4946
let cache = Cache::new()?;
5047
let res = cache.exec_problem(id, Run::Submit, None).await?;
5148

‎src/cmds/list.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl Command for ListCommand {
102102
.short('r')
103103
.long("range")
104104
.num_args(2..)
105+
.value_parser(clap::value_parser!(i32))
105106
.help("Filter questions by id range"),
106107
)
107108
.after_help(LIST_AFTER_HELP)
@@ -148,15 +149,15 @@ impl Command for ListCommand {
148149
#[cfg(feature = "pym")]
149150
{
150151
if m.contains_id("plan") {
151-
let ids = crate::pym::exec(m.value_of("plan").unwrap_or(""))?;
152+
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
152153
crate::helper::squash(&mut ps, ids)?;
153154
}
154155
}
155156

156157
// filter tag
157158
if m.contains_id("tag") {
158159
let ids = cache
159-
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
160+
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
160161
.await?;
161162
crate::helper::squash(&mut ps, ids)?;
162163
}
@@ -165,34 +166,31 @@ impl Command for ListCommand {
165166
if m.contains_id("category") {
166167
ps.retain(|x| {
167168
x.category
168-
== m.get_one::<String>("category")
169-
.map(|s| s.as_str())
170-
.unwrap_or("algorithms")
169+
== *m
170+
.get_one::<String>("category")
171+
.unwrap_or(&"algorithms".to_string())
171172
});
172173
}
173174

174175
// filter query
175176
if m.contains_id("query") {
176-
let query = m
177-
.get_one::<String>("query")
178-
.map(|s| s.as_str())
179-
.ok_or(Error::NoneError)?;
177+
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
180178
crate::helper::filter(&mut ps, query.to_string());
181179
}
182180

183181
// filter range
184182
if m.contains_id("range") {
185183
let num_range: Vec<i32> = m
186-
.get_many::<String>("range")
184+
.get_many::<i32>("range")
187185
.ok_or(Error::NoneError)?
186+
.map(|id| *id)
188187
.into_iter()
189-
.map(|x| x.parse::<i32>().unwrap_or(0))
190188
.collect();
191189
ps.retain(|x| num_range[0] <= x.fid && x.fid <= num_range[1]);
192190
}
193191

194192
// retain if keyword exists
195-
if let Some(keyword) = m.get_one::<String>("keyword").map(|s| s.as_str()) {
193+
if let Some(keyword) = m.get_one::<String>("keyword") {
196194
let lowercase_kw = keyword.to_lowercase();
197195
ps.retain(|x| x.name.to_lowercase().contains(&lowercase_kw));
198196
}

‎src/cmds/pick.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ impl Command for PickCommand {
4747
ClapCommand::new("pick")
4848
.about("Pick a problem")
4949
.visible_alias("p")
50-
.arg(Arg::new("id").help("Problem id").num_args(1))
50+
.arg(
51+
Arg::new("id")
52+
.value_parser(clap::value_parser!(i32))
53+
.help("Problem id")
54+
.num_args(1),
55+
)
5156
.arg(
5257
Arg::new("plan")
5358
.short('p')
@@ -96,11 +101,7 @@ impl Command for PickCommand {
96101
#[cfg(feature = "pym")]
97102
{
98103
if m.contains_id("plan") {
99-
let ids = crate::pym::exec(
100-
m.get_one::<String>("plan")
101-
.map(|s| s.as_str())
102-
.unwrap_or(""),
103-
)?;
104+
let ids = crate::pym::exec(m.get_one::<String>("plan").unwrap_or(&"".to_string()))?;
104105
crate::helper::squash(&mut problems, ids)?;
105106
}
106107
}
@@ -109,17 +110,14 @@ impl Command for PickCommand {
109110
if m.contains_id("tag") {
110111
let ids = cache
111112
.clone()
112-
.get_tagged_questions(m.get_one::<String>("tag").map(|s| s.as_str()).unwrap_or(""))
113+
.get_tagged_questions(m.get_one::<String>("tag").unwrap_or(&"".to_string()))
113114
.await?;
114115
crate::helper::squash(&mut problems, ids)?;
115116
}
116117

117118
// query filter
118119
if m.contains_id("query") {
119-
let query = m
120-
.get_one::<String>("query")
121-
.map(|s| s.as_str())
122-
.ok_or(Error::NoneError)?;
120+
let query = m.get_one::<String>("query").ok_or(Error::NoneError)?;
123121
crate::helper::filter(&mut problems, query.to_string());
124122
}
125123

@@ -130,9 +128,8 @@ impl Command for PickCommand {
130128
};
131129

132130
let fid = m
133-
.get_one::<String>("id")
134-
.map(|s| s.as_str())
135-
.and_then(|id| id.parse::<i32>().ok())
131+
.get_one::<i32>("id")
132+
.map(|id| *id)
136133
.or(daily_id)
137134
.unwrap_or_else(|| {
138135
// Pick random without specify id

‎src/cmds/test.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl Command for TestCommand {
3333
Arg::new("id")
3434
.num_args(1)
3535
.required(true)
36+
.value_parser(clap::value_parser!(i32))
3637
.help("question id"),
3738
)
3839
.arg(
@@ -46,12 +47,8 @@ impl Command for TestCommand {
4647
/// `test` handler
4748
async fn handler(m: &ArgMatches) -> Result<(), Error> {
4849
use crate::cache::{Cache, Run};
49-
let id: i32 = m
50-
.get_one::<String>("id")
51-
.map(|s| s.as_str())
52-
.ok_or(Error::NoneError)?
53-
.parse()?;
54-
let testcase = m.get_one::<String>("testcase").map(|s| s.as_str());
50+
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
51+
let testcase = m.get_one::<String>("testcase");
5552
let case_str: Option<String> = match testcase {
5653
Some(case) => Option::from(case.replace("\\n", "\n")),
5754
_ => None,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /