| 1 | // Test .long (GNU) parse style |
| 2 | import flag |
| 3 | |
| 4 | const exe_and_gnu_args = ['/path/to/exe', '--f=10.2', '--mix', '--f2=2', '--test=test', '--amount=5', |
| 5 | '--version=1.2.3'] |
| 6 | const exe_and_gnu_args_with_tail = ['/path/to/exe', '--f2=2', '--f=10.2', '--mix', '--test=test', |
| 7 | '--amount=6', '--version=1.2.3', '/path/to/x', '/path/to/y', '/path/to/z'] |
| 8 | |
| 9 | struct Config { |
| 10 | f f32 |
| 11 | f2 f64 |
| 12 | mix bool |
| 13 | some_test string = 'abc' @[long: test] |
| 14 | path string @[tail] |
| 15 | amount int = 1 |
| 16 | version_str string @[long: version] |
| 17 | } |
| 18 | |
| 19 | fn test_pure_gnu_long() { |
| 20 | config, _ := flag.to_struct[Config](exe_and_gnu_args, skip: 1, style: .long)! |
| 21 | assert config.f == 10.2 |
| 22 | assert config.f2 == 2.0 |
| 23 | assert config.mix == true |
| 24 | assert config.some_test == 'test' |
| 25 | assert config.path == '' |
| 26 | assert config.amount == 5 |
| 27 | assert config.version_str == '1.2.3' |
| 28 | } |
| 29 | |
| 30 | fn test_pure_gnu_long_no_exe() { |
| 31 | config, _ := flag.to_struct[Config](exe_and_gnu_args[1..], style: .long)! |
| 32 | assert config.f == 10.2 |
| 33 | assert config.f2 == 2.0 |
| 34 | assert config.mix == true |
| 35 | assert config.some_test == 'test' |
| 36 | assert config.path == '' |
| 37 | assert config.amount == 5 |
| 38 | assert config.version_str == '1.2.3' |
| 39 | } |
| 40 | |
| 41 | fn test_pure_gnu_long_with_tail() { |
| 42 | config, no_matches := flag.to_struct[Config](exe_and_gnu_args_with_tail, skip: 1, style: .long)! |
| 43 | assert config.path == '/path/to/x' // path is of type `string`, not `[]string` |
| 44 | assert no_matches[0] == '/path/to/y' |
| 45 | assert no_matches[1] == '/path/to/z' |
| 46 | |
| 47 | assert config.amount == 6 |
| 48 | } |
| 49 | |
| 50 | fn test_pure_gnu_long_with_tail_no_exe() { |
| 51 | a := exe_and_gnu_args_with_tail[1..] |
| 52 | config, no_matches := flag.to_struct[Config](a, style: .long)! |
| 53 | assert config.path == '/path/to/x' |
| 54 | assert no_matches[0] == '/path/to/y' |
| 55 | assert no_matches[1] == '/path/to/z' |
| 56 | |
| 57 | assert config.amount == 6 |
| 58 | } |
| 59 | |