
struct Args {
thing: String,
number: u32,
shout: bool,
}
fn parse_args() -> Result<Args, lexopt::Error> {
use lexopt::prelude::*;
let mut thing = None;
let mut number = 1;
let mut shout = false;
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()? {
match arg {
Short('n') | Long("number") => {
number = parser.value()?.parse()?;
}
Long("shout") => {
shout = true;
}
Value(val) if thing.is_none() => {
thing = Some(val.into_string()?);
}
Long("help") => {
println!("Usage: hello [-n|--number=NUM] [--shout] THING");
std::process::exit(0);
}
_ => return Err(arg.unexpected()),
}
}
Ok(Args {
thing: thing.ok_or("missing argument THING")?,
number,
shout,
})
}
fn main() -> Result<(), lexopt::Error> {
let args = parse_args()?;
let mut message = format!("Hello {}", args.thing);
if args.shout {
message = message.to_uppercase();
}
for _ in 0..args.number {
println!("{}", message);
}
Ok(())
}
struct MyState;
impl highnoon::State for MyState {
type Context = ();
fn new_context(&self) -> Context {
()
}
}
#[tokio::main]
async fn main() -> highnoon::Result<()> {
let mut app = highnoon::App::new(MyState);
app.with(highnoon::filter::Log);
app.at("/hello").get(|_request| async {
"Hello world!\n\n"
});
app.listen("0.0.0.0:8888").await?;
Ok(())
}
From 日报小组 洋芋
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。