getopt2 0.1.0

Zero dependency strict command line argument parser
Documentation
/*
 * Copyright (c) Radim Kolar 2013, 2018, 2023.
 * SPDX-License-Identifier: MIT
 *
 * getopt2 library is licensed under MIT license:
 *   https://spdx.org/licenses/MIT.html
*/

/*  Tests for double dash -- POSIX standard */

use crate::new;

/** converts iterable &str into String Vec */
fn toStringVec<'a>(iter: impl IntoIterator<Item = &'a str>) -> Vec<String> {
    iter.into_iter().map(|s| s.to_string()).collect()
}


// tests that double dash is eaten

#[test]
fn gnu_eat_dash_at_start() {
   let g = new( ["--", "one","two"], "a");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two"]) );
   assert_eq! ( g.options.len(), 0);
}

#[test]
fn gnu_eat_dash_in_middle() {
   let g = new( [ "one","--","two" ], "a");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two"]) );
   assert_eq! ( g.options.len(), 0);
}

#[test]
fn gnu_eat_dash_at_end() {
   let g = new( ["one","two","--"], "a");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two"]) );
   assert_eq! ( g.options.len(), 0);
}

// tests that double dash is eaten after option

#[test]
fn gnu_eat_dash_following_option_without_argument() {
   let g = new( ["one","two","-c","--","end"], "c");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","end"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"".to_owned()) );
   assert_eq! ( g.options.len(), 1 );
}

#[test]
fn gnu_eat_dash_following_option_without_argument_at_end() {
   let g = new( ["one","two","-c","--"], "cd");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"".to_owned()) );
   assert_eq! ( g.options.len(), 1 );
}

#[test]
fn gnu_eat_dash_following_option_with_argument() {
   let g = new( ["one","two","-c","arg", "--","end"], "c:d");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","end"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"arg".to_owned()) );
   assert_eq! ( g.options.len(), 1 );
}

#[test]
fn gnu_eat_dash_following_option_with_argument_at_end() {
   let g = new( ["one","two","-c","arg", "--"], "c:d");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"arg".to_owned()) );
   assert_eq! ( g.options.len(), 1 );
}

// test that option is not parsed after double dash

#[test]
fn gnu_option_notparsed_after_option_without_argument_and_dash_in_middle() {
   let g = new( ["one","two","-b", "--","-a"], "ab");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","-a"]) );
   assert_eq! ( g.options.len(), 1);
   assert_eq! ( g.options.get(&'b'), Some(&"".to_string()) );
}

#[test]
fn gnu_option_notparsed_after_option_with_argument_and_dash_in_middle() {
   let g = new( ["one","two","-b", "agz", "--","-a"], "ab:");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","-a"]) );
   assert_eq! ( g.options.len(), 1);
   assert_eq! ( g.options.get(&'b'), Some(&"agz".to_string()) );
}

#[test]
fn gnu_option_notparsed_after_dash_at_start() {
   let g = new( ["--","-a","2"], "a:");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["-a", "2"]) );
   assert_eq! ( g.options.len(), 0);
}

// special cases

#[test]
fn gnu_dash_following_option_with_argument_is_not_a_separator() {
   let g = new( ["one","two","-c","--","-b", "end"], "c:b");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","end"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"--".to_string()) );
   assert_eq! ( g.options.get(&'b'), Some(&"".to_string()) );
   assert_eq! ( g.options.len(), 2 );
}

#[test]
fn gnu_dash_following_option_with_optional_argument_is_a_separator() {
   let g = new( ["one","two","-c","--","-b", "end"], "c::b");
   assert! ( g.is_ok() );
   let g = g.unwrap();
   assert_eq! ( g.arguments, toStringVec(["one","two","-b","end"]) );
   assert_eq! ( g.options.get(&'c'), Some(&"".to_string()) );
   assert_eq! ( g.options.get(&'b'), None );
   assert_eq! ( g.options.len(), 1 );
}