rustutils_pwd/
lib.rs

1use clap::Parser;
2use rustutils_runnable::Runnable;
3use std::error::Error;
4
5/// Print the full path of the current working directory.
6#[derive(Parser, Clone, Debug)]
7#[clap(author, version, about, long_about = None)]
8pub struct Pwd {
9    /// Print the path, even if it contains symbolic links.
10    #[clap(short = 'L', long)]
11    logical: bool,
12    /// Resolve the path to the absolute path, without any symbolic links.
13    #[clap(short = 'P', long)]
14    physical: bool,
15}
16
17impl Runnable for Pwd {
18    fn run(&self) -> Result<(), Box<dyn Error>> {
19        // normalize by default
20        let normalize = match (self.logical, self.physical) {
21            (true, false) => false,
22            _ => true,
23        };
24        let dir = std::env::current_dir()?;
25        let dir = if normalize { dir.canonicalize()? } else { dir };
26        println!("{}", dir.display());
27        Ok(())
28    }
29}