mkwin: added a 'find' flag to use 'find' to locate binaries with changing paths

This commit is contained in:
Valerie Wolfe 2024-06-25 09:45:01 -04:00
parent 63f8e69afc
commit 9738b9a0e9
3 changed files with 26 additions and 9 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "mkwin"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
[dependencies]

View file

@ -1,7 +1,8 @@
//! constants for flag arguments
pub const HELP: [&str;2] = ["-h", "--help"];
pub const EMPTY: &str = "--empty";
pub const EMPTY: [&str;2] = ["-e", "--empty"];
pub const FIND_BIN: &str = "--find";
pub const PATH_CONVERT: &str = "--pc";
pub const QUIET: [&str;2] = ["-q", "--quiet"];

View file

@ -51,6 +51,13 @@ pub fn main() {
}
}
// consume find binary flag
let find_bin: Option<String>;
match args.value_from_str::<&str, String>(flag::FIND_BIN) {
Ok(inner) => find_bin = Some(inner),
_ => find_bin = None
}
// consume simple flags
let empty = args.contains(flag::EMPTY);
let quiet = args.contains(flag::QUIET);
@ -58,20 +65,25 @@ pub fn main() {
// get target executable
let target: String;
if let Ok(Some(arg)) = args.subcommand() {
if let Some(root) = find_bin { // handle find binary flag
target = format!("target=`find {root} -name '{arg}'`\n\"$target\"");
} else { // handle normal path
let path = Path::new(&arg);
if let Ok(path) = path.canonicalize() {
target = path.to_string_lossy().into();
let path: String = path.to_string_lossy().into();
target = format!("'{path}'");
} else {
error::canonicalize_fail(arg);
return;
}
}
} else {
error::missing_target();
return;
}
// construct output
print!("#!/usr/bin/bash\n# generated with: mkwin{arg_string}\n\n'{target}'");
print!("#!/usr/bin/bash\n# generated with: mkwin{arg_string}\n\n{target}");
// handle forwarded arguments
if !forwarded.is_empty() {
@ -115,6 +127,10 @@ flags:
--empty The resulting script will not pass arguments.
--find=<root> The resulting script will use the 'find' utility to locate
the target binary in the given directory. The value given by
root is passed verbatim to 'find', unexpanded.
--pc=<flags> The resulting script will use the 'path-convert' tool to
convert arguments from UNIX to DOS, with the provided set of
flags ('x' for no flags).