Begin a basic command line interface.
Add an external dependency to the docopt package and use it for a scaffold command line interface for the cton-util command. I am not too happy about taking external dependencies, and docopt pulls in 13 other packages. However, I really don't want to be writing command line parsers, and as long as the external dependencies are confined to the tools crate, we should be fine. The core cretonne crate should stay free of external dependencies to avoid trouble with embedding it. Implement a basic 'cat' subcommand which currently behaves like unix 'cat'. It will gain parser powers soon.
This commit is contained in:
28
src/tools/cat.rs
Normal file
28
src/tools/cat.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
//! The `cat` sub-command.
|
||||
//!
|
||||
//! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of
|
||||
//! normalizing formatting and removing comments.
|
||||
|
||||
use CommandResult;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
pub fn run(files: Vec<String>) -> CommandResult {
|
||||
for (i, f) in files.into_iter().enumerate() {
|
||||
if i != 0 {
|
||||
println!("");
|
||||
}
|
||||
try!(cat_one(f))
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cat_one(filename: String) -> CommandResult {
|
||||
let mut file = try!(File::open(&filename).map_err(|e| format!("{}: {}", filename, e)));
|
||||
let mut buffer = String::new();
|
||||
try!(file.read_to_string(&mut buffer)
|
||||
.map_err(|e| format!("Couldn't read {}: {}", filename, e)));
|
||||
|
||||
print!("{}", buffer);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user