Skip to content

Commit 86a0d6b

Browse files
committed
Added some command line options and "fixed" max number issue
1 parent de2c19b commit 86a0d6b

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
sscanf = "*"
9+
sscanf = "*"
10+
getopts = "*"

langs/english.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ hundreds 3 = "billions"
3838
out_range_err = "Warning: the number is out of range, the result may be wrong!"
3939
nan_err 0 = "Wait"
4040
nan_err 1 = "is not a number!"
41-
insert_num = "Insert a number to convert: "
41+
insert_num = "Insert a number to convert:"

src/main.rs

+52-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use getopts::Options;
12
use std::fs::File;
23
use std::io::{stdin, stdout, Read, Write};
34
extern crate sscanf;
@@ -118,7 +119,7 @@ fn separate_nums(src: &String, ui: &Ui) -> Vec<u128> {
118119
return numv;
119120
}
120121

121-
fn convert(digits: Digits, ui: &Ui, numv: Vec<u128>) -> String {
122+
fn convert(digits: &Digits, ui: &Ui, numv: Vec<u128>) -> String {
122123
// write "zero" only if the whole number is actually 0
123124
if numv.len() == 1 && numv[0] == 0 {
124125
return format!("{}", digits.ones[0]);
@@ -159,7 +160,6 @@ fn convert(digits: Digits, ui: &Ui, numv: Vec<u128>) -> String {
159160
if weight < 4 {
160161
result += &format!("{} ", digits.hundreds[weight % 4]);
161162
} else {
162-
println!("W: {}", weight / 3);
163163
for _ in 0..(weight / 4) + 1 {
164164
result += &format!("{} ", digits.hundreds[weight % 4]);
165165
}
@@ -177,17 +177,42 @@ fn read_lang_file(name: String) -> String {
177177
return content;
178178
}
179179

180+
// prints the usage
181+
fn usage(program: &str, opts: Options) {
182+
print!("{}", opts.usage(&format!("Usage: {} [options]", program)));
183+
}
184+
180185
fn main() {
181-
// get eventual language file name
186+
// get command line options
182187
let argv: Vec<String> = std::env::args().collect();
188+
let mut opts = Options::new();
189+
opts.optflag("h", "help", "prints this help message");
190+
opts.optopt("l", "lang", "set language file", "FILE");
191+
opts.optopt("o", "out", "set output file", "FILE");
192+
let matches = match opts.parse(&argv[1..]) {
193+
Ok(m) => m,
194+
Err(f) => panic!("{}", f.to_string()),
195+
};
196+
if matches.opt_present("help") {
197+
usage(&argv[0], opts);
198+
return;
199+
}
200+
// get language file name option (or default)
183201
let lang: String;
184-
if argv.len() < 2 {
185-
// default language
186-
lang = read_lang_file(String::from("langs/english.txt"));
202+
let lang_file_name: String;
203+
if matches.opt_present("lang") {
204+
match matches.opt_str("l") {
205+
Some(x) => lang_file_name = String::from(x.as_str()),
206+
None => {
207+
eprintln!("Error: language file was not provided!");
208+
std::process::exit(1); // exit with error
209+
}
210+
}
187211
} else {
188-
// argument language
189-
lang = read_lang_file(argv[1].clone());
212+
// default language file name
213+
lang_file_name = String::from("langs/english.txt");
190214
}
215+
lang = read_lang_file(lang_file_name);
191216
// load the language
192217
let ui = Ui::new(&lang);
193218
let digits = Digits::new(&lang);
@@ -198,5 +223,23 @@ fn main() {
198223
stdin().read_line(&mut user_input).expect("");
199224
// remove the '\n' from the input
200225
user_input.remove(user_input.len() - 1);
201-
println!("{}", convert(digits, &ui, separate_nums(&user_input, &ui)));
226+
if matches.opt_present("out") {
227+
match matches.opt_str("o") {
228+
Some(x) => {
229+
if !(x == "stdout" || x == "/dev/stdout") {
230+
writeln!(
231+
File::create(&x).expect(&format!("Failed to open {}: {0}", &x)),
232+
"{}",
233+
convert(&digits, &ui, separate_nums(&user_input, &ui))
234+
)
235+
.expect(&format!("Failed to write to {}!", &x));
236+
return;
237+
}
238+
}
239+
240+
None => {}
241+
}
242+
} else {
243+
println!("{}", convert(&digits, &ui, separate_nums(&user_input, &ui)));
244+
}
202245
}

0 commit comments

Comments
 (0)