| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | import cli |
| 5 | |
| 6 | __global ( |
| 7 | if_subcommands_parse_args_called bool |
| 8 | flag_should_be_set_called bool |
| 9 | assert_flags_called bool |
| 10 | flag_is_set_in_subcommand_called bool |
| 11 | ) |
| 12 | |
| 13 | fn test_if_command_parses_empty_args() { |
| 14 | mut cmd := cli.Command{ |
| 15 | name: 'command' |
| 16 | execute: empty_func |
| 17 | } |
| 18 | cmd.parse(['command']) |
| 19 | assert cmd.name == 'command' && cmd.args == [] |
| 20 | } |
| 21 | |
| 22 | fn test_if_command_parses_args() { |
| 23 | mut cmd := cli.Command{ |
| 24 | name: 'command' |
| 25 | execute: empty_func |
| 26 | } |
| 27 | cmd.parse(['command', 'arg0', 'arg1']) |
| 28 | assert cmd.name == 'command' && cmd.args == ['arg0', 'arg1'] |
| 29 | } |
| 30 | |
| 31 | fn test_if_subcommands_parse_args() { |
| 32 | mut cmd := cli.Command{ |
| 33 | name: 'command' |
| 34 | } |
| 35 | if_subcommands_parse_args_called = false |
| 36 | subcmd := cli.Command{ |
| 37 | name: 'subcommand' |
| 38 | execute: if_subcommands_parse_args_func |
| 39 | } |
| 40 | cmd.add_command(subcmd) |
| 41 | cmd.parse(['command', 'subcommand', 'arg0', 'arg1']) |
| 42 | assert if_subcommands_parse_args_called |
| 43 | } |
| 44 | |
| 45 | fn test_if_subcommand_alias_parses_args() { |
| 46 | mut cmd := cli.Command{ |
| 47 | name: 'command' |
| 48 | } |
| 49 | subcmd := cli.Command{ |
| 50 | name: 'subcommand' |
| 51 | alias: 'sc' |
| 52 | execute: if_subcommands_parse_args_func |
| 53 | } |
| 54 | cmd.add_command(subcmd) |
| 55 | cmd.parse(['command', 'sc', 'arg0', 'arg1']) |
| 56 | } |
| 57 | |
| 58 | fn if_subcommands_parse_args_func(cmd cli.Command) ! { |
| 59 | if_subcommands_parse_args_called = true |
| 60 | assert cmd.name == 'subcommand' && cmd.args == ['arg0', 'arg1'] |
| 61 | } |
| 62 | |
| 63 | fn test_default_subcommands() { |
| 64 | mut cmd := cli.Command{ |
| 65 | name: 'command' |
| 66 | } |
| 67 | cmd.parse(['command']) |
| 68 | assert cmd.commands.any(it.name == 'help') |
| 69 | assert cmd.commands.any(it.name == 'man') |
| 70 | |
| 71 | cmd = cli.Command{ |
| 72 | name: 'command' |
| 73 | version: '1.0.0' |
| 74 | } |
| 75 | cmd.parse(['command']) |
| 76 | assert cmd.commands.any(it.name == 'version') |
| 77 | } |
| 78 | |
| 79 | fn flag_should_be_set(cmd cli.Command) ! { |
| 80 | flag_should_be_set_called = true |
| 81 | flag := cmd.flags.get_string('flag')! |
| 82 | assert flag == 'value' |
| 83 | } |
| 84 | |
| 85 | fn test_if_flag_gets_set() { |
| 86 | flag_should_be_set_called = false |
| 87 | mut cmd := cli.Command{ |
| 88 | name: 'command' |
| 89 | execute: flag_should_be_set |
| 90 | } |
| 91 | cmd.add_flag(cli.Flag{ |
| 92 | flag: .string |
| 93 | name: 'flag' |
| 94 | }) |
| 95 | cmd.parse(['command', '-flag', 'value']) |
| 96 | assert flag_should_be_set_called |
| 97 | } |
| 98 | |
| 99 | fn test_if_flag_gets_set_with_abbrev() { |
| 100 | flag_should_be_set_called = false |
| 101 | mut cmd := cli.Command{ |
| 102 | name: 'command' |
| 103 | execute: flag_should_be_set |
| 104 | } |
| 105 | cmd.add_flag(cli.Flag{ |
| 106 | flag: .string |
| 107 | name: 'flag' |
| 108 | abbrev: 'f' |
| 109 | }) |
| 110 | cmd.parse(['command', '-f', 'value']) |
| 111 | assert flag_should_be_set_called |
| 112 | } |
| 113 | |
| 114 | fn test_if_flag_gets_set_with_long_arg() { |
| 115 | flag_should_be_set_called = false |
| 116 | mut cmd := cli.Command{ |
| 117 | name: 'command' |
| 118 | execute: flag_should_be_set |
| 119 | posix_mode: true |
| 120 | } |
| 121 | cmd.add_flag(cli.Flag{ |
| 122 | flag: .string |
| 123 | name: 'flag' |
| 124 | abbrev: 'f' |
| 125 | }) |
| 126 | cmd.parse(['command', '--flag', 'value']) |
| 127 | assert flag_should_be_set_called |
| 128 | } |
| 129 | |
| 130 | fn assert_flags(cmd cli.Command) ! { |
| 131 | assert_flags_called = true |
| 132 | flag := cmd.flags.get_string('flag')! |
| 133 | assert flag == 'value' |
| 134 | value := cmd.flags.get_int('value')! |
| 135 | assert value == 42 |
| 136 | flag2 := cmd.flags.get_string('flag-2')! |
| 137 | assert flag2 == 'value-2' |
| 138 | } |
| 139 | |
| 140 | fn test_if_multiple_flags_get_set() { |
| 141 | assert_flags_called = false |
| 142 | mut cmd := cli.Command{ |
| 143 | name: 'command' |
| 144 | execute: assert_flags |
| 145 | } |
| 146 | cmd.add_flag(cli.Flag{ |
| 147 | flag: .string |
| 148 | name: 'flag' |
| 149 | }) |
| 150 | cmd.add_flag(cli.Flag{ |
| 151 | flag: .string |
| 152 | name: 'flag-2' |
| 153 | }) |
| 154 | cmd.add_flag(cli.Flag{ |
| 155 | flag: .int |
| 156 | name: 'value' |
| 157 | }) |
| 158 | cmd.parse(['command', '-flag=value', '-value', '42', '-flag-2', 'value-2']) |
| 159 | assert assert_flags_called |
| 160 | } |
| 161 | |
| 162 | fn test_if_required_flags_get_set() { |
| 163 | assert_flags_called = false |
| 164 | mut cmd := cli.Command{ |
| 165 | name: 'command' |
| 166 | execute: assert_flags |
| 167 | } |
| 168 | cmd.add_flag(cli.Flag{ |
| 169 | flag: .string |
| 170 | name: 'flag' |
| 171 | }) |
| 172 | cmd.add_flag(cli.Flag{ |
| 173 | flag: .string |
| 174 | name: 'flag-2' |
| 175 | }) |
| 176 | cmd.add_flag(cli.Flag{ |
| 177 | flag: .int |
| 178 | name: 'value' |
| 179 | required: true |
| 180 | }) |
| 181 | cmd.parse(['command', '-flag', 'value', '-value', '42', '-flag-2', 'value-2']) |
| 182 | assert assert_flags_called |
| 183 | } |
| 184 | |
| 185 | fn flag_is_set_in_subcommand(cmd cli.Command) ! { |
| 186 | flag_is_set_in_subcommand_called = true |
| 187 | flag := cmd.flags.get_string('flag') or { panic(err) } |
| 188 | assert flag == 'value' |
| 189 | } |
| 190 | |
| 191 | fn test_if_flag_gets_set_in_subcommand() { |
| 192 | mut cmd := cli.Command{ |
| 193 | name: 'command' |
| 194 | execute: empty_func |
| 195 | } |
| 196 | flag_is_set_in_subcommand_called = false |
| 197 | mut subcmd := cli.Command{ |
| 198 | name: 'subcommand' |
| 199 | execute: flag_is_set_in_subcommand |
| 200 | } |
| 201 | subcmd.add_flag(cli.Flag{ |
| 202 | flag: .string |
| 203 | name: 'flag' |
| 204 | }) |
| 205 | cmd.add_command(subcmd) |
| 206 | cmd.parse(['command', 'subcommand', '-flag', 'value']) |
| 207 | assert flag_is_set_in_subcommand_called |
| 208 | } |
| 209 | |
| 210 | fn test_if_global_flag_gets_set_in_subcommand() { |
| 211 | mut cmd := cli.Command{ |
| 212 | name: 'command' |
| 213 | execute: empty_func |
| 214 | } |
| 215 | cmd.add_flag(cli.Flag{ |
| 216 | flag: .string |
| 217 | name: 'flag' |
| 218 | global: true |
| 219 | }) |
| 220 | flag_is_set_in_subcommand_called = false |
| 221 | subcmd := cli.Command{ |
| 222 | name: 'subcommand' |
| 223 | execute: flag_is_set_in_subcommand |
| 224 | } |
| 225 | cmd.add_command(subcmd) |
| 226 | cmd.parse(['command', '-flag', 'value', 'subcommand']) |
| 227 | assert flag_is_set_in_subcommand_called |
| 228 | } |
| 229 | |
| 230 | fn test_command_setup() { |
| 231 | mut cmd := cli.Command{ |
| 232 | name: 'root' |
| 233 | commands: [ |
| 234 | cli.Command{ |
| 235 | name: 'child' |
| 236 | commands: [ |
| 237 | cli.Command{ |
| 238 | name: 'child-child' |
| 239 | }, |
| 240 | ] |
| 241 | }, |
| 242 | ] |
| 243 | } |
| 244 | assert isnil(cmd.commands[0].parent) |
| 245 | assert isnil(cmd.commands[0].commands[0].parent) |
| 246 | cmd.setup() |
| 247 | assert cmd.commands[0].parent.name == 'root' |
| 248 | assert cmd.commands[0].commands[0].parent.name == 'child' |
| 249 | } |
| 250 | |
| 251 | fn test_help_message_lists_command_aliases() { |
| 252 | cmd := cli.Command{ |
| 253 | name: 'command' |
| 254 | description: 'description' |
| 255 | commands: [ |
| 256 | cli.Command{ |
| 257 | name: 'sub' |
| 258 | alias: 's' |
| 259 | description: 'subcommand' |
| 260 | }, |
| 261 | ] |
| 262 | } |
| 263 | assert cmd.help_message() == r'Usage: command [commands] |
| 264 | |
| 265 | description |
| 266 | |
| 267 | Commands: |
| 268 | sub (s) subcommand |
| 269 | ' |
| 270 | } |
| 271 | |
| 272 | fn test_manpage_lists_command_aliases() { |
| 273 | mut cmd := cli.Command{ |
| 274 | name: 'command' |
| 275 | description: 'description' |
| 276 | commands: [ |
| 277 | cli.Command{ |
| 278 | name: 'sub' |
| 279 | alias: 's' |
| 280 | description: 'subcommand' |
| 281 | }, |
| 282 | ] |
| 283 | } |
| 284 | cmd.setup() |
| 285 | assert cmd.manpage().after_char(`\n`) == r'.Dt COMMAND 1 |
| 286 | .Os |
| 287 | .Sh NAME |
| 288 | .Nm command |
| 289 | .Nd description |
| 290 | .Sh SYNOPSIS |
| 291 | .Nm command |
| 292 | .Nm command |
| 293 | .Ar subcommand |
| 294 | .Sh DESCRIPTION |
| 295 | description |
| 296 | .Pp |
| 297 | The subcommands are as follows: |
| 298 | .Bl -tag -width indent |
| 299 | .It Cm sub Pq Cm s |
| 300 | subcommand |
| 301 | .El |
| 302 | .Sh SEE ALSO |
| 303 | .Xr command-sub 1 |
| 304 | ' |
| 305 | } |
| 306 | |
| 307 | // helper functions |
| 308 | fn empty_func(cmd cli.Command) ! { |
| 309 | } |
| 310 | |