From 060600f0d796d096979f09304292fb998778cc51 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 9 Feb 2021 21:06:07 +0100 Subject: feat(users/Profpatsch/execline/exec_helpers): add no_args() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some programs don’t need any arguments, so fail if they do get them, because that’s usually a bug. Change-Id: I28639056d3d9cea0cc0e7fcbfa42120c4f129c8c Reviewed-on: https://cl.tvl.fyi/c/depot/+/2503 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/exec_helpers.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'users/Profpatsch/execline') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index b0b0984911..bc4650fbad 100644 --- a/users/Profpatsch/execline/exec_helpers.rs +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -2,6 +2,15 @@ use std::os::unix::process::CommandExt; use std::ffi::OsStr; use std::os::unix::ffi::{OsStringExt, OsStrExt}; +pub fn no_args(current_prog_name: &str) -> () { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + if args.len() > 0 { + die_user_error(current_prog_name, format!("Expected no arguments, got {:?}", args.collect::>())) + } +} + pub fn args_for_exec(current_prog_name: &str, no_of_positional_args: usize) -> (Vec>, Vec>) { let mut args = std::env::args_os(); // remove argv[0] -- cgit 1.4.1