diff --git a/README.md b/README.md index c0f2cbb..35f4e84 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ These are tools I use to use Windows primarily through WSL. A lot of them assume you have PowerShell somewhere in your `PATH` as "`pwsh`". +## `dos-var` + +Gets Windows environment variables from DOS. + ## `env-share` A hacked-together utility for sending WSL variables back to Windows diff --git a/dos-var/Cargo.toml b/dos-var/Cargo.toml index 5307eea..6744d1f 100644 --- a/dos-var/Cargo.toml +++ b/dos-var/Cargo.toml @@ -1,6 +1,15 @@ [package] name = "dos-var" -version = "0.0.1" +version = "0.0.2" edition = "2021" [dependencies] +pico-args = "0.5.0" + +[profile.release] +opt-level = 's' +codegen-units = 1 +debug = false +lto = true +panic = "abort" +strip = "symbols" diff --git a/dos-var/src/main.rs b/dos-var/src/main.rs index 9f4498d..4e74bd5 100644 --- a/dos-var/src/main.rs +++ b/dos-var/src/main.rs @@ -1,36 +1,39 @@ -use std::{ - env::args, - process::{ - Command, - Stdio - } -}; +use std::process::Command; + +use pico_args::Arguments; mod error; const CMD: &str = "/mnt/c/Windows/System32/cmd.exe"; +const FLAG_PATH: [&str; 2] = [ "-p", "--path" ]; + pub fn main() { - let args: Vec = args().collect(); - if args.len() != 2 { error::arg_count(); } - let target = &args[1]; + let mut args = Arguments::from_env(); - let cmd = Command::new(CMD) - .current_dir("/mnt/c/") - .arg( format!("/C echo %{target}%") ) - .output(); - if let Ok(output) = cmd { - if let Ok(stdout) = String::from_utf8(output.stdout) { - // trim output - let stdout = stdout.trim_end_matches("\"\r\n"); - // catch empty variable case - if stdout == format!("%{target}%") { error::not_found(&target); } + let convert_path = args.contains(FLAG_PATH); - // convert and write - let path = stdout - .replace("\\", "/") - .replace("C:/", "/mnt/c/"); - println!("{path}"); + if let Ok(Some(target)) = args.subcommand() { + let cmd = Command::new(CMD) + .current_dir("/mnt/c/") + .arg( format!("/C echo %{target}%") ) + .output(); + if let Ok(output) = cmd { + if let Ok(stdout) = String::from_utf8(output.stdout) { + // trim output + let stdout = stdout.trim_end_matches("\"\r\n"); + // catch empty variable case + if stdout == format!("%{target}%") { error::not_found(&target); } + + // handle path flag and write + let path = + if convert_path { + stdout + .replace("\\", "/") + .replace("C:/", "/mnt/c/") + } else { stdout.to_owned() }; + println!("{path}"); + } } } }