Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Easily add address & port flags to CLIs using Clap

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
NotificationsYou must be signed in to change notification settings

clap-rs/clap-port-flag

crates.io versionbuild statusdownloadsdocs.rs docs

Easily add a--port flag to CLIs using clap.

Usage

Example: Base

With the following code insrc/main.rs:

use clap::Parser;use clap_port_flag::Port;#[derive(Debug,Parser)]structCli{#[clap(flatten)]port:Port,}fnmain(){let args =Cli::parse();let _tcp_listener = args.port.bind().unwrap();}

When you run the binary, it'll provide the following output:

my-cool-app 0.2.0Alice Person <alice@person.com>Application that does things over TCP.USAGE:    main [OPTIONS]FLAGS:    -h, --help       Prints help information    -V, --version    Prints version informationOPTIONS:        --listen-fd <fd>         A previously opened network socket. [env: LISTEN_FD=]    -a, --address <hostname>     The network address to listen to. [default: 127.0.0.1]    -p, --port <port>            The network port to listen to. [env: PORT=]

Example: Hyper

use clap_port_flag::Port;use futures::prelude::*;use hyper::service::service_fn;use hyper::{Body,Response,Request};use clap::Parser;#[derive(Debug,Parser)]structCli{#[clap(flatten)]port:Port,}asyncfnhello(_:Request<Body>) ->Result<Response<String>, std::convert::Infallible>{Ok(Response::new(String::from("Hello World!")))}#[tokio::main]asyncfnmain() ->Result<(),Box<dyn std::error::Error>>{let args =Cli::parse();let listener = args.port.bind()?;let listener = tokio::net::TcpListener::from_std(listener)?;let addr = listener.local_addr()?;println!("Server listening on {}", addr);let(stream, _) = listener.accept().await?;ifletErr(e) = hyper::server::conn::Http::new().serve_connection(stream,service_fn(hello)).await{eprintln!("server error: {}", e);}Ok(())}

Installation

$ cargo add clap-port-flag

Further Reading

Acknowledgements

The original version of this crate was sketched out by@TeXitoi inrust-lang-nursery/cli-wg#37.

License

MIT ORApache-2.0

About

Easily add address & port flags to CLIs using Clap

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp