Created
November 9, 2012 16:33
-
-
Save kipanshi/4046702 to your computer and use it in GitHub Desktop.
Exception handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern mod std; | |
extern mod docopt; | |
use send_map::linear::LinearMap; | |
use io::{ReaderUtil, WriterUtil}; | |
/// Reads whole stream and outputs it as string | |
fn read_whole_stream_to_str(input_stream: io::Reader) -> ~str { | |
let mut buf = ~[]; | |
loop { | |
let ch = input_stream.read_byte(); | |
if input_stream.eof() { break; } | |
buf.push(ch as u8); | |
} | |
str::from_bytes(buf) | |
} | |
fn main () { | |
let input_stream = io::stdin(); | |
// let doc: ~str = read_whole_stream_to_str(input_stream); | |
let doc = ~"some fancy docstring"; | |
let docopt_result: Result<~LinearMap<~str, ~str>, ()> = do task::try { | |
let options = docopt::docopt(copy doc); | |
move options | |
}; | |
match docopt_result { | |
Ok(options) => io::println( | |
fmt!("%?", options)), | |
Err(options) => io::println(fmt!("%?", options.to_str())) | |
} | |
/** | |
* I get this: | |
* | |
~{ k0: 4644902254958875435, k1: 16078254338373962874, resize_at: 24, size: 1, buckets: | |
~[ None, Some({ hash: 7004769276290571297, key: ~"some fancy string", value: ~"some fancy string" }), | |
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, | |
None, None, None, None, None, None, None, None, None, None, None, None, None, None ] } | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment