Easily add a --port
flag to CLIs using clap.
With the following code in src/main.rs
:
```rust,norun use clap::Parser; use clapport_flag::Port;
struct Cli { #[clap(flatten)] port: Port, }
fn main() { let args = Cli::parse(); let tcplistener = args.port.bind().unwrap(); } ```
When you run the binary, it'll provide the following output:
```txt my-cool-app 0.2.0 Alice Person alice@person.com Application that does things over TCP.
USAGE: main [OPTIONS]
FLAGS: -h, --help Prints help information -V, --version Prints version information
OPTIONS:
--listen-fd
```rust,norun use clapportflag::Port; use futures::prelude::*; use hyper::service::servicefn; use hyper::{Body, Response, Request}; use clap::Parser;
struct Cli { #[clap(flatten)] port: Port, }
async fn hello(_: Request
) -> Resultasync fn main() -> Result<(), Box
println!("Server listening on {}", addr);
let (stream, _) = listener.accept().await?;
if let Err(e) = hyper::server::conn::Http::new()
.serve_connection(stream, service_fn(hello))
.await
{
eprintln!("server error: {}", e);
}
Ok(())
} ```
sh
$ cargo add clap-port-flag
The original version of this crate was sketched out by @TeXitoi in rust-lang-nursery/cli-wg#37.
MIT OR Apache-2.0