fixed a crash and made paths canonicalize

This commit is contained in:
Valerie Wolfe 2024-06-11 09:32:57 -04:00
parent 6ff3b992bc
commit b9cb18ea0b
4 changed files with 27 additions and 14 deletions

View file

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

View file

@ -8,4 +8,9 @@ pub fn missing_target() {
exit(1); exit(1);
} }
/// failed to canonicalize target path; code 2
pub fn canonicalize_fail(target: String) {
println!("mkwin: failed to canonicalze '{target}'--does the file exist?");
exit(2);
}

View file

@ -1,6 +1,6 @@
//! constants for flag arguments //! constants for flag arguments
pub const HELP: [&str;2] = ["-h", "--help"]; pub const HELP: [&str;2] = ["-h", "--help"];
pub const PATH_CONVERT: [&str;2] = [ "--pc", "--path-convert" ]; pub const PATH_CONVERT: &str = "--pc";
pub const QUIET: [&str;2] = ["-q", "--quiet"]; pub const QUIET: [&str;2] = ["-q", "--quiet"];

View file

@ -1,3 +1,4 @@
use std::path::Path;
use pico_args::Arguments; use pico_args::Arguments;
@ -41,12 +42,19 @@ pub fn main() {
let quiet = args.contains(flag::QUIET); let quiet = args.contains(flag::QUIET);
// get target executable // get target executable
let i_target = args.subcommand().unwrap(); let target: String;
if i_target.is_none() { if let Ok(Some(arg)) = args.subcommand() {
error::missing_target(); let path = Path::new(&arg);
if let Ok(path) = path.canonicalize() {
target = path.to_string_lossy().into();
} else {
error::canonicalize_fail(arg);
return;
}
} else {
error::missing_target();
return;
} }
let target = i_target.unwrap();
// construct output // 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}' ");
@ -73,13 +81,13 @@ args:
<target> The target program the resulting script will run. <target> The target program the resulting script will run.
flags: flags:
-h, --help Shows this help text. -h, --help Shows this help text and exit.
--pc=<flags>, The resulting script will use the 'path-convert'
--path-convert=<flags> to convert arguments from UNIX to DOS, with the
provided set of flags ('x' for no flags).
-q, --quiet The resulting script will run the target program --pc=<flags> The resulting script will use the 'path-convert' to convert
in the background and with its outputs redirected arguments from UNIX to DOS, with the provided set of flags
to '/dev/null'.", ('x' for no flags).
-q, --quiet The resulting script will run the target program in the
background and with its outputs redirected to '/dev/null'.",
env!("CARGO_PKG_VERSION"));} env!("CARGO_PKG_VERSION"));}