File: basename.rs

package info (click to toggle)
rust-jargon-args 0.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use jargon_args::Jargon;

// Not required, but a helper struct to contain arguments and their data.
struct Args {
    multiple: bool,
    suffix: Option<String>,
    zero: bool,
    names: Vec<String>,
}

fn main() {
    let mut j: Jargon = Jargon::from_env(); // Get an instance of Jargon using `std::env::args()`

    if j.contains(["-h", "--help"]) { // check for help argument
        print!("{}", HELP);
        return;
    }

    if j.contains(["-v", "--version"]) { // check for version argument
        println!(
            "basename example for Jargon crate {}",
            env!("CARGO_PKG_VERSION")
        );
        return;
    }

    let args = Args { // fill helper struct
        multiple: j.contains(["-a", "--multiple"]), // multiple
        suffix: j.option_arg(["-s", "--suffix"]), // suffix to remove
        zero: j.contains(["-z", "--zero"]), // terminate lines with null
        names: j.finish(), // get names
    };

    if args.names.is_empty() { // check if there are names
        println!("Missing NAMES");
        return;
    }

    let mut v: Vec<String> = vec![print(&args, &args.names[0])]; // initiate vector of names

    if args.multiple { // fill the rest if `-a` or `--multiple` was passed
        args.names.iter().skip(1).for_each(|name| v.append(&mut vec![print(&args, name)]));
    }

    if args.zero { // terminate with null if `-z` or `--zero` was passed
        v.iter().for_each(|name| print!("{}", name));
    } else { // terminate each name with space or new line
        v.iter().for_each(|name| println!("{} ", name));
    }
}

// extract basename and remove suffix
fn print(args: &Args, name: &String) -> String {
    if let Some(name) = name.split('/').last() {
        if let Some(suffix) = &args.suffix {
            if name.ends_with(suffix) {
                if let Some(n) = name.strip_suffix(suffix) {
                    return n.to_string();
                }
            }
        }
        return name.to_string();
    }
    name.to_string()
}

const HELP: &str = "Usage: basename NAME [SUFFIX]
  or:  basename OPTION... NAME...
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.

  -a, --multiple       support multiple arguments and treat each as a NAME
  -s, --suffix SUFFIX  remove a trailing SUFFIX; implies -a
  -z, --zero           end each output line with NUL, not newline
  -h, --help     display this help and exit
  -v, --version  output version information and exit
";