From 81122c1297d4cfa2c811bb775308ff412ba674b1 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 9 Feb 2021 21:21:20 +0100 Subject: feat(users/Profpatsch/execline/exec_helpers): add args() Some programs need an exact amount of arguments, and we want to fail if they get too many or not enough. Change-Id: Ic703949f38780718f26118b896e7c7d7aa5553d9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2504 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/execline/exec_helpers.rs | 10 ++++++++++ users/Profpatsch/netencode/default.nix | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'users') diff --git a/users/Profpatsch/execline/exec_helpers.rs b/users/Profpatsch/execline/exec_helpers.rs index bc4650fbad9a..b9e1f5797386 100644 --- a/users/Profpatsch/execline/exec_helpers.rs +++ b/users/Profpatsch/execline/exec_helpers.rs @@ -11,6 +11,16 @@ pub fn no_args(current_prog_name: &str) -> () { } } +pub fn args(current_prog_name: &str, no_of_positional_args: usize) -> Vec> { + let mut args = std::env::args_os(); + // remove argv[0] + let _ = args.nth(0); + if args.len() != no_of_positional_args { + die_user_error(current_prog_name, format!("Expected {} arguments, got {}, namely {:?}", no_of_positional_args, args.len(), args.collect::>())) + } + args.map(|arg| arg.into_vec()).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/netencode/default.nix b/users/Profpatsch/netencode/default.nix index 92672470e902..4965bb74f20c 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -109,7 +109,7 @@ let fn main() { let mut buf = vec![]; - let (args, prog) = exec_helpers::args_for_exec("record-get", 1); + let args = exec_helpers::args("record-get", 1); let field = match std::str::from_utf8(&args[0]) { Ok(f) => f, Err(_e) => exec_helpers::die_user_error("record-get", format!("The field name needs to be valid unicode")) -- cgit 1.4.1