Custom subcommands

I need a program named program1 to take ANY subcommand c1 and run an executable or an alias named program1-c1 with the arguments supplied. Like how git and cargo do.

So program1 c1 -f1 -f2 -f3 should run program1-c1 -f1 -f2 -f3. Any library dedicated for this?

I don’t know of a crate, but it should be relatively straightforward to do it yourself. Something like this: (untested, probably buggy)

let mut args=std::env::ArgsOs();
let mut exe:Path = args.next().unwrap();
let ext:Option<OsStr> = exe.extension();
let mut basename: OsString: exe.file_stem().unwrap().into();
basename.push("-".parse().unwrap());
basename.push(args.next().unwrap());
let exe = exe.with_file_name(basename).with_extension(ext);
process::exit(Command::new(exe).args(args).status().unwrap().code().unwrap_or(0));
1 Like

I got something from cargo's source code.

Funny that something like this (probably) doesn't have its own package.

1 Like