| 1 | module cmdline |
| 2 | |
| 3 | // Fetch multiple option by param, e.g. |
| 4 | // args: ['v', '-d', 'aa', '-d', 'bb', '-d', 'cc'] |
| 5 | // param: '-d' |
| 6 | // ret: ['aa', 'bb', 'cc'] |
| 7 | pub fn options(args []string, param string) []string { |
| 8 | mut flags := []string{} |
| 9 | for i, v in args { |
| 10 | if v == param { |
| 11 | if i + 1 < args.len { |
| 12 | flags << args[i + 1] |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | return flags |
| 17 | } |
| 18 | |
| 19 | // Fetch option by param, e.g. |
| 20 | // args: ['v', '-d', 'aa'] |
| 21 | // param: '-d' |
| 22 | // def: '' |
| 23 | // ret: 'aa' |
| 24 | pub fn option(args []string, param string, def string) string { |
| 25 | mut found := false |
| 26 | for arg in args { |
| 27 | if found { |
| 28 | return arg |
| 29 | } else if param == arg { |
| 30 | found = true |
| 31 | } |
| 32 | } |
| 33 | return def |
| 34 | } |
| 35 | |
| 36 | // Fetch all options before what params, e.g. |
| 37 | // args: ['-stat', 'test', 'aaa.v'] |
| 38 | // what: ['test'] |
| 39 | // ret: ['-stat'] |
| 40 | pub fn options_before(args []string, what []string) []string { |
| 41 | mut args_before := []string{} |
| 42 | for a in args { |
| 43 | if a in what { |
| 44 | break |
| 45 | } |
| 46 | args_before << a |
| 47 | } |
| 48 | return args_before |
| 49 | } |
| 50 | |
| 51 | // Fetch all options after what params, e.g. |
| 52 | // args: ['-stat', 'test', 'aaa.v'] |
| 53 | // what: ['test'] |
| 54 | // ret: ['aaa.v'] |
| 55 | pub fn options_after(args []string, what []string) []string { |
| 56 | mut found := false |
| 57 | mut args_after := []string{} |
| 58 | for a in args { |
| 59 | if a in what { |
| 60 | found = true |
| 61 | continue |
| 62 | } |
| 63 | if found { |
| 64 | args_after << a |
| 65 | } |
| 66 | } |
| 67 | return args_after |
| 68 | } |
| 69 | |
| 70 | // Fetch all options not start with '-', e.g. |
| 71 | // args: ['-d', 'aa', '--help', 'bb'] |
| 72 | // ret: ['aa', 'bb'] |
| 73 | pub fn only_non_options(args []string) []string { |
| 74 | return args.filter(!it.starts_with('-')) |
| 75 | } |
| 76 | |
| 77 | // Fetch all options start with '-', e.g. |
| 78 | // args: ['-d', 'aa', '--help', 'bb'] |
| 79 | // ret: ['-d', '--help'] |
| 80 | pub fn only_options(args []string) []string { |
| 81 | return args.filter(it.starts_with('-')) |
| 82 | } |
| 83 | |