#![cfg_attr(not(windows), allow(dead_code, unused_imports))]
use argh::FromArgs;
use std::fmt::Display;
use std::path::PathBuf;
#[derive(FromArgs)]
#[argh(
help_triggers("-h", "--help"),
note = "If more than one option is given then the input paths are output in\n\
all formats specified, in the order long, short, Unix, Windows.\n\
If no option is given the default is Unix format."
)]
struct Cli {
#[argh(switch, short = 'u')]
unix: bool,
#[argh(switch, short = 'w')]
windows: bool,
#[argh(switch, short = 'l')]
long: bool,
#[argh(switch, short = 's')]
short: bool,
#[argh(switch, short = '0')]
nul: bool,
#[argh(positional)]
paths: Vec<PathBuf>,
}
impl Cli {
fn print_path(&self, path: impl Display) {
if self.nul {
print!("{}\0", path);
} else {
println!("{}", path);
}
}
}
#[cfg(windows)]
fn main() {
use winer::path::{self, PathExt, UnixPath};
let mut cli: Cli = argh::from_env();
if !cli.unix && !cli.windows && !cli.short && !cli.long {
cli.unix = true;
}
for path in &cli.paths {
if cli.long {
cli.print_path(path.to_long_path().unwrap_or_default().display());
}
if cli.short {
cli.print_path(path.to_short_path().unwrap_or_default().display());
}
if cli.unix {
cli.print_path(path::dos2unix(path).unwrap_or_default().display());
}
if cli.windows {
let unix = UnixPath::new(path.as_os_str().as_encoded_bytes());
let converted = path::unix2dos(unix).unwrap_or_default();
cli.print_path(converted.display());
}
}
}
#[cfg(not(windows))]
fn main() {
eprintln!("This example only runs on Wine / Windows.")
}