I have a fn main that is parsing arguments via StructOpt .. Args::from_args.
Is there a way to create this Args object without actually starting the executable for testing? Can I just create a Args object directly?
Can I do this for example
fn test_function() {
let args = Args::default();
args.param1 = "value1";
args.param2 = "value2";
core_function(args);
}
fn main() {
let args = Args::from_args();
core_function(args);
}
Yes, structopt also provides
from_iterandfrom_iter_safewhich do what you'd expect: they take iterables of strings, and parse those as if they were CLI args.All of them really delegate to clap, but semantically
from_argsjust callsfrom_iterwithargs_os()as parameter.from_iter_safeis probably the one you want to use in a test: much likefrom_args,from_iterwill print an error message and immediately exit if a parsing / matching error occurs.