diff --git a/Cargo.toml b/Cargo.toml index 83e1751..a45b5bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ anstyle = "1.0.8" anyhow = "1.0.89" capnp = "0.20.1" capnp-futures = "0.20.0" -clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help"] } +clap = { version = "4.5.19", features = ["wrap_help", "derive", "cargo", "help", "string"] } console = "0.15.8" dns-lookup = "2.0.4" futures-util = { version = "0.3.31", default-features = false } diff --git a/src/cli/args.rs b/src/cli/args.rs index 1ae6b1e..5b6154b 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -1,8 +1,7 @@ // QCP top-level command-line arguments // (c) 2024 Ross Younger -use crate::build_info; -use clap::Parser; +use clap::{Args as _, FromArgMatches as _, Parser}; /// Options that switch us into another mode i.e. which don't require source/destination arguments pub(crate) const MODE_OPTIONS: &[&str] = &["server", "help_buffers"]; @@ -10,8 +9,7 @@ pub(crate) const MODE_OPTIONS: &[&str] = &["server", "help_buffers"]; #[derive(Debug, Parser, Clone)] #[command( author, - version, - version(build_info::GIT_VERSION), + // we set short/long version strings explicitly, see custom_parse() about, before_help = "e.g. qcp some/file my-server:some-directory/", infer_long_args(true) @@ -70,3 +68,13 @@ pub(crate) struct CliArgs { // // N.B. ClientOptions has positional arguments! } + +impl CliArgs { + /// Sets up and executes ou + pub(crate) fn custom_parse() -> Self { + let cli = clap::Command::new(clap::crate_name!()); + let ver = crate::version::short(); + let cli = CliArgs::augment_args(cli).version(ver); + CliArgs::from_arg_matches(&cli.get_matches_from(std::env::args_os())).unwrap() + } +} diff --git a/src/cli/cli_main.rs b/src/cli/cli_main.rs index 56dda72..eac3b90 100644 --- a/src/cli/cli_main.rs +++ b/src/cli/cli_main.rs @@ -11,7 +11,6 @@ use crate::{ server::server_main, util::setup_tracing, }; -use clap::Parser; use indicatif::{MultiProgress, ProgressDrawTarget}; use tracing::error_span; @@ -23,7 +22,7 @@ use tracing::error_span; #[tokio::main(flavor = "current_thread")] #[allow(clippy::missing_panics_doc)] pub async fn cli() -> anyhow::Result { - let args = CliArgs::parse(); + let args = CliArgs::custom_parse(); if args.help_buffers { os::print_udp_buffer_size_help_message( args.bandwidth.recv_buffer(), diff --git a/src/lib.rs b/src/lib.rs index 890bea0..c1016ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,8 +66,4 @@ pub mod doc; pub mod os; -/// Build-time info (autogenerated by `built`) -#[allow(unreachable_pub)] -mod build_info { - include!(concat!(env!("OUT_DIR"), "/built.rs")); -} +mod version; diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000..d0960df --- /dev/null +++ b/src/version.rs @@ -0,0 +1,19 @@ +//! Autogenerated build-time information +// (c) 2024 Ross Younger + +/// Build-time info (autogenerated by `built`) +#[allow(unreachable_pub)] +mod build_info { + include!(concat!(env!("OUT_DIR"), "/built.rs")); +} + +/// Short version string +/// +/// If possible use GIT_VERSION; if not, do what we can +pub(crate) fn short() -> String { + if let Some(ver) = build_info::GIT_VERSION { + return ver.into(); + } + let hash = build_info::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown"); + format!("{}-{}", clap::crate_version!(), hash) +}