winer 0.1.5

Inspect Wine runtimes, host OS details, and hosted processes
Documentation
//! A replicate of the official `winepath` utility
#![cfg_attr(not(windows), allow(dead_code, unused_imports))]

use argh::FromArgs;
use std::fmt::Display;
use std::path::PathBuf;

/// Convert PATH(s) to Unix or Windows long or short paths.
#[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 {
    /// converts a Windows path to a Unix path
    #[argh(switch, short = 'u')]
    unix: bool,

    /// converts a Unix path to a long Windows path
    #[argh(switch, short = 'w')]
    windows: bool,

    /// converts the short Windows path of an existing file or directory to the long format
    #[argh(switch, short = 'l')]
    long: bool,

    /// converts the long Windows path of an existing file or directory to the short format
    #[argh(switch, short = 's')]
    short: bool,

    /// separate output with \0 character, instead of a newline
    #[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();

    // default: convert to Unix path
    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.")
}