Skip to content

Commit 7923739

Browse files
authored
Add --daily cli option for edit, test and exec (#175)
* add `--daily` cli option for `e`, `t` and `x` * Bump version to 0.4.6 * Fix description typo
1 parent e6d4df8 commit 7923739

File tree

5 files changed

+90
-13
lines changed

5 files changed

+90
-13
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ path = "src/bin/lc.rs"
44

55
[package]
66
name = "leetcode-cli"
7-
version = "0.4.5"
7+
version = "0.4.6"
88
authors = ["clearloop <[email protected]>"]
99
edition = "2021"
1010
description = "Leetcode command-line interface in rust."

src/cmds/edit.rs

+29-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::Command;
33
use crate::{Error, Result};
44
use anyhow::anyhow;
55
use async_trait::async_trait;
6-
use clap::{Arg, ArgMatches, Command as ClapCommand};
6+
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
77
use std::collections::HashMap;
88

99
/// Abstract `edit` command
@@ -29,7 +29,7 @@ impl Command for EditCommand {
2929
/// `edit` usage
3030
fn usage() -> ClapCommand {
3131
ClapCommand::new("edit")
32-
.about("Edit question by id")
32+
.about("Edit question")
3333
.visible_alias("e")
3434
.arg(
3535
Arg::new("lang")
@@ -41,10 +41,22 @@ impl Command for EditCommand {
4141
.arg(
4242
Arg::new("id")
4343
.num_args(1)
44-
.required(true)
4544
.value_parser(clap::value_parser!(i32))
4645
.help("question id"),
4746
)
47+
.arg(
48+
Arg::new("daily")
49+
.short('d')
50+
.long("daily")
51+
.help("Edit today's daily challenge")
52+
.action(ArgAction::SetTrue),
53+
)
54+
.group(
55+
ArgGroup::new("question-id")
56+
.args(["id", "daily"])
57+
.multiple(false)
58+
.required(true),
59+
)
4860
}
4961

5062
/// `edit` handler
@@ -54,8 +66,21 @@ impl Command for EditCommand {
5466
use std::io::Write;
5567
use std::path::Path;
5668

57-
let id = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
5869
let cache = Cache::new()?;
70+
71+
let daily = m.get_one::<bool>("daily").unwrap_or(&false);
72+
let daily_id = if *daily {
73+
Some(cache.get_daily_problem_id().await?)
74+
} else {
75+
None
76+
};
77+
78+
let id = m
79+
.get_one::<i32>("id")
80+
.copied()
81+
.or(daily_id)
82+
.ok_or(Error::NoneError)?;
83+
5984
let problem = cache.get_problem(id)?;
6085
let mut conf = cache.to_owned().0.conf;
6186

src/cmds/exec.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use super::Command;
33
use crate::{Error, Result};
44
use async_trait::async_trait;
5-
use clap::{Arg, ArgMatches, Command as ClapCommand};
5+
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
66

77
/// Abstract Exec Command
88
///
@@ -36,14 +36,40 @@ impl Command for ExecCommand {
3636
.value_parser(clap::value_parser!(i32))
3737
.help("question id"),
3838
)
39+
.arg(
40+
Arg::new("daily")
41+
.short('d')
42+
.long("daily")
43+
.help("Exec today's daily challenge")
44+
.action(ArgAction::SetTrue),
45+
)
46+
.group(
47+
ArgGroup::new("question-id")
48+
.args(["id", "daily"])
49+
.multiple(false)
50+
.required(true),
51+
)
3952
}
4053

4154
/// `exec` handler
4255
async fn handler(m: &ArgMatches) -> Result<()> {
4356
use crate::cache::{Cache, Run};
4457

45-
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
4658
let cache = Cache::new()?;
59+
60+
let daily = m.get_one::<bool>("daily").unwrap_or(&false);
61+
let daily_id = if *daily {
62+
Some(cache.get_daily_problem_id().await?)
63+
} else {
64+
None
65+
};
66+
67+
let id = m
68+
.get_one::<i32>("id")
69+
.copied()
70+
.or(daily_id)
71+
.ok_or(Error::NoneError)?;
72+
4773
let res = cache.exec_problem(id, Run::Submit, None).await?;
4874

4975
println!("{}", res);

src/cmds/test.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use super::Command;
33
use crate::{Error, Result};
44
use async_trait::async_trait;
5-
use clap::{Arg, ArgMatches, Command as ClapCommand};
5+
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
66

77
/// Abstract Test Command
88
///
@@ -27,12 +27,11 @@ impl Command for TestCommand {
2727
/// `test` usage
2828
fn usage() -> ClapCommand {
2929
ClapCommand::new("test")
30-
.about("Test question by id")
30+
.about("Test a question")
3131
.visible_alias("t")
3232
.arg(
3333
Arg::new("id")
3434
.num_args(1)
35-
.required(true)
3635
.value_parser(clap::value_parser!(i32))
3736
.help("question id"),
3837
)
@@ -42,18 +41,45 @@ impl Command for TestCommand {
4241
.required(false)
4342
.help("custom testcase"),
4443
)
44+
.arg(
45+
Arg::new("daily")
46+
.short('d')
47+
.long("daily")
48+
.help("Test today's daily challenge")
49+
.action(ArgAction::SetTrue),
50+
)
51+
.group(
52+
ArgGroup::new("question-id")
53+
.args(["id", "daily"])
54+
.multiple(false)
55+
.required(true),
56+
)
4557
}
4658

4759
/// `test` handler
4860
async fn handler(m: &ArgMatches) -> Result<()> {
4961
use crate::cache::{Cache, Run};
50-
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
62+
63+
let cache = Cache::new()?;
64+
65+
let daily = m.get_one::<bool>("daily").unwrap_or(&false);
66+
let daily_id = if *daily {
67+
Some(cache.get_daily_problem_id().await?)
68+
} else {
69+
None
70+
};
71+
72+
let id = m
73+
.get_one::<i32>("id")
74+
.copied()
75+
.or(daily_id)
76+
.ok_or(Error::NoneError)?;
77+
5178
let testcase = m.get_one::<String>("testcase");
5279
let case_str: Option<String> = match testcase {
5380
Some(case) => Option::from(case.replace("\\n", "\n")),
5481
_ => None,
5582
};
56-
let cache = Cache::new()?;
5783
let res = cache.exec_problem(id, Run::Test, case_str).await?;
5884

5985
println!("{}", res);

0 commit comments

Comments
 (0)