Browse Source
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.pull/1019/head
Jakob Stoklund Olesen
9 years ago
5 changed files with 194 additions and 1 deletions
@ -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(()) |
|||
} |
@ -1,5 +1,65 @@ |
|||
|
|||
extern crate cretonne; |
|||
extern crate cton_reader; |
|||
extern crate docopt; |
|||
extern crate rustc_serialize; |
|||
|
|||
fn main() {} |
|||
use cretonne::VERSION; |
|||
use docopt::Docopt; |
|||
use std::io::{self, Write}; |
|||
use std::process; |
|||
|
|||
|
|||
mod cat; |
|||
|
|||
const USAGE: &'static str = " |
|||
Cretonne code generator utility |
|||
|
|||
Usage: |
|||
cton-util cat <file>... |
|||
cton-util --help | --version |
|||
|
|||
Options: |
|||
-h, --help print this help message |
|||
--version print the Cretonne version |
|||
|
|||
"; |
|||
|
|||
#[derive(RustcDecodable, Debug)] |
|||
struct Args { |
|||
cmd_cat: bool, |
|||
arg_file: Vec<String>, |
|||
} |
|||
|
|||
/// A command either succeeds or fails with an error message.
|
|||
pub type CommandResult = Result<(), String>; |
|||
|
|||
/// Parse the command line arguments and run the requested command.
|
|||
fn cton_util() -> CommandResult { |
|||
// Parse comand line arguments.
|
|||
let args: Args = Docopt::new(USAGE) |
|||
.and_then(|d| { |
|||
d.help(true) |
|||
.version(Some(format!("Cretonne {}", VERSION))) |
|||
.decode() |
|||
}) |
|||
.unwrap_or_else(|e| e.exit()); |
|||
|
|||
// Find the sub-command to execute.
|
|||
if args.cmd_cat { |
|||
cat::run(args.arg_file) |
|||
} else { |
|||
// Debugging / shouldn't happen with proper command line handling above.
|
|||
Err(format!("Unhandled args: {:?}", args)) |
|||
} |
|||
} |
|||
|
|||
fn main() { |
|||
if let Err(mut msg) = cton_util() { |
|||
if !msg.ends_with('\n') { |
|||
msg.push('\n'); |
|||
} |
|||
io::stderr().write(msg.as_bytes()).unwrap(); |
|||
process::exit(1); |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue