diff options
author | Profpatsch <mail@profpatsch.de> | 2021-02-09T20·21+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-02-13T20·00+0000 |
commit | 81122c1297d4cfa2c811bb775308ff412ba674b1 (patch) | |
tree | 29ed792cd7691e9a3c5441b03a690b2a9ed8a38b /users/Profpatsch/execline | |
parent | 060600f0d796d096979f09304292fb998778cc51 (diff) |
feat(users/Profpatsch/execline/exec_helpers): add args() r/2207
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 <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch/execline')
-rw-r--r-- | users/Profpatsch/execline/exec_helpers.rs | 10 |
1 files changed, 10 insertions, 0 deletions
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<Vec<u8>> { + 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::<Vec<_>>())) + } + args.map(|arg| arg.into_vec()).collect() +} + 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] |