-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcli.rs
More file actions
100 lines (84 loc) · 2.72 KB
/
cli.rs
File metadata and controls
100 lines (84 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Clap Commanders
use crate::{
cmd::{CompletionsArgs, DataArgs, EditArgs, ExecArgs, ListArgs, PickArgs, StatArgs, TestArgs},
err::Error,
};
use clap::{CommandFactory, Parser, Subcommand};
use env_logger::Env;
use log::LevelFilter;
/// This should be called before calling any cli method or printing any output.
pub fn reset_signal_pipe_handler() {
#[cfg(target_family = "unix")]
{
use nix::sys::signal;
unsafe {
let _ = signal::signal(signal::Signal::SIGPIPE, signal::SigHandler::SigDfl)
.map_err(|e| println!("{:?}", e));
}
}
}
/// May the Code be with You
#[derive(Parser)]
#[command(name = "leetcode", version, about = "May the Code be with You 👻")]
#[command(arg_required_else_help = true)]
pub struct Cli {
/// Debug mode
#[arg(short, long)]
pub debug: bool,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
pub enum Commands {
/// Manage Cache
#[command(visible_alias = "d", display_order = 1)]
Data(DataArgs),
/// Edit question
#[command(visible_alias = "e", display_order = 2)]
Edit(EditArgs),
/// Submit solution
#[command(visible_alias = "x", display_order = 3)]
Exec(ExecArgs),
/// List problems
#[command(visible_alias = "l", display_order = 4)]
List(ListArgs),
/// Pick a problem
#[command(visible_alias = "p", display_order = 5)]
Pick(PickArgs),
/// Show simple chart about submissions
#[command(visible_alias = "s", display_order = 6)]
Stat(StatArgs),
/// Test a question
#[command(visible_alias = "t", display_order = 7)]
Test(TestArgs),
/// Generate shell Completions
#[command(visible_alias = "c", display_order = 8)]
Completions(CompletionsArgs),
}
/// Get matches
pub async fn main() -> Result<(), Error> {
reset_signal_pipe_handler();
let cli = Cli::parse();
if cli.debug {
env_logger::Builder::from_env(Env::default().default_filter_or("debug")).init();
} else {
env_logger::Builder::new()
.filter_level(LevelFilter::Info)
.format_timestamp(None)
.init();
}
match cli.command {
Some(Commands::Data(args)) => args.run().await,
Some(Commands::Edit(args)) => args.run().await,
Some(Commands::Exec(args)) => args.run().await,
Some(Commands::List(args)) => args.run().await,
Some(Commands::Pick(args)) => args.run().await,
Some(Commands::Stat(args)) => args.run().await,
Some(Commands::Test(args)) => args.run().await,
Some(Commands::Completions(args)) => {
let mut cmd = Cli::command();
args.run(&mut cmd)
}
None => Err(Error::MatchError),
}
}