Skip to content

Commit c502008

Browse files
committed
backend: handle version/help flags before logger init
1 parent 38ed0e3 commit c502008

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

anycode-backend/src/config.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn print_help() {
156156
println!();
157157
println!("OPTIONS:");
158158
println!(" -h, --help Print help information");
159-
println!(" --version Print version information");
159+
println!(" -v, -V, --version Print version information");
160160
println!(" -p, --port <PORT> Port to listen on");
161161
println!();
162162
println!("ENVIRONMENT:");
@@ -167,6 +167,28 @@ fn print_help() {
167167
println!("Start the anycode server. The server will be available at http://localhost:<port>");
168168
}
169169

170+
pub fn handle_early_cli_flags() {
171+
let mut args = std::env::args().skip(1);
172+
173+
while let Some(arg) = args.next() {
174+
match arg.as_str() {
175+
"--help" | "-h" => {
176+
print_help();
177+
std::process::exit(0);
178+
}
179+
"--version" | "-V" | "-v" => {
180+
println!("anycode {}", env!("CARGO_PKG_VERSION"));
181+
std::process::exit(0);
182+
}
183+
"--port" | "-p" => {
184+
// Skip value for port flag in early pass.
185+
let _ = args.next();
186+
}
187+
_ => {}
188+
}
189+
}
190+
}
191+
170192
fn parse_port(value: &str, source: &str) -> Result<u16> {
171193
value
172194
.parse::<u16>()
@@ -183,7 +205,7 @@ pub fn resolve_server_port() -> Result<u16> {
183205
print_help();
184206
std::process::exit(0);
185207
}
186-
"--version" | "-V" => {
208+
"--version" | "-V" | "-v" => {
187209
println!("anycode {}", env!("CARGO_PKG_VERSION"));
188210
std::process::exit(0);
189211
}

anycode-backend/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ async fn not_found() -> Response {
242242

243243
#[tokio::main]
244244
async fn main() -> Result<()> {
245+
crate::config::handle_early_cli_flags();
246+
245247
tracing_subscriber::fmt()
246248
.with_env_filter(tracing_subscriber::EnvFilter::new("info"))
247249
.init();

0 commit comments

Comments
 (0)