diff options
author | Profpatsch <mail@profpatsch.de> | 2021-02-09T20·06+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-02-13T20·00+0000 |
commit | 060600f0d796d096979f09304292fb998778cc51 (patch) | |
tree | 98bd654efd4212a9a269c2c14806884c863643a8 | |
parent | 1e5baa0dea9375ffc9b446b369e552876f6a15fd (diff) |
feat(users/Profpatsch/execline/exec_helpers): add no_args() r/2206
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 <mail@profpatsch.de>
-rw-r--r-- | users/Profpatsch/execline/exec_helpers.rs | 9 | ||||
-rw-r--r-- | users/Profpatsch/read-http.rs | 2 |
2 files changed, 11 insertions, 0 deletions
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::<Vec<_>>())) + } +} + pub fn args_for_exec(current_prog_name: &str, no_of_positional_args: usize) -> (Vec<Vec<u8>>, Vec<Vec<u8>>) { 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 { |