hostname: ensure that this works on Windows and improve help menu

This commit is contained in:
Arcterus 2014-02-08 18:28:48 -08:00
parent 79acabb381
commit 9d432b0a26

View file

@ -36,11 +36,11 @@ fn main () {
let matches = match getopts(args.tail(), options) { let matches = match getopts(args.tail(), options) {
Ok(m) => { m } Ok(m) => { m }
_ => { println!("{:s}", usage(program, options)); return; } _ => { help_menu(program, options); return; }
}; };
if matches.opt_present("h") { if matches.opt_present("h") {
println!("{:s}", usage(program, options)); help_menu(program, options);
return return
} }
if matches.opt_present("V") { version(); return } if matches.opt_present("V") { version(); return }
@ -60,16 +60,25 @@ fn main () {
println!("{:s}", hostname); println!("{:s}", hostname);
} }
1 => { xsethostname( matches.free.last().unwrap() ) } 1 => { xsethostname( matches.free.last().unwrap() ) }
_ => { println!("{:s}", usage(program, options)); } _ => { help_menu(program, options); }
}; };
} }
fn version() { fn version() {
println!("hostname version 1.0.0"); println!("hostname 1.0.0");
}
fn help_menu(program: &str, options: &[getopts::OptGroup]) {
version();
println!("");
println!("Usage:");
println!(" {:s} [OPTION]... [HOSTNAME]", program);
println!("");
print!("{:s}", usage("Print or set the system's host name.", options));
} }
fn xgethostname() -> ~str { fn xgethostname() -> ~str {
let namelen = 255u; let namelen = 256u;
let mut name = vec::from_elem(namelen, 0u8); let mut name = vec::from_elem(namelen, 0u8);
let err = unsafe { let err = unsafe {