Skip to content

Add --daily cli option for edit, test and exec #175

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 3 commits into from
Jan 16, 2025
Merged
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.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ path = "src/bin/lc.rs"

[package]
name = "leetcode-cli"
version = "0.4.5"
version = "0.4.6"
authors = ["clearloop <tianyi.gc@gmail.com>"]
edition = "2021"
description = "Leetcode command-line interface in rust."
33 changes: 29 additions & 4 deletions src/cmds/edit.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use super::Command;
use crate::{Error, Result};
use anyhow::anyhow;
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
use std::collections::HashMap;

/// Abstract `edit` command
@@ -29,7 +29,7 @@ impl Command for EditCommand {
/// `edit` usage
fn usage() -> ClapCommand {
ClapCommand::new("edit")
.about("Edit question by id")
.about("Edit question")
.visible_alias("e")
.arg(
Arg::new("lang")
@@ -41,10 +41,22 @@ impl Command for EditCommand {
.arg(
Arg::new("id")
.num_args(1)
.required(true)
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
.arg(
Arg::new("daily")
.short('d')
.long("daily")
.help("Edit today's daily challenge")
.action(ArgAction::SetTrue),
)
.group(
ArgGroup::new("question-id")
.args(["id", "daily"])
.multiple(false)
.required(true),
)
}

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

let id = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
let cache = Cache::new()?;

let daily = m.get_one::<bool>("daily").unwrap_or(&false);
let daily_id = if *daily {
Some(cache.get_daily_problem_id().await?)
} else {
None
};

let id = m
.get_one::<i32>("id")
.copied()
.or(daily_id)
.ok_or(Error::NoneError)?;

let problem = cache.get_problem(id)?;
let mut conf = cache.to_owned().0.conf;

30 changes: 28 additions & 2 deletions src/cmds/exec.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
use super::Command;
use crate::{Error, Result};
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};

/// Abstract Exec Command
///
@@ -36,14 +36,40 @@ impl Command for ExecCommand {
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
.arg(
Arg::new("daily")
.short('d')
.long("daily")
.help("Exec today's daily challenge")
.action(ArgAction::SetTrue),
)
.group(
ArgGroup::new("question-id")
.args(["id", "daily"])
.multiple(false)
.required(true),
)
}

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

let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;
let cache = Cache::new()?;

let daily = m.get_one::<bool>("daily").unwrap_or(&false);
let daily_id = if *daily {
Some(cache.get_daily_problem_id().await?)
} else {
None
};

let id = m
.get_one::<i32>("id")
.copied()
.or(daily_id)
.ok_or(Error::NoneError)?;

let res = cache.exec_problem(id, Run::Submit, None).await?;

println!("{}", res);
36 changes: 31 additions & 5 deletions src/cmds/test.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
use super::Command;
use crate::{Error, Result};
use async_trait::async_trait;
use clap::{Arg, ArgMatches, Command as ClapCommand};
use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};

/// Abstract Test Command
///
@@ -27,12 +27,11 @@ impl Command for TestCommand {
/// `test` usage
fn usage() -> ClapCommand {
ClapCommand::new("test")
.about("Test question by id")
.about("Test a question")
.visible_alias("t")
.arg(
Arg::new("id")
.num_args(1)
.required(true)
.value_parser(clap::value_parser!(i32))
.help("question id"),
)
@@ -42,18 +41,45 @@ impl Command for TestCommand {
.required(false)
.help("custom testcase"),
)
.arg(
Arg::new("daily")
.short('d')
.long("daily")
.help("Test today's daily challenge")
.action(ArgAction::SetTrue),
)
.group(
ArgGroup::new("question-id")
.args(["id", "daily"])
.multiple(false)
.required(true),
)
}

/// `test` handler
async fn handler(m: &ArgMatches) -> Result<()> {
use crate::cache::{Cache, Run};
let id: i32 = *m.get_one::<i32>("id").ok_or(Error::NoneError)?;

let cache = Cache::new()?;

let daily = m.get_one::<bool>("daily").unwrap_or(&false);
let daily_id = if *daily {
Some(cache.get_daily_problem_id().await?)
} else {
None
};

let id = m
.get_one::<i32>("id")
.copied()
.or(daily_id)
.ok_or(Error::NoneError)?;

let testcase = m.get_one::<String>("testcase");
let case_str: Option<String> = match testcase {
Some(case) => Option::from(case.replace("\\n", "\n")),
_ => None,
};
let cache = Cache::new()?;
let res = cache.exec_problem(id, Run::Test, case_str).await?;

println!("{}", res);