2
0
mirror of https://github.com/xvxx/phd synced 2024-11-12 07:10:45 +00:00
phd/src/main.rs

79 lines
2.0 KiB
Rust
Raw Normal View History

2019-12-27 03:51:59 +00:00
use phd;
2019-12-27 03:57:59 +00:00
use std::process;
2019-12-23 02:05:48 +00:00
2019-12-28 22:06:43 +00:00
const DEFAULT_HOST: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 7070;
2019-12-23 02:05:48 +00:00
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
2019-12-27 23:42:57 +00:00
let mut root = ".";
2019-12-27 04:59:34 +00:00
let mut iter = args.iter();
2019-12-28 22:06:43 +00:00
let mut host = DEFAULT_HOST;
let mut port = DEFAULT_PORT;
2019-12-27 04:59:34 +00:00
while let Some(arg) = iter.next() {
match arg.as_ref() {
"--version" | "-v" | "-version" => return print_version(),
2019-12-28 00:52:15 +00:00
"--help" | "-help" => return print_help(),
2019-12-27 04:59:34 +00:00
"--port" | "-p" | "-port" => {
if let Some(p) = iter.next() {
2019-12-28 04:09:41 +00:00
port = p
.parse()
.map_err(|_| {
eprintln!("bad port: {}", p);
process::exit(1)
})
.unwrap();
2019-12-27 04:59:34 +00:00
}
}
2019-12-28 00:52:15 +00:00
"-h" => {
if let Some(h) = iter.next() {
host = h;
} else {
return print_help();
}
}
"--host" | "-host" => {
2019-12-27 04:59:34 +00:00
if let Some(h) = iter.next() {
2019-12-27 23:42:57 +00:00
host = h;
2019-12-27 04:59:34 +00:00
}
}
_ => {
if let Some('-') = arg.chars().nth(0) {
eprintln!("unknown flag: {}", arg);
process::exit(1);
} else {
2019-12-27 23:42:57 +00:00
root = arg;
2019-12-27 04:59:34 +00:00
}
}
}
2019-12-27 03:57:59 +00:00
}
2019-12-27 23:42:57 +00:00
if let Err(e) = phd::server::start(host, port, root) {
2019-12-23 02:05:48 +00:00
eprintln!("{}", e);
}
}
2019-12-27 03:57:59 +00:00
2019-12-27 04:59:34 +00:00
fn print_help() {
2019-12-27 03:57:59 +00:00
println!(
"Usage:
2019-12-28 00:52:15 +00:00
phd [options] <root directory>
2019-12-27 03:57:59 +00:00
Options:
2019-12-28 22:06:43 +00:00
-p, --port Port to bind to. [Default: {port}]
-h, --host Hostname when generating links. [Default: {host}]
2019-12-28 00:52:15 +00:00
Other flags:
2019-12-27 03:57:59 +00:00
-h, --help Print this screen.
2019-12-28 22:06:43 +00:00
-v, --version Print phd version.",
host = DEFAULT_HOST,
port = DEFAULT_PORT,
2019-12-27 03:57:59 +00:00
);
}
fn print_version() {
println!("phd v{}", env!("CARGO_PKG_VERSION"));
}