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 +++++++++ users/Profpatsch/read-http.rs | 2 ++ 2 files changed, 11 insertions(+) (limited to 'users/Profpatsch') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index b0b0984911a5..bc4650fbad9a 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] diff --git a/users/Profpatsch/read-http.rs b/users/Profpatsch/read-http.rs index afbfbfdb6714..50ff663b994a 100644 --- a/users/Profpatsch/read-http.rs +++ b/users/Profpatsch/read-http.rs @@ -22,6 +22,8 @@ enum What { // The keys are text, but can be lists of text iff headers appear multiple times, so beware. fn main() -> std::io::Result<()> { + exec_helpers::no_args("read-http"); + let args = dec::RecordDot { field: "what", inner: dec::OneOf { -- cgit 1.4.1