| 1 | use jargon_argsrust-analyzer cargo jargon-args 0.2.5 crate/::Jargonrust-analyzer cargo jargon-args 0.2.5 jargons/Jargon#; |
| 2 | |
| 3 | // Not required, but a helper struct to contain arguments and their data. |
| 4 | struct Argsrust-analyzer cargo jargon-args 0.2.5 Args# { |
| 5 | multiplerust-analyzer cargo jargon-args 0.2.5 Args#multiple.: bool, |
| 6 | suffixrust-analyzer cargo jargon-args 0.2.5 Args#suffix.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 7 | zerorust-analyzer cargo jargon-args 0.2.5 Args#zero.: bool, |
| 8 | namesrust-analyzer cargo jargon-args 0.2.5 Args#names.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 9 | } |
| 10 | |
| 11 | fn mainrust-analyzer cargo jargon-args 0.2.5 main().() { |
| 12 | let mut j: Jargonrust-analyzer cargo jargon-args 0.2.5 jargons/Jargon# = Jargonrust-analyzer cargo jargon-args 0.2.5 jargons/Jargon#::from_envrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]from_env().(); // Get an instance of Jargon using `std::env::args()` |
| 13 | |
| 14 | if j.containsrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]contains().(["-h", "--help"]) { // check for help argument |
| 15 | printrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/print!!("{}", HELPrust-analyzer cargo jargon-args 0.2.5 HELP.); |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | if j.containsrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]contains().(["-v", "--version"]) { // check for version argument |
| 20 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 21 | "basename example for Jargon crate {}", |
| 22 | envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_PKG_VERSION") |
| 23 | ); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | let args = Argsrust-analyzer cargo jargon-args 0.2.5 Args# { // fill helper struct |
| 28 | multiplerust-analyzer cargo jargon-args 0.2.5 Args#multiple.: j.containsrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]contains().(["-a", "--multiple"]), // multiple |
| 29 | suffixrust-analyzer cargo jargon-args 0.2.5 Args#suffix.: j.option_argrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]option_arg().(["-s", "--suffix"]), // suffix to remove |
| 30 | zerorust-analyzer cargo jargon-args 0.2.5 Args#zero.: j.containsrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]contains().(["-z", "--zero"]), // terminate lines with null |
| 31 | namesrust-analyzer cargo jargon-args 0.2.5 Args#names.: j.finishrust-analyzer cargo jargon-args 0.2.5 jargons/impl#[Jargon]finish().(), // get names |
| 32 | }; |
| 33 | |
| 34 | if args.namesrust-analyzer cargo jargon-args 0.2.5 Args#names..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { // check if there are names |
| 35 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Missing NAMES"); |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | let mut v: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![printrust-analyzer cargo jargon-args 0.2.5 print().(&args, &args.namesrust-analyzer cargo jargon-args 0.2.5 Args#names.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().)]; // initiate vector of names |
| 40 | |
| 41 | if args.multiplerust-analyzer cargo jargon-args 0.2.5 Args#multiple. { // fill the rest if `-a` or `--multiple` was passed |
| 42 | args.namesrust-analyzer cargo jargon-args 0.2.5 Args#names..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(1).for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|name| v.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![printrust-analyzer cargo jargon-args 0.2.5 print().(&args, name)])); |
| 43 | } |
| 44 | |
| 45 | if args.zerorust-analyzer cargo jargon-args 0.2.5 Args#zero. { // terminate with null if `-z` or `--zero` was passed |
| 46 | v.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]for_each().(|name| printrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/print!!("{}", name)); |
| 47 | } else { // terminate each name with space or new line |
| 48 | v.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]for_each().(|name| printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{} ", name)); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // extract basename and remove suffix |
| 53 | fn printrust-analyzer cargo jargon-args 0.2.5 print().(args: &Argsrust-analyzer cargo jargon-args 0.2.5 Args#, name: &Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 54 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(name) = name.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('/').lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#last().() { |
| 55 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(suffix) = &args.suffixrust-analyzer cargo jargon-args 0.2.5 Args#suffix. { |
| 56 | if name.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(suffix) { |
| 57 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(n) = name.strip_suffixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_suffix().(suffix) { |
| 58 | return n.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return name.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 63 | } |
| 64 | name.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().() |
| 65 | } |
| 66 | |
| 67 | const HELPrust-analyzer cargo jargon-args 0.2.5 HELP.: &str = "Usage: basename NAME [SUFFIX] |
| 68 | or: basename OPTION... NAME... |
| 69 | Print NAME with any leading directory components removed. |
| 70 | If specified, also remove a trailing SUFFIX. |
| 71 | |
| 72 | -a, --multiple support multiple arguments and treat each as a NAME |
| 73 | -s, --suffix SUFFIX remove a trailing SUFFIX; implies -a |
| 74 | -z, --zero end each output line with NUL, not newline |
| 75 | -h, --help display this help and exit |
| 76 | -v, --version output version information and exit |
| 77 | "; |