Skip to content

Commit 35704f2

Browse files
authored
Add the parse_options function (#3)
1 parent fb6f15c commit 35704f2

File tree

1 file changed

+172
-4
lines changed

1 file changed

+172
-4
lines changed

redis-cli/src/main.rs

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,185 @@
11
mod help;
2+
mod conf;
23

34
use std::env;
4-
use std::process;
5+
use std::process::exit;
56
use redis_core::version;
7+
use conf::CliConfig;
8+
use conf::Pref;
9+
use conf::consts;
10+
use crate::help::print_usage;
11+
12+
const spectrum_palette_color_size: i32 = 19;
13+
const spectrum_palette_color: [i32; 19] = [0, 233, 234, 235, 237, 239, 241, 243, 245, 247, 144, 143, 142, 184, 226, 214, 208, 202, 196];
14+
const spectrum_palette_mono_size: i32 = 13;
15+
const spectrum_palette_mono: [i32; 13] = [0, 233, 234, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253];
16+
617

718
fn main() {
19+
let first_arg = 0;
20+
let mut config = CliConfig::new();
21+
let pref = Pref::new();
22+
let mut spectrum_palette = spectrum_palette_color.to_vec();
23+
let mut spectrum_palette_size = spectrum_palette_color_size;
24+
825
let args: Vec<String> = env::args().collect();
926
if args.len() == 1 {
10-
help::print_usage(0);
27+
print_usage(0);
1128
}
12-
let arg1 = &args[1];
29+
let arg1 = &args[1].clone();
30+
31+
parse_options(args, &mut config, &mut spectrum_palette, &mut spectrum_palette_size);
32+
1333
if arg1 == "-v" || arg1 == "--version" {
1434
println!("{}", version::redis_cli_version());
15-
process::exit(0);
35+
return;
36+
}
37+
}
38+
39+
fn parse_options(args: Vec<String>, config: &mut CliConfig, spectrum_palette: &mut Vec<i32>, spectrum_palette_size: &mut i32) {
40+
for (i, v) in args.iter().enumerate() {
41+
if i == 0 {
42+
continue;
43+
}
44+
let next = || args.get(i + 1).unwrap().to_string();
45+
let last_arg = i == args.len() - 1;
46+
47+
if v == "-h" && !last_arg {
48+
config.conn_info.host_ip = next();
49+
} else if v == "-h" && last_arg {
50+
help::print_usage(0);
51+
} else if v == "--help" {
52+
help::print_usage(0);
53+
} else if v == "-x" {
54+
config.stdin_last_arg = 1;
55+
} else if v == "-X" && !last_arg {
56+
config.stdin_tag_arg = 1;
57+
config.stdin_tag_name = next();
58+
} else if v == "-p" && !last_arg {
59+
config.conn_info.host_port = next().parse().unwrap_or_default();
60+
} else if v == "-s" && !last_arg {
61+
config.host_socket = next();
62+
} else if v == "-r" && !last_arg {
63+
config.repeat = next().parse().unwrap_or_default();
64+
} else if v == "-i" && !last_arg {
65+
config.interval = next().parse::<i64>().unwrap_or_default() * 1000000;
66+
} else if v == "-n" && !last_arg {
67+
config.conn_info.input_db_num = next().parse().unwrap_or_default();
68+
} else if v == "--no-auth-warning" {
69+
config.no_auth_warning = 1;
70+
} else if v == "--askpass" {
71+
config.ask_pass = 1;
72+
} else if (v == "-a" || v == "--pass") && !last_arg {
73+
config.conn_info.auth = next();
74+
} else if v == "--user" && !last_arg {
75+
config.conn_info.user = next();
76+
} else if v == "-u" && !last_arg {
77+
// TODO
78+
} else if v == "--raw" {
79+
config.output = consts::OUTPUT_RAW;
80+
} else if v == "--no-raw" {
81+
config.output = consts::OUTPUT_STANDARD;
82+
} else if v == "--quoted-input" {
83+
config.quoted_input = 1;
84+
} else if v == "--csv" {
85+
config.output = consts::OUTPUT_CSV;
86+
} else if v == "--json" {
87+
if config.resp3 == 0 {
88+
config.resp3 = 2;
89+
}
90+
config.output = consts::OUTPUT_JSON;
91+
} else if v == "--quoted-json" {
92+
if config.resp3 == 0 {
93+
config.resp3 = 2;
94+
}
95+
config.output = consts::OUTPUT_QUOTED_JSON;
96+
} else if v == "--latency" {
97+
config.latency_mode = 1;
98+
} else if v == "--latency-dist" {
99+
config.latency_dist_mode = 1;
100+
} else if v == "--mono" {
101+
*spectrum_palette = spectrum_palette_mono.to_vec();
102+
*spectrum_palette_size = spectrum_palette_mono_size;
103+
} else if v == "--latency-history" {
104+
config.latency_mode = 1;
105+
config.latency_history = 1;
106+
} else if v == "--lru-test" && !last_arg {
107+
config.lru_test_mode = 1;
108+
config.lru_test_sample_size = next().parse().unwrap_or_default();
109+
} else if v == "--slave" {
110+
config.slave_mode = 1;
111+
} else if v == "--replica" {
112+
config.slave_mode = 1;
113+
} else if v == "--stat" {
114+
config.stat_mode = 1;
115+
} else if v == "--scan" {
116+
config.scan_mode = 1;
117+
} else if v == "--pattern" && !last_arg {
118+
config.pattern = next();
119+
} else if v == "--quoted-pattern" && !last_arg {
120+
// TODO
121+
config.pattern = next();
122+
if config.pattern.is_empty() {
123+
eprintln!("Invalid quoted string specified for --quoted-pattern.");
124+
exit(1);
125+
}
126+
} else if v == "--intrinsic-latency" && !last_arg {
127+
config.intrinsic_latency_mode = 1;
128+
config.intrinsic_latency_duration = next().parse().unwrap_or_default();
129+
} else if v == "--rdb" && !last_arg {
130+
config.get_rdb_mode = 1;
131+
config.rdb_filename = next();
132+
} else if v == "--functions-rdb" && !last_arg {
133+
config.get_functions_rdb_mode = 1;
134+
config.rdb_filename = next();
135+
} else if v == "--pipe" {
136+
config.pipe_mode = 1;
137+
} else if v == "--pipe-timeout" && !last_arg {
138+
config.pipe_timeout = next().parse().unwrap_or_default();
139+
} else if v == "--bigkeys" {
140+
config.big_keys = 1;
141+
} else if v == "--memkeys" {
142+
config.mem_keys = 1;
143+
config.mem_keys_samples = 0;
144+
} else if v == "--memkeys-samples" {
145+
config.mem_keys = 1;
146+
config.mem_keys_samples = next().parse().unwrap_or_default();
147+
} else if v == "--hotkeys" {
148+
config.hot_keys = 1;
149+
} else if v == "--eval" && !last_arg {
150+
config.eval = next();
151+
} else if v == "--ldb" {
152+
config.eval_ldb = 1;
153+
config.output = consts::OUTPUT_RAW;
154+
} else if v == "--ldb-sync-mode" {
155+
config.eval_ldb = 1;
156+
config.eval_ldb_sync = 1;
157+
config.output = consts::OUTPUT_RAW;
158+
} else if v == "-c" {
159+
config.cluster_mode = 1;
160+
} else if v == "-d" && !last_arg {
161+
config.mb_delim = next();
162+
} else if v == "-D" && !last_arg {
163+
config.cmd_delim = next();
164+
} else if v == "-e" {
165+
config.set_errcode = 1;
166+
} else if v == "--verbose" {
167+
config.verbose = 1;
168+
} else if v == "--cluster" && !last_arg {
169+
if !config.cluster_manager_command.name.is_empty() {
170+
print_usage(1);
171+
}
172+
// TODO
173+
} else if v == "--cluster" && last_arg {
174+
print_usage(1);
175+
} else if v == "--cluster-only-masters" {
176+
config.cluster_manager_command.flags |= consts::CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY;
177+
} else if v == "--cluster-only-replicas" {
178+
config.cluster_manager_command.flags |= consts::CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY;
179+
} else if v == "--cluster-replicas" && !last_arg {
180+
config.cluster_manager_command.replicas = next().parse().unwrap_or_default();
181+
} else if v == "--cluster-master-id" && !last_arg {
182+
config.cluster_manager_command.master_id = next();
183+
}
16184
}
17185
}

0 commit comments

Comments
 (0)