Add command line parsing

This commit is contained in:
Brian Anderson 2012-05-05 16:00:25 -07:00
parent d550defdf3
commit e0149a6043
3 changed files with 54 additions and 1 deletions

47
src/servo/opts.rs Normal file
View file

@ -0,0 +1,47 @@
#[doc = "
Configuration options for a single run of the servo application. Created
from command line arguments.
"];
type opts = {
urls: [str],
render_mode: render_mode
};
enum render_mode {
screen,
file(str)
}
fn from_cmdline_args(args: [str]) -> opts {
import std::getopts;
let args = args.tail();
let opts = [
getopts::optopt("o")
];
let match = alt getopts::getopts(args, opts) {
result::ok(m) { m }
result::err(f) { fail getopts::fail_str(f) }
};
let urls = if match.free.is_empty() {
fail "servo asks that you provide 1 or more URLs"
} else {
match.free
};
let render_mode = alt getopts::opt_maybe_str(match, "o") {
some(output_file) { file(output_file) }
none { screen }
};
{
urls: urls,
render_mode: render_mode
}
}

View file

@ -49,3 +49,4 @@ mod util {
}
mod content;
mod opts;

View file

@ -6,6 +6,11 @@ import gfx::renderer;
import platform::osmain;
fn main(args: [str]) {
run(opts::from_cmdline_args(args))
}
fn run(opts: opts::opts) {
// The platform event handler thread
let osmain = osmain::osmain();
@ -23,7 +28,7 @@ fn main(args: [str]) {
let content = content::content(layout);
// Send each file to render then wait for keypress
for args.tail().each { |filename|
for opts.urls.each { |filename|
#debug["master: Sending filename `%s`", filename];
content.send(content::parse(filename));
#debug["master: Waiting for keypress"];