2019-11-21 13:03:12 +01:00
|
|
|
import cli
|
|
|
|
|
|
|
|
fn test_if_command_parses_empty_args() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
|
|
|
execute: empty_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.parse(['command'])
|
2020-07-18 13:24:10 +02:00
|
|
|
assert cmd.name == 'command' && compare_arrays(cmd.args, [])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_command_parses_args() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
|
|
|
execute: empty_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.parse(['command', 'arg0', 'arg1'])
|
2020-07-18 13:24:10 +02:00
|
|
|
assert cmd.name == 'command' && compare_arrays(cmd.args, ['arg0', 'arg1'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_subcommands_parse_args() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
subcmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'subcommand'
|
2020-12-31 09:16:05 +01:00
|
|
|
execute: if_subcommands_parse_args_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_command(subcmd)
|
|
|
|
cmd.parse(['command', 'subcommand', 'arg0', 'arg1'])
|
|
|
|
}
|
|
|
|
|
2020-12-31 09:16:05 +01:00
|
|
|
fn if_subcommands_parse_args_func(cmd cli.Command) ? {
|
2020-07-18 13:24:10 +02:00
|
|
|
assert cmd.name == 'subcommand' && compare_arrays(cmd.args, ['arg0', 'arg1'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_command_has_default_help_subcommand() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.parse(['command'])
|
|
|
|
assert has_command(cmd, 'help')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_command_has_default_version_subcommand_if_version_is_set() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
|
|
|
version: '1.0.0'
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.parse(['command'])
|
|
|
|
assert has_command(cmd, 'version')
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:01:54 +02:00
|
|
|
fn flag_should_be_set(cmd cli.Command) ? {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := cmd.flags.get_string('flag') ?
|
2020-07-18 14:01:54 +02:00
|
|
|
assert flag == 'value'
|
|
|
|
}
|
2020-08-12 06:11:40 +02:00
|
|
|
|
2019-11-21 13:03:12 +01:00
|
|
|
fn test_if_flag_gets_set() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
2020-07-18 14:01:54 +02:00
|
|
|
execute: flag_should_be_set
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
})
|
2020-07-18 13:24:10 +02:00
|
|
|
cmd.parse(['command', '-flag', 'value'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_flag_gets_set_with_abbrev() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
2020-07-18 14:01:54 +02:00
|
|
|
execute: flag_should_be_set
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
2020-07-18 13:24:10 +02:00
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
abbrev: 'f'
|
2019-11-21 13:03:12 +01:00
|
|
|
})
|
|
|
|
cmd.parse(['command', '-f', 'value'])
|
2021-01-22 18:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_flag_gets_set_with_long_arg() {
|
|
|
|
mut cmd := cli.Command{
|
|
|
|
name: 'command'
|
|
|
|
execute: flag_should_be_set
|
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
abbrev: 'f'
|
|
|
|
})
|
2020-07-18 13:24:10 +02:00
|
|
|
cmd.parse(['command', '--flag', 'value'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-07-18 14:01:54 +02:00
|
|
|
fn flag_should_have_value_of_42(cmd cli.Command) ? {
|
2020-09-16 22:01:44 +02:00
|
|
|
flag := cmd.flags.get_string('flag') ?
|
2020-07-18 14:01:54 +02:00
|
|
|
assert flag == 'value'
|
2020-09-16 22:01:44 +02:00
|
|
|
value := cmd.flags.get_int('value') ?
|
2020-07-18 14:01:54 +02:00
|
|
|
assert value == 42
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:03:12 +01:00
|
|
|
fn test_if_multiple_flags_get_set() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
2020-07-18 14:01:54 +02:00
|
|
|
execute: flag_should_have_value_of_42
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
})
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .int
|
|
|
|
name: 'value'
|
|
|
|
})
|
2020-07-18 13:24:10 +02:00
|
|
|
cmd.parse(['command', '-flag', 'value', '-value', '42'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-02-19 11:43:18 +01:00
|
|
|
fn test_if_required_flags_get_set() {
|
|
|
|
mut cmd := cli.Command{
|
|
|
|
name: 'command'
|
|
|
|
execute: flag_should_have_value_of_42
|
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
})
|
|
|
|
cmd.add_flag(cli.Flag{
|
|
|
|
flag: .int
|
|
|
|
name: 'value'
|
|
|
|
required: true
|
|
|
|
})
|
|
|
|
cmd.parse(['command', '-flag', 'value', '-value', '42'])
|
|
|
|
}
|
|
|
|
|
2020-07-18 14:01:54 +02:00
|
|
|
fn flag_is_set_in_subcommand(cmd cli.Command) ? {
|
2021-03-01 00:18:14 +01:00
|
|
|
flag := cmd.flags.get_string('flag') or { panic(err) }
|
2020-07-18 14:01:54 +02:00
|
|
|
assert flag == 'value'
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:03:12 +01:00
|
|
|
fn test_if_flag_gets_set_in_subcommand() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
|
|
|
execute: empty_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
mut subcmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'subcommand'
|
2020-07-18 14:01:54 +02:00
|
|
|
execute: flag_is_set_in_subcommand
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
subcmd.add_flag(cli.Flag{
|
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
})
|
|
|
|
cmd.add_command(subcmd)
|
2020-07-18 13:24:10 +02:00
|
|
|
cmd.parse(['command', 'subcommand', '-flag', 'value'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_if_global_flag_gets_set_in_subcommand() {
|
|
|
|
mut cmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'command'
|
|
|
|
execute: empty_func
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_flag(cli.Flag{
|
2020-07-18 13:24:10 +02:00
|
|
|
flag: .string
|
|
|
|
name: 'flag'
|
|
|
|
global: true
|
2019-11-21 13:03:12 +01:00
|
|
|
})
|
|
|
|
subcmd := cli.Command{
|
2020-07-18 13:24:10 +02:00
|
|
|
name: 'subcommand'
|
2020-07-18 14:01:54 +02:00
|
|
|
execute: flag_is_set_in_subcommand
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
cmd.add_command(subcmd)
|
2020-07-18 13:24:10 +02:00
|
|
|
cmd.parse(['command', '-flag', 'value', 'subcommand'])
|
2019-11-21 13:03:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:25:25 +01:00
|
|
|
fn test_command_setup() {
|
|
|
|
mut cmd := cli.Command{
|
|
|
|
name: 'root'
|
|
|
|
commands: [
|
|
|
|
cli.Command{
|
|
|
|
name: 'child'
|
|
|
|
commands: [
|
|
|
|
cli.Command{
|
|
|
|
name: 'child-child'
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
assert isnil(cmd.commands[0].parent)
|
|
|
|
assert isnil(cmd.commands[0].commands[0].parent)
|
|
|
|
cmd.setup()
|
|
|
|
assert cmd.commands[0].parent.name == 'root'
|
|
|
|
assert cmd.commands[0].commands[0].parent.name == 'child'
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:03:12 +01:00
|
|
|
// helper functions
|
2020-07-18 13:24:10 +02:00
|
|
|
fn empty_func(cmd cli.Command) ? {
|
|
|
|
}
|
2019-11-21 13:03:12 +01:00
|
|
|
|
|
|
|
fn has_command(cmd cli.Command, name string) bool {
|
|
|
|
for subcmd in cmd.commands {
|
|
|
|
if subcmd.name == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:23:03 +02:00
|
|
|
fn compare_arrays(array0 []string, array1 []string) bool {
|
2019-11-21 13:03:12 +01:00
|
|
|
if array0.len != array1.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-18 13:24:10 +02:00
|
|
|
for i in 0 .. array0.len {
|
2019-11-21 13:03:12 +01:00
|
|
|
if array0[i] != array1[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|