mirror of
https://github.com/servo/servo.git
synced 2025-07-03 21:43:41 +01:00
Don't crash on bad command-line arguments
As amusing as it is to have servo --help die with SIGILL, it's not the best user experience :)
This commit is contained in:
parent
d7b8d103a9
commit
c277d25506
2 changed files with 19 additions and 10 deletions
|
@ -117,7 +117,7 @@ pub mod util;
|
||||||
#[start]
|
#[start]
|
||||||
fn start(argc: int, argv: **u8) -> int {
|
fn start(argc: int, argv: **u8) -> int {
|
||||||
native::start(argc, argv, proc() {
|
native::start(argc, argv, proc() {
|
||||||
run(opts::from_cmdline_args(os::args()))
|
opts::from_cmdline_args(os::args()).map(run);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ pub extern "C" fn android_start(argc: int, argv: **u8) -> int {
|
||||||
args.push(str::raw::from_c_str(*argv.offset(i as int) as *i8));
|
args.push(str::raw::from_c_str(*argv.offset(i as int) as *i8));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
run(opts::from_cmdline_args(args))
|
opts::from_cmdline_args(os::args()).map(run);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,8 @@ use azure::azure_hl::{CoreGraphicsAcceleratedBackend, Direct2DBackend, SkiaBacke
|
||||||
use extra::getopts::groups;
|
use extra::getopts::groups;
|
||||||
use std::num;
|
use std::num;
|
||||||
use std::rt;
|
use std::rt;
|
||||||
|
use std::io;
|
||||||
|
use std::os;
|
||||||
|
|
||||||
/// Global flags for Servo, currently set on the command line.
|
/// Global flags for Servo, currently set on the command line.
|
||||||
#[deriving(Clone)]
|
#[deriving(Clone)]
|
||||||
|
@ -59,7 +61,12 @@ fn print_usage(app: &str, opts: &[groups::OptGroup]) {
|
||||||
println(groups::usage(message, opts));
|
println(groups::usage(message, opts));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_cmdline_args(args: &[~str]) -> Opts {
|
fn args_fail(msg: &str) {
|
||||||
|
io::stderr().write_line(msg);
|
||||||
|
os::set_exit_status(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_cmdline_args(args: &[~str]) -> Option<Opts> {
|
||||||
let app_name = args[0].to_str();
|
let app_name = args[0].to_str();
|
||||||
let args = args.tail();
|
let args = args.tail();
|
||||||
|
|
||||||
|
@ -80,19 +87,21 @@ pub fn from_cmdline_args(args: &[~str]) -> Opts {
|
||||||
|
|
||||||
let opt_match = match groups::getopts(args, opts) {
|
let opt_match = match groups::getopts(args, opts) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => fail!(f.to_err_msg()),
|
Err(f) => {
|
||||||
|
args_fail(f.to_err_msg());
|
||||||
|
return None;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if opt_match.opt_present("h") || opt_match.opt_present("help") {
|
if opt_match.opt_present("h") || opt_match.opt_present("help") {
|
||||||
print_usage(app_name, opts);
|
print_usage(app_name, opts);
|
||||||
// TODO: how to return a null struct and let the caller know that
|
return None;
|
||||||
// it should abort?
|
|
||||||
fail!("")
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let urls = if opt_match.free.is_empty() {
|
let urls = if opt_match.free.is_empty() {
|
||||||
print_usage(app_name, opts);
|
print_usage(app_name, opts);
|
||||||
fail!(~"servo asks that you provide 1 or more URLs")
|
args_fail("servo asks that you provide 1 or more URLs");
|
||||||
|
return None;
|
||||||
} else {
|
} else {
|
||||||
opt_match.free.clone()
|
opt_match.free.clone()
|
||||||
};
|
};
|
||||||
|
@ -138,7 +147,7 @@ pub fn from_cmdline_args(args: &[~str]) -> Opts {
|
||||||
None => num::max(rt::default_sched_threads() * 3 / 4, 1),
|
None => num::max(rt::default_sched_threads() * 3 / 4, 1),
|
||||||
};
|
};
|
||||||
|
|
||||||
Opts {
|
Some(Opts {
|
||||||
urls: urls,
|
urls: urls,
|
||||||
render_backend: render_backend,
|
render_backend: render_backend,
|
||||||
n_render_threads: n_render_threads,
|
n_render_threads: n_render_threads,
|
||||||
|
@ -151,5 +160,5 @@ pub fn from_cmdline_args(args: &[~str]) -> Opts {
|
||||||
headless: opt_match.opt_present("z"),
|
headless: opt_match.opt_present("z"),
|
||||||
hard_fail: opt_match.opt_present("f"),
|
hard_fail: opt_match.opt_present("f"),
|
||||||
bubble_widths_separately: opt_match.opt_present("b"),
|
bubble_widths_separately: opt_match.opt_present("b"),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue