Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created March 23, 2015 22:42
Show Gist options
  • Save adamhjk/49751914f36ee609694f to your computer and use it in GitHub Desktop.
Save adamhjk/49751914f36ee609694f to your computer and use it in GitHub Desktop.
sysadmin crate?
extern crate sysadmin;
use sysadmin::{file};
// Use docopt.rs to generate the cli arguments, process ARGV,
// and set up a main() that runs the run function. Checks the
// results and runs at_exit with nice error messages and a variety
// of error codes
cli!(
"Usage: wc [--lines] <file>
-l, --lines Show the count of lines
"
);
fn run(args: Args) -> Result<SysadminError> {
let Args{cmd_file: filename, flag_lines: lines} = args;
// Raise good looking errors for IO, file not found, etc
// We would only have to make these once
let file = try!(file::open(&filename));
if flag_lines {
lines = try!(file.split(b'\n'));
prinln!("lines: {}", lines.count());
} else {
spaces = try!(file.split(b' '));
println!("words: {}", spaces.count());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment