-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem Definition: Reusable Partial CLI Parsing #37
Comments
Proposition: Bind is provided by a dedicated crate: #[macro_use]
extern crate structopt;
use structopt::StructOpt;
use structopt::clap::ArgGroup;
use std::net::TcpListener;
use std::os::raw::c_int;
use std::os::unix::io::FromRawFd;
use std::io;
#[derive(StructOpt, Debug)]
// this annotation should be moved to Bind, but that doesn't work
// actually. Maybe creating a structopt issue?
#[structopt(raw(group = r#"ArgGroup::with_name("bind").required(true)"#))]
struct Opt {
#[structopt(long = "debug")]
debug: bool,
#[structopt(flatten)]
bind: Bind,
}
#[derive(StructOpt, Debug)]
struct Bind {
#[structopt(short = "p", long = "port", env = "PORT", group = "bind")]
port: Option<u16>,
#[structopt(long = "file-descriptor", env = "FD", group = "bind")]
fd: Option<c_int>,
}
impl Bind {
fn bind(&self) -> std::io::Result<TcpListener> {
match self {
Bind { fd: Some(fd), .. } => unsafe {
Ok(TcpListener::from_raw_fd(*fd))
},
Bind { port: Some(port), .. } => TcpListener::bind(("*", *port)),
_ => Err(io::Error::new(io::ErrorKind::Other, "Not port supplied")),
}
}
}
fn main() -> io::Result<()> {
let opt = Opt::from_args();
let _bind = opt.bind.bind()?;
Ok(())
} |
Published the |
Ah, thanks for the reminder! I moved the verbosity-flag repo to https://github.com/rust-clique/clap-verbosity-flag! |
I think we have a pretty good thing going here, but we still need to make this a bit more popular. Any ideas how to do that? Long-term we might use these crates in getting-started guide or things like quicli, but we should have a larger user base to validate that they are actually helpful before doing that. |
@killercup I think a good first step is to add more documentation. This should perhaps include a We already got one of the crates featured in "this week in rust". We're now on I think perhaps another avenue worth exploring is creating PRs to the Rust cookbook. It seems be having some traction, and might be a good place for exposure. |
@yoshuawuyts I assigned this to you for now to settle the popularity thing but feel free to ping me :) maybe we can piggy-back on clap3 and its website? :) |
Since "only" docs and publicity remain, I'm closing this as finished (🎉) and let's move the discussion to issues focusses specifically on docs (#45) |
When thinking about networked services, one of the basic command line requirements is to accept a port to listen to.
This is usually expressed through the
--port
command. But that's not the only version; while talking in person, we've encountered the following variations:--port
to accept a port (number)-p
as a shorthand for--port
$PORT
, environment variable to accept a port (number)$LISTEN_FD
to forward a socket, usually from SystemdFor an average program, this is a lot to think about. If we were to write this for every application we build, it'd both end up being repetitive boilerplate, and easy to get wrong.
So the question we're asking here is: how can we turn this information into a reusable component?
There's a few more constraints we need to think about here:
We're not sure what the solution to these problems are, but we do know that it would be beneficial to solve them. We'll continue from here by experimenting, and we encourage people interested in this to do the same.
Thanks!
The text was updated successfully, but these errors were encountered: