src/main.rs

changeset 3
cec573b16b46
parent 2
254e1e4bd795
child 6
de1cf8032322
equal deleted inserted replaced
2:254e1e4bd795 3:cec573b16b46
1 // The main documentation is in the README. 1 // The main documentation is in the README.
2 #![doc = include_str!("../README.md")] 2 #![doc = include_str!("../README.md")]
3 3
4 use std::io; 4 use std::io;
5 use std::io::BufWriter; 5 use std::fs::File;
6 use std::io::{BufWriter, BufRead, BufReader};
6 use std::io::Write; 7 use std::io::Write;
7 use clap::Parser; 8 use clap::Parser;
8 9
9 /// Command line parameters 10 /// Command line parameters
10 #[derive(Parser, Debug)] 11 #[derive(Parser, Debug)]
12 about = env!("CARGO_PKG_DESCRIPTION"), 13 about = env!("CARGO_PKG_DESCRIPTION"),
13 author = env!("CARGO_PKG_AUTHORS"), 14 author = env!("CARGO_PKG_AUTHORS"),
14 version = env!("CARGO_PKG_VERSION"), 15 version = env!("CARGO_PKG_VERSION"),
15 )] 16 )]
16 struct CommandLineArgs { 17 struct CommandLineArgs {
18 /// Input file (default is stdin)
19 input : Option<String>,
20
21 /// Output file (defalt is stdout)
22 #[arg(long, short = 'o')]
23 output : Option<String>,
24
17 #[arg(long, short = 'c')] 25 #[arg(long, short = 'c')]
18 /// Strip comments 26 /// Strip comments
19 strip_comments : bool, 27 strip_comments : bool,
20 28
21 #[arg(long, short = 'w')] 29 #[arg(long, short = 'w')]
111 } 119 }
112 } 120 }
113 121
114 fn main() { 122 fn main() {
115 let cli = CommandLineArgs::parse(); 123 let cli = CommandLineArgs::parse();
116 let input = io::stdin(); 124 let input = cli.input.map_or_else(
117 125 || Box::new(BufReader::new(io::stdin())) as Box<dyn BufRead>,
126 |f| Box::new(BufReader::new(File::open(f).unwrap())) as Box<dyn BufRead>
127 );
128 let output = cli.output.map_or_else(
129 || Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>,
130 |f| Box::new(BufWriter::new(File::create(f).unwrap())) as Box<dyn Write>
131 );
132
118 let mut o = Out { 133 let mut o = Out {
119 only_whitespace : true, 134 only_whitespace : true,
120 stored_whitespace : String::new(), 135 stored_whitespace : String::new(),
121 output : BufWriter::new(io::stdout()), 136 output,
122 stack : Vec::new(), 137 stack : Vec::new(),
123 whitespace_satisfied : true, 138 whitespace_satisfied : true,
124 par_satisfied : true, 139 par_satisfied : true,
125 }; 140 };
126 141

mercurial