Skip to content

Commit b3a1634

Browse files
committed
create cli commands
1 parent ec2b098 commit b3a1634

File tree

3 files changed

+251
-1
lines changed

3 files changed

+251
-1
lines changed

Cargo.lock

Lines changed: 130 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ serde_json = "1.0.52"
1616
serde = { version = "1.0.106", features = ["derive"] }
1717
reqwest = { version = "0.10.4", features = ["blocking", "json"] }
1818
prettytable-rs = "^0.8"
19+
clap = "3.0.0-beta.1"
1920

2021
[[bin]]
2122
bench = false

src/main.rs

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#[macro_use]
2+
extern crate clap;
3+
#[macro_use]
24
extern crate prettytable;
35

46
use std::borrow::Cow;
57
use std::error::Error;
68

9+
use clap::{App, AppSettings, Arg};
710
use prettytable::{Cell, Row, Table};
811
use serde::Deserialize;
912

@@ -39,7 +42,123 @@ struct Language {
3942
}
4043

4144
fn main() {
42-
println!("Hello, world!");
45+
let matches = App::new("wukong")
46+
.setting(AppSettings::SubcommandRequiredElseHelp)
47+
.about("A command-line tool for browsing GitHub trending written by Rust.")
48+
.version(crate_version!())
49+
.author(crate_authors!())
50+
.after_help("https://github.com/TonnyL/wukong/")
51+
.subcommand(
52+
App::new("repos")
53+
.about("See the developers that the GitHub community is most excited about")
54+
.aliases(&vec!["r", "repositories", "repository"])
55+
.args(&[
56+
Arg::with_name("lang")
57+
.short('l')
58+
.long("lang")
59+
.takes_value(true)
60+
.required(false)
61+
.about("filter by programming language"),
62+
Arg::with_name("period")
63+
.short('p')
64+
.long("period")
65+
.takes_value(true)
66+
.required(false)
67+
.about("filter by time period"),
68+
Arg::with_name("spoken_lang")
69+
.short('s')
70+
.long("spoken_language")
71+
.takes_value(true)
72+
.required(false)
73+
.about("filter by spoken language")
74+
])
75+
)
76+
.subcommand(
77+
App::new("devs")
78+
.about("These are the developers building the hot tools today")
79+
.aliases(&vec!["d", "developers", "developer"])
80+
.args(&[
81+
Arg::with_name("lang")
82+
.short('l')
83+
.long("language")
84+
.takes_value(true)
85+
.required(false)
86+
.about("filter by programming language"),
87+
Arg::with_name("period")
88+
.short('p')
89+
.long("period")
90+
.takes_value(true)
91+
.required(false)
92+
.about("filter by time period"),
93+
]),
94+
)
95+
.subcommand(
96+
App::new("langs")
97+
.about("List all the available programming language options")
98+
.aliases(&vec!["l", "languages", "language"]),
99+
)
100+
.subcommand(
101+
App::new("spoken_langs")
102+
.about("List all the available spoken language options")
103+
.aliases(&vec!["sl", "spoken-languages", "spoken-language", "spoken-lang"]),
104+
)
105+
.get_matches();
106+
107+
match matches.subcommand() {
108+
("langs", _) => {
109+
let result = list_languages();
110+
match result {
111+
Ok(value) => {
112+
show_table_of_languages(value);
113+
}
114+
Err(e) => {
115+
print_err_msg(e)
116+
}
117+
}
118+
}
119+
("spoken_langs", _) => {
120+
let result = list_spoken_language_codes();
121+
match result {
122+
Ok(value) => {
123+
show_table_of_languages(value);
124+
}
125+
Err(e) => {
126+
print_err_msg(e)
127+
}
128+
}
129+
}
130+
("repos", Some(sub_m)) => {
131+
let result = list_repositories(
132+
sub_m.value_of("lang").unwrap_or("").to_string(),
133+
sub_m.value_of("period").unwrap_or("daily").to_string(),
134+
sub_m.value_of("spoken_lang").unwrap_or("").to_string(),
135+
);
136+
match result {
137+
Ok(value) => {
138+
show_table_of_repositories(value);
139+
}
140+
Err(e) => {
141+
print_err_msg(e)
142+
}
143+
}
144+
}
145+
("devs", Some(sub_m)) => {
146+
let result = list_developers(
147+
sub_m.value_of("lang").unwrap_or("").to_string(),
148+
sub_m.value_of("period").unwrap_or("daily").to_string(),
149+
);
150+
151+
match result {
152+
Ok(value) => {
153+
show_table_of_developers(value);
154+
}
155+
Err(e) => {
156+
print_err_msg(e)
157+
}
158+
}
159+
}
160+
_ => {}
161+
}
43162
}
44163

45164
/// Display the languages data as a table.

0 commit comments

Comments
 (0)