| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/command_line_parser.h> |
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
|
| 9 |
static void s_reset_static_state(void) { |
| 10 |
aws_cli_optind = 1; |
| 11 |
} |
| 12 |
|
| 13 |
static int s_test_short_argument_parse_fn(struct aws_allocator *allocator, void *ctx) { |
| 14 |
(void)allocator; |
| 15 |
(void)ctx; |
| 16 |
struct aws_cli_option options[] = { |
| 17 |
{.name = NULL, .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'}, |
| 18 |
{.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'}, |
| 19 |
{.name = NULL, .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'}, |
| 20 |
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}, |
| 21 |
}; |
| 22 |
|
| 23 |
char *const args[] = { |
| 24 |
"prog-name", |
| 25 |
"-a", |
| 26 |
"-b", |
| 27 |
"bval", |
| 28 |
"-c", |
| 29 |
}; |
| 30 |
int argc = 5; |
| 31 |
int longindex = 0; |
| 32 |
s_reset_static_state(); |
| 33 |
int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 34 |
ASSERT_INT_EQUALS('a', arg); |
| 35 |
ASSERT_INT_EQUALS(0, longindex); |
| 36 |
ASSERT_INT_EQUALS(2, aws_cli_optind); |
| 37 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 38 |
ASSERT_INT_EQUALS('b', arg); |
| 39 |
ASSERT_STR_EQUALS("bval", aws_cli_optarg); |
| 40 |
ASSERT_INT_EQUALS(1, longindex); |
| 41 |
ASSERT_INT_EQUALS(4, aws_cli_optind); |
| 42 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 43 |
ASSERT_INT_EQUALS('c', arg); |
| 44 |
ASSERT_INT_EQUALS(2, longindex); |
| 45 |
ASSERT_INT_EQUALS(-1, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex)); |
| 46 |
|
| 47 |
return AWS_OP_SUCCESS; |
| 48 |
} |
| 49 |
|
| 50 |
AWS_TEST_CASE(short_argument_parse, s_test_short_argument_parse_fn) |
| 51 |
|
| 52 |
static int s_test_long_argument_parse_fn(struct aws_allocator *allocator, void *ctx) { |
| 53 |
(void)allocator; |
| 54 |
(void)ctx; |
| 55 |
struct aws_cli_option options[] = { |
| 56 |
{.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'}, |
| 57 |
{.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'}, |
| 58 |
{.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'}, |
| 59 |
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}, |
| 60 |
}; |
| 61 |
|
| 62 |
char *const args[] = { |
| 63 |
"prog-name", |
| 64 |
"--aaee", |
| 65 |
"--beeee", |
| 66 |
"bval", |
| 67 |
"-cceeee", |
| 68 |
}; |
| 69 |
int argc = 5; |
| 70 |
int longindex = 0; |
| 71 |
s_reset_static_state(); |
| 72 |
int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 73 |
ASSERT_INT_EQUALS('a', arg); |
| 74 |
ASSERT_INT_EQUALS(0, longindex); |
| 75 |
ASSERT_INT_EQUALS(2, aws_cli_optind); |
| 76 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 77 |
ASSERT_INT_EQUALS('b', arg); |
| 78 |
ASSERT_STR_EQUALS("bval", aws_cli_optarg); |
| 79 |
ASSERT_INT_EQUALS(1, longindex); |
| 80 |
ASSERT_INT_EQUALS(4, aws_cli_optind); |
| 81 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 82 |
ASSERT_INT_EQUALS('c', arg); |
| 83 |
ASSERT_INT_EQUALS(2, longindex); |
| 84 |
|
| 85 |
ASSERT_INT_EQUALS(-1, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex)); |
| 86 |
|
| 87 |
return AWS_OP_SUCCESS; |
| 88 |
} |
| 89 |
|
| 90 |
AWS_TEST_CASE(long_argument_parse, s_test_long_argument_parse_fn) |
| 91 |
|
| 92 |
static int s_test_unqualified_argument_parse_fn(struct aws_allocator *allocator, void *ctx) { |
| 93 |
(void)allocator; |
| 94 |
(void)ctx; |
| 95 |
struct aws_cli_option options[] = { |
| 96 |
{.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'}, |
| 97 |
{.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'}, |
| 98 |
{.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'}, |
| 99 |
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}, |
| 100 |
}; |
| 101 |
|
| 102 |
char *const args[] = {"prog-name", "-a", "--beeee", "bval", "-c", "operand"}; |
| 103 |
int argc = 6; |
| 104 |
int longindex = 0; |
| 105 |
s_reset_static_state(); |
| 106 |
int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 107 |
ASSERT_INT_EQUALS('a', arg); |
| 108 |
ASSERT_INT_EQUALS(0, longindex); |
| 109 |
ASSERT_INT_EQUALS(2, aws_cli_optind); |
| 110 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 111 |
ASSERT_INT_EQUALS('b', arg); |
| 112 |
ASSERT_STR_EQUALS("bval", aws_cli_optarg); |
| 113 |
ASSERT_INT_EQUALS(1, longindex); |
| 114 |
ASSERT_INT_EQUALS(4, aws_cli_optind); |
| 115 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 116 |
ASSERT_INT_EQUALS('c', arg); |
| 117 |
ASSERT_INT_EQUALS(2, longindex); |
| 118 |
|
| 119 |
ASSERT_INT_EQUALS(0x02, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex)); |
| 120 |
ASSERT_TRUE(aws_cli_optind == argc); |
| 121 |
ASSERT_STR_EQUALS("operand", aws_cli_positional_arg); |
| 122 |
|
| 123 |
return AWS_OP_SUCCESS; |
| 124 |
} |
| 125 |
|
| 126 |
AWS_TEST_CASE(unqualified_argument_parse, s_test_unqualified_argument_parse_fn) |
| 127 |
|
| 128 |
static int s_test_unknown_argument_parse_fn(struct aws_allocator *allocator, void *ctx) { |
| 129 |
(void)allocator; |
| 130 |
(void)ctx; |
| 131 |
struct aws_cli_option options[] = { |
| 132 |
{.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'}, |
| 133 |
{.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'}, |
| 134 |
{.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'}, |
| 135 |
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}, |
| 136 |
}; |
| 137 |
|
| 138 |
char *const args[] = {"prog-name", "-BOO!", "--beeee", "bval", "-c", "operand"}; |
| 139 |
int argc = 6; |
| 140 |
int longindex = 0; |
| 141 |
s_reset_static_state(); |
| 142 |
int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 143 |
ASSERT_INT_EQUALS('?', arg); |
| 144 |
ASSERT_INT_EQUALS(0, longindex); |
| 145 |
ASSERT_INT_EQUALS(2, aws_cli_optind); |
| 146 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 147 |
ASSERT_INT_EQUALS('b', arg); |
| 148 |
ASSERT_STR_EQUALS("bval", aws_cli_optarg); |
| 149 |
ASSERT_INT_EQUALS(1, longindex); |
| 150 |
ASSERT_INT_EQUALS(4, aws_cli_optind); |
| 151 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 152 |
ASSERT_INT_EQUALS('c', arg); |
| 153 |
ASSERT_INT_EQUALS(2, longindex); |
| 154 |
|
| 155 |
arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex); |
| 156 |
ASSERT_TRUE(arg == 0x02); |
| 157 |
ASSERT_TRUE(aws_cli_optind == argc); |
| 158 |
ASSERT_STR_EQUALS("operand", aws_cli_positional_arg); |
| 159 |
|
| 160 |
return AWS_OP_SUCCESS; |
| 161 |
} |
| 162 |
|
| 163 |
AWS_TEST_CASE(unknown_argument_parse, s_test_unknown_argument_parse_fn) |
| 164 |
|
| 165 |
struct subcommand_dispatch_data { |
| 166 |
const char *command_name; |
| 167 |
int argc; |
| 168 |
char *const *argv; |
| 169 |
}; |
| 170 |
|
| 171 |
static int s_subcommand_callback(int argc, char *const argv[], const char *command_name, void *user_data) { |
| 172 |
struct subcommand_dispatch_data *dispatch_data = user_data; |
| 173 |
dispatch_data->command_name = command_name; |
| 174 |
dispatch_data->argc = argc; |
| 175 |
dispatch_data->argv = argv; |
| 176 |
|
| 177 |
return AWS_OP_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
static int s_test_command_dispatch_fn(struct aws_allocator *allocator, void *ctx) { |
| 181 |
(void)allocator; |
| 182 |
(void)ctx; |
| 183 |
|
| 184 |
struct subcommand_dispatch_data dispatch_data; |
| 185 |
AWS_ZERO_STRUCT(dispatch_data); |
| 186 |
|
| 187 |
struct aws_cli_subcommand_dispatch dispatch_table[] = { |
| 188 |
{ |
| 189 |
.command_name = "command1", |
| 190 |
.subcommand_fn = s_subcommand_callback, |
| 191 |
}, |
| 192 |
{ |
| 193 |
.command_name = "command2", |
| 194 |
.subcommand_fn = s_subcommand_callback, |
| 195 |
}, |
| 196 |
}; |
| 197 |
|
| 198 |
char *const args_1[] = {"prog-name", "command1", "-BOO!", "--beeee", "bval", "-c", "operand"}; |
| 199 |
ASSERT_SUCCESS(aws_cli_dispatch_on_subcommand(7, args_1, dispatch_table, 2, &dispatch_data)); |
| 200 |
ASSERT_STR_EQUALS("command1", dispatch_data.command_name); |
| 201 |
ASSERT_INT_EQUALS(6, dispatch_data.argc); |
| 202 |
ASSERT_STR_EQUALS("command1", dispatch_data.argv[0]); |
| 203 |
|
| 204 |
AWS_ZERO_STRUCT(dispatch_data); |
| 205 |
char *const args_2[] = {"prog-name", "command2", "-BOO!", "--beeee", "bval", "-c", "operand"}; |
| 206 |
ASSERT_SUCCESS(aws_cli_dispatch_on_subcommand(7, args_2, dispatch_table, 2, &dispatch_data)); |
| 207 |
ASSERT_STR_EQUALS("command2", dispatch_data.command_name); |
| 208 |
ASSERT_INT_EQUALS(6, dispatch_data.argc); |
| 209 |
ASSERT_STR_EQUALS("command2", dispatch_data.argv[0]); |
| 210 |
|
| 211 |
char *const args_3[] = {"prog-name", "command3", "-BOO!", "--beeee", "bval", "-c", "operand"}; |
| 212 |
ASSERT_ERROR(AWS_ERROR_UNIMPLEMENTED, aws_cli_dispatch_on_subcommand(7, args_3, dispatch_table, 2, &dispatch_data)); |
| 213 |
|
| 214 |
char *const args_4[] = {"prog-name"}; |
| 215 |
ASSERT_ERROR( |
| 216 |
AWS_ERROR_INVALID_ARGUMENT, aws_cli_dispatch_on_subcommand(1, args_4, dispatch_table, 2, &dispatch_data)); |
| 217 |
|
| 218 |
return AWS_OP_SUCCESS; |
| 219 |
} |
| 220 |
|
| 221 |
AWS_TEST_CASE(test_command_dispatch, s_test_command_dispatch_fn) |