From 4b8568cb4ff20a87bf2859bb0eb63c5afd3831d7 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Thu, 25 Jul 2024 17:17:35 +0200 Subject: [PATCH] termproxy: fix parsing free-form listen-port argument While commit d8e9c1a ("termproxy: fix the command line help text") corrected the usage text to match the behavior and was a good stop-gap it simply is to easy to make this work as most CLI tools do and most user expect. Switch to parsing all options first and only then get the free-form listen-port(-or-fd) argument. Signed-off-by: Thomas Lamprecht --- termproxy/src/cli.rs | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/termproxy/src/cli.rs b/termproxy/src/cli.rs index 553374f..e8a12b2 100644 --- a/termproxy/src/cli.rs +++ b/termproxy/src/cli.rs @@ -4,7 +4,7 @@ use std::os::fd::RawFd; use anyhow::{bail, Result}; const CMD_HELP: &str = "\ -Usage: proxmox-termproxy [OPTIONS] --path -- ... +Usage: proxmox-termproxy [OPTIONS] --path -- ... Arguments: Port or file descriptor to listen for TCP connections @@ -86,23 +86,33 @@ impl Options { bail!("missing terminal command or -- option-end marker, see '-h' for usage"); } + let terminal_command = terminal_command.unwrap(); // checked above + let port_as_fd = args.contains("--port-as-fd"); + let acl_path = args.value_from_str("--path")?; + let acl_permission = args.opt_value_from_str("--perm")?; + let api_daemon_address = { + let auth_port: Option = args.opt_value_from_str("--authport")?; + let auth_socket: Option = args.opt_value_from_str("--auth-socket")?; + match (auth_port, auth_socket) { + (Some(auth_port), None) => DaemonAddress::Port(auth_port), + (None, Some(auth_socket)) => DaemonAddress::UnixSocket(auth_socket), + (None, None) => DaemonAddress::Port(85), + (Some(_), Some(_)) => bail!( + "conflicting options: --auth-port and --auth-socket are mutually exclusive." + ), + } + }; + + // NOTE: free-form arguments are literally the next unused argument, so only get them after + // all options got parsed + let auth_port_or_fd = args.free_from_str()?; + let options = Self { - terminal_command: terminal_command.unwrap(), // checked above - listen_port: PortOrFd::from_cli(args.free_from_str()?, args.contains("--port-as-fd"))?, - api_daemon_address: { - let authport: Option = args.opt_value_from_str("--authport")?; - let authsocket: Option = args.opt_value_from_str("--authsocket")?; - match (authport, authsocket) { - (Some(authport), None) => DaemonAddress::Port(authport), - (None, Some(authsocket)) => DaemonAddress::UnixSocket(authsocket), - (None, None) => DaemonAddress::Port(85), - (Some(_), Some(_)) => bail!( - "conflicting options: --authport and --authsocket are mutually exclusive." - ), - } - }, - acl_path: args.value_from_str("--path")?, - acl_permission: args.opt_value_from_str("--perm")?, + terminal_command, + listen_port: PortOrFd::from_cli(auth_port_or_fd, port_as_fd)?, + api_daemon_address, + acl_path, + acl_permission, }; if !args.finish().is_empty() { -- 2.39.5