Skip to content

Commit b617309

Browse files
committed
Add input file as pos argument, depricate -f
1 parent 4ff8222 commit b617309

2 files changed

Lines changed: 20 additions & 39 deletions

File tree

src/config.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clap::{App, Arg};
22

33
pub struct Config {
4-
pub minimize: bool,
4+
pub minimize: bool,
55
pub separator: String,
6-
pub file: Option<String>,
7-
pub outfile: Option<String>,
6+
pub file: Option<String>,
7+
pub out: Option<String>,
88
}
99

1010
impl Config {
@@ -13,36 +13,17 @@ impl Config {
1313
.version("1.0.2")
1414
.author("Axel Lindeberg")
1515
.about("Makes creating tables in markdown much easier!")
16-
.arg(Arg::with_name("minimize")
17-
.help("Minimizes table output")
18-
.long("minimize")
19-
.short("m")
20-
)
21-
.arg(Arg::with_name("file")
22-
.help("Reads table values from this if given, stdin otherwise.")
23-
.long("file")
24-
.short("f")
25-
.takes_value(true)
26-
)
27-
.arg(Arg::with_name("outfile")
28-
.help("Prints output to this if given, stdout otherwise.")
29-
.long("out")
30-
.short("o")
31-
.takes_value(true)
32-
)
33-
.arg(Arg::with_name("separator")
34-
.help("String that separates values.")
35-
.long("separator")
36-
.short("s")
37-
.default_value(",")
38-
)
16+
.arg(Arg::from_usage("[FILE] 'Reads table tables from this. [default: stdin]'"))
17+
.arg(Arg::from_usage("-o --out [FILE] 'Prints output to this. [default: stdout]'"))
18+
.arg(Arg::from_usage("-s --separator [STRING] 'Separates values. [default: ,]'"))
19+
.arg(Arg::from_usage("-m --minimize 'Minimizes table output'"))
3920
.get_matches();
4021

4122
Config {
4223
minimize: args.is_present("minimize"),
43-
separator: args.value_of("separator").map(String::from).unwrap(),
44-
file: args.value_of("file").map(String::from),
45-
outfile: args.value_of("outfile").map(String::from),
24+
separator: args.value_of("separator").unwrap_or(",").to_string(),
25+
file: args.value_of("FILE").map(String::from),
26+
out: args.value_of("out").map(String::from),
4627
}
4728
}
4829
}

src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use config::Config;
1010

1111
fn read_lines(file: &Option<String>) -> Result<Vec<String>> {
1212
match file {
13+
Some(f) => BufReader::new( File::open(f)? )
14+
.lines()
15+
.collect(),
1316
None => {
1417
let stdin = io::stdin();
1518
let lines = stdin.lock()
@@ -21,21 +24,18 @@ fn read_lines(file: &Option<String>) -> Result<Vec<String>> {
2124
.collect();
2225
lines
2326
},
24-
Some(f) => BufReader::new( File::open(f)? )
25-
.lines()
26-
.collect()
2727
}
2828
}
2929

3030
fn parse_table_data(lines: &Vec<String>, separator: &String) -> Vec<Vec<String>> {
31-
let mut rows: Vec<Vec<String>> = lines.iter()
31+
let mut rows = lines.iter()
3232
.map(|line| line
3333
.split(separator)
3434
.map(|word| word.trim())
3535
.map(String::from)
36-
.collect()
36+
.collect::<Vec<_>>()
3737
)
38-
.collect();
38+
.collect::<Vec<_>>();
3939
let max_len = rows.iter()
4040
.map(|row| row.len())
4141
.max()
@@ -87,8 +87,8 @@ fn format_pretty(data: &Vec<Vec<String>>) -> String {
8787

8888
fn main() -> Result<()> {
8989
let config = Config::from_args();
90-
let lines = read_lines(&config.file)?;
91-
let data = parse_table_data(&lines, &config.separator);
90+
let lines = read_lines(&config.file)?;
91+
let data = parse_table_data(&lines, &config.separator);
9292

9393
if data.len() < 2 || data[0].len() == 0 {
9494
eprintln!("Bad Input: Table requires at least 2 rows (including header) and 1 column.");
@@ -99,9 +99,9 @@ fn main() -> Result<()> {
9999
true => format_minimized(&data),
100100
false => format_pretty(&data),
101101
} + "\n";
102-
match config.outfile {
103-
None => print!("{}", &table),
102+
match config.out {
104103
Some(f) => fs::write(f, &table)?,
104+
None => print!("{}", &table),
105105
};
106106
Ok(())
107107
}

0 commit comments

Comments
 (0)