| [88e7dc5] | 1 | /*
|
|---|
| [d7f7a4a] | 2 | * SPDX-FileCopyrightText: 2019 Matthieu Riolo
|
|---|
| [88e7dc5] | 3 | *
|
|---|
| [d7f7a4a] | 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| [88e7dc5] | 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <pcut/pcut.h>
|
|---|
| 8 | #include <getopt.h>
|
|---|
| 9 | #include <stdio.h>
|
|---|
| 10 |
|
|---|
| 11 | PCUT_INIT;
|
|---|
| 12 |
|
|---|
| 13 | PCUT_TEST_SUITE(getopt);
|
|---|
| 14 |
|
|---|
| 15 | PCUT_TEST(getopt_param_flag)
|
|---|
| 16 | {
|
|---|
| 17 | int argc = 4;
|
|---|
| 18 | const char *argv[] = {
|
|---|
| 19 | "get_opt_test",
|
|---|
| 20 | "-f",
|
|---|
| 21 | "-p",
|
|---|
| 22 | "param",
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | const char *options = "fp:";
|
|---|
| 26 |
|
|---|
| 27 | int ret;
|
|---|
| 28 | optreset = 1;
|
|---|
| 29 | opterr = 0;
|
|---|
| 30 |
|
|---|
| 31 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 32 |
|
|---|
| 33 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 34 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 35 |
|
|---|
| 36 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 37 |
|
|---|
| 38 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 39 | PCUT_ASSERT_INT_EQUALS(4, optind);
|
|---|
| 40 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 41 |
|
|---|
| 42 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 43 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | PCUT_TEST(getopt_concat_flags)
|
|---|
| 47 | {
|
|---|
| 48 | int argc = 2;
|
|---|
| 49 | const char *argv[] = {
|
|---|
| 50 | "get_opt_test",
|
|---|
| 51 | "-fda",
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | const char *options = "afd";
|
|---|
| 55 |
|
|---|
| 56 | int ret;
|
|---|
| 57 | optreset = 1;
|
|---|
| 58 | opterr = 0;
|
|---|
| 59 |
|
|---|
| 60 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 61 |
|
|---|
| 62 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 63 | PCUT_ASSERT_INT_EQUALS(1, optind);
|
|---|
| 64 |
|
|---|
| 65 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 66 |
|
|---|
| 67 | PCUT_ASSERT_INT_EQUALS('d', ret);
|
|---|
| 68 | PCUT_ASSERT_INT_EQUALS(1, optind);
|
|---|
| 69 |
|
|---|
| 70 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 71 |
|
|---|
| 72 | PCUT_ASSERT_INT_EQUALS('a', ret);
|
|---|
| 73 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 74 |
|
|---|
| 75 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 76 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | PCUT_TEST(getopt_concat_flag_param)
|
|---|
| 80 | {
|
|---|
| 81 | int argc = 3;
|
|---|
| 82 | const char *argv[] = {
|
|---|
| 83 | "get_opt_test",
|
|---|
| 84 | "-fp",
|
|---|
| 85 | "param"
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | const char *options = "fp:";
|
|---|
| 89 |
|
|---|
| 90 | int ret;
|
|---|
| 91 | optreset = 1;
|
|---|
| 92 | opterr = 0;
|
|---|
| 93 |
|
|---|
| 94 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 95 |
|
|---|
| 96 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 97 | PCUT_ASSERT_INT_EQUALS(1, optind);
|
|---|
| 98 |
|
|---|
| 99 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 100 |
|
|---|
| 101 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 102 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 103 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 104 |
|
|---|
| 105 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 106 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | PCUT_TEST(getopt_missing_param1)
|
|---|
| 110 | {
|
|---|
| 111 | int argc = 2;
|
|---|
| 112 | const char *argv[] = {
|
|---|
| 113 | "get_opt_test",
|
|---|
| 114 | "-p",
|
|---|
| 115 | };
|
|---|
| 116 |
|
|---|
| 117 | const char *options = "p:";
|
|---|
| 118 |
|
|---|
| 119 | int ret;
|
|---|
| 120 | optreset = 1;
|
|---|
| 121 | opterr = 0;
|
|---|
| 122 |
|
|---|
| 123 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 124 |
|
|---|
| 125 | PCUT_ASSERT_INT_EQUALS('?', ret);
|
|---|
| 126 | PCUT_ASSERT_INT_EQUALS('p', optopt);
|
|---|
| 127 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 128 |
|
|---|
| 129 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 130 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | PCUT_TEST(getopt_missing_param2)
|
|---|
| 134 | {
|
|---|
| 135 | int argc = 2;
|
|---|
| 136 | const char *argv[] = {
|
|---|
| 137 | "get_opt_test",
|
|---|
| 138 | "-p",
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | const char *options = ":p:";
|
|---|
| 142 |
|
|---|
| 143 | int ret;
|
|---|
| 144 | optreset = 1;
|
|---|
| 145 | opterr = 0;
|
|---|
| 146 |
|
|---|
| 147 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 148 |
|
|---|
| 149 | PCUT_ASSERT_INT_EQUALS(':', ret);
|
|---|
| 150 | PCUT_ASSERT_INT_EQUALS('p', optopt);
|
|---|
| 151 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 152 |
|
|---|
| 153 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 154 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | PCUT_TEST(getopt_illegal_option)
|
|---|
| 158 | {
|
|---|
| 159 | int argc = 2;
|
|---|
| 160 | const char *argv[] = {
|
|---|
| 161 | "get_opt_test",
|
|---|
| 162 | "-p",
|
|---|
| 163 | };
|
|---|
| 164 |
|
|---|
| 165 | const char *options = "a";
|
|---|
| 166 |
|
|---|
| 167 | int ret;
|
|---|
| 168 | optreset = 1;
|
|---|
| 169 | opterr = 0;
|
|---|
| 170 |
|
|---|
| 171 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 172 |
|
|---|
| 173 | PCUT_ASSERT_INT_EQUALS('?', ret);
|
|---|
| 174 | PCUT_ASSERT_INT_EQUALS('p', optopt);
|
|---|
| 175 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 176 |
|
|---|
| 177 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 178 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 179 |
|
|---|
| 180 | options = ":a";
|
|---|
| 181 | optreset = 1;
|
|---|
| 182 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 183 |
|
|---|
| 184 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 185 | PCUT_ASSERT_INT_EQUALS('p', optopt);
|
|---|
| 186 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 187 |
|
|---|
| 188 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 189 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | PCUT_TEST(getopt_flag_with_param)
|
|---|
| 193 | {
|
|---|
| 194 | int argc = 4;
|
|---|
| 195 | const char *argv[] = {
|
|---|
| 196 | "get_opt_test",
|
|---|
| 197 | "-f",
|
|---|
| 198 | "param",
|
|---|
| 199 | "-d"
|
|---|
| 200 | };
|
|---|
| 201 |
|
|---|
| 202 | const char *options = "fd";
|
|---|
| 203 |
|
|---|
| 204 | int ret;
|
|---|
| 205 | optreset = 1;
|
|---|
| 206 | opterr = 0;
|
|---|
| 207 |
|
|---|
| 208 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 209 |
|
|---|
| 210 | /* getopt() would print a error message but thx to opterror=0 it doesnt */
|
|---|
| 211 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 212 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 213 |
|
|---|
| 214 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 215 |
|
|---|
| 216 | PCUT_ASSERT_INT_EQUALS('d', ret);
|
|---|
| 217 | PCUT_ASSERT_INT_EQUALS(4, optind);
|
|---|
| 218 |
|
|---|
| 219 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 220 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | PCUT_TEST(getopt_case_sensitive)
|
|---|
| 224 | {
|
|---|
| 225 | int argc = 3;
|
|---|
| 226 | const char *argv[] = {
|
|---|
| 227 | "get_opt_test",
|
|---|
| 228 | "-F",
|
|---|
| 229 | "-f"
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | const char *options = "fF";
|
|---|
| 233 |
|
|---|
| 234 | int ret;
|
|---|
| 235 | optreset = 1;
|
|---|
| 236 | opterr = 0;
|
|---|
| 237 |
|
|---|
| 238 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 239 |
|
|---|
| 240 | PCUT_ASSERT_INT_EQUALS('F', ret);
|
|---|
| 241 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 242 |
|
|---|
| 243 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 244 |
|
|---|
| 245 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 246 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 247 |
|
|---|
| 248 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 249 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | PCUT_TEST(getopt_optional_param)
|
|---|
| 253 | {
|
|---|
| 254 | int argc = 4;
|
|---|
| 255 | const char *argv[] = {
|
|---|
| 256 | "get_opt_test",
|
|---|
| 257 | "-f",
|
|---|
| [a163d10] | 258 | "-pparam"
|
|---|
| [88e7dc5] | 259 | };
|
|---|
| 260 |
|
|---|
| 261 | const char *options = "f::p::";
|
|---|
| 262 |
|
|---|
| 263 | int ret;
|
|---|
| 264 | optreset = 1;
|
|---|
| 265 | opterr = 0;
|
|---|
| 266 |
|
|---|
| 267 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 268 |
|
|---|
| 269 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 270 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 271 | PCUT_ASSERT_NULL(optarg);
|
|---|
| 272 |
|
|---|
| 273 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 274 |
|
|---|
| 275 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 276 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 277 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 278 |
|
|---|
| 279 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 280 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | PCUT_TEST(getopt_special_option)
|
|---|
| 284 | {
|
|---|
| 285 | int argc = 4;
|
|---|
| 286 | const char *argv[] = {
|
|---|
| 287 | "get_opt_test",
|
|---|
| 288 | "-f",
|
|---|
| 289 | "--",
|
|---|
| 290 | "-p"
|
|---|
| 291 | };
|
|---|
| 292 |
|
|---|
| 293 | const char *options = "fp";
|
|---|
| 294 |
|
|---|
| 295 | int ret;
|
|---|
| 296 | optreset = 1;
|
|---|
| 297 | opterr = 0;
|
|---|
| 298 |
|
|---|
| 299 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 300 |
|
|---|
| 301 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 302 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 303 |
|
|---|
| 304 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 305 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | PCUT_TEST(getopt_gnu_plus)
|
|---|
| 309 | {
|
|---|
| 310 | int argc = 4;
|
|---|
| 311 | const char *argv[] = {
|
|---|
| 312 | "get_opt_test",
|
|---|
| 313 | "-f",
|
|---|
| 314 | "break",
|
|---|
| 315 | "-p"
|
|---|
| 316 | };
|
|---|
| 317 |
|
|---|
| 318 | const char *options = "+fp";
|
|---|
| 319 |
|
|---|
| 320 | int ret;
|
|---|
| 321 | optreset = 1;
|
|---|
| 322 | opterr = 0;
|
|---|
| 323 |
|
|---|
| 324 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 325 |
|
|---|
| 326 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 327 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 328 |
|
|---|
| 329 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 330 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | PCUT_TEST(getopt_gnu_minus)
|
|---|
| 334 | {
|
|---|
| 335 | int argc = 4;
|
|---|
| 336 | const char *argv[] = {
|
|---|
| 337 | "get_opt_test",
|
|---|
| 338 | "-f",
|
|---|
| 339 | "break",
|
|---|
| 340 | "-p"
|
|---|
| 341 | };
|
|---|
| 342 |
|
|---|
| 343 | const char *options = "-fp";
|
|---|
| 344 |
|
|---|
| 345 | int ret;
|
|---|
| 346 | optreset = 1;
|
|---|
| 347 | opterr = 0;
|
|---|
| 348 |
|
|---|
| 349 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 350 |
|
|---|
| 351 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 352 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 353 |
|
|---|
| 354 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 355 |
|
|---|
| 356 | PCUT_ASSERT_INT_EQUALS(1, ret);
|
|---|
| 357 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 358 | PCUT_ASSERT_STR_EQUALS("break", optarg);
|
|---|
| 359 |
|
|---|
| 360 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 361 |
|
|---|
| 362 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 363 | PCUT_ASSERT_INT_EQUALS(4, optind);
|
|---|
| 364 |
|
|---|
| 365 | ret = getopt(argc, (char *const *)argv, options);
|
|---|
| 366 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | PCUT_TEST(getopt_long_flag_param)
|
|---|
| 370 | {
|
|---|
| 371 | int argc = 4;
|
|---|
| 372 | const char *argv[] = {
|
|---|
| 373 | "get_opt_test",
|
|---|
| 374 | "--flag",
|
|---|
| 375 | "--parameter",
|
|---|
| 376 | "param"
|
|---|
| 377 | };
|
|---|
| 378 |
|
|---|
| 379 | const char *options = "fp:";
|
|---|
| 380 |
|
|---|
| 381 | const struct option long_options[] = {
|
|---|
| 382 | { "flag", no_argument, NULL, 'f' },
|
|---|
| 383 | { "parameter", required_argument, NULL, 'p' },
|
|---|
| 384 | };
|
|---|
| 385 |
|
|---|
| 386 | int ret;
|
|---|
| 387 | int idx;
|
|---|
| 388 | optreset = 1;
|
|---|
| 389 | opterr = 0;
|
|---|
| 390 |
|
|---|
| 391 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 392 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 393 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 394 | PCUT_ASSERT_INT_EQUALS(0, idx);
|
|---|
| 395 |
|
|---|
| 396 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 397 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 398 | PCUT_ASSERT_INT_EQUALS(4, optind);
|
|---|
| 399 | PCUT_ASSERT_INT_EQUALS(1, idx);
|
|---|
| 400 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 401 |
|
|---|
| 402 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 403 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | PCUT_TEST(getopt_long_alt_param)
|
|---|
| 407 | {
|
|---|
| 408 | int argc = 3;
|
|---|
| 409 | const char *argv[] = {
|
|---|
| 410 | "get_opt_test",
|
|---|
| 411 | "--flag=\"param param\"",
|
|---|
| 412 | "--parameter=param",
|
|---|
| 413 | };
|
|---|
| 414 |
|
|---|
| 415 | const char *options = "f:p:";
|
|---|
| 416 |
|
|---|
| 417 | const struct option long_options[] = {
|
|---|
| 418 | { "flag", required_argument, NULL, 'f' },
|
|---|
| 419 | { "parameter", required_argument, NULL, 'p' },
|
|---|
| 420 | { 0, 0, 0, 0 }
|
|---|
| 421 | };
|
|---|
| 422 |
|
|---|
| 423 | int ret;
|
|---|
| 424 | int idx;
|
|---|
| 425 | optreset = 1;
|
|---|
| 426 | opterr = 0;
|
|---|
| 427 |
|
|---|
| 428 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 429 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 430 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 431 | PCUT_ASSERT_INT_EQUALS(0, idx);
|
|---|
| 432 | PCUT_ASSERT_STR_EQUALS("\"param param\"", optarg);
|
|---|
| 433 |
|
|---|
| 434 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 435 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 436 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 437 | PCUT_ASSERT_INT_EQUALS(1, idx);
|
|---|
| 438 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 439 |
|
|---|
| 440 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 441 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | PCUT_TEST(getopt_long_optional_param)
|
|---|
| 445 | {
|
|---|
| 446 | int argc = 3;
|
|---|
| 447 | const char *argv[] = {
|
|---|
| 448 | "get_opt_test",
|
|---|
| 449 | "--flag=param",
|
|---|
| 450 | "--parameter",
|
|---|
| 451 | };
|
|---|
| 452 |
|
|---|
| 453 | const char *options = "f::p::";
|
|---|
| 454 |
|
|---|
| 455 | const struct option long_options[] = {
|
|---|
| 456 | { "flag", optional_argument, NULL, 'f' },
|
|---|
| 457 | { "parameter", optional_argument, NULL, 'p' },
|
|---|
| 458 | { 0, 0, 0, 0 }
|
|---|
| 459 | };
|
|---|
| 460 |
|
|---|
| 461 | int ret;
|
|---|
| 462 | int idx;
|
|---|
| 463 | optreset = 1;
|
|---|
| 464 | opterr = 0;
|
|---|
| 465 |
|
|---|
| 466 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 467 | PCUT_ASSERT_INT_EQUALS('f', ret);
|
|---|
| 468 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 469 | PCUT_ASSERT_INT_EQUALS(0, idx);
|
|---|
| 470 | PCUT_ASSERT_STR_EQUALS("param", optarg);
|
|---|
| 471 |
|
|---|
| 472 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 473 | PCUT_ASSERT_INT_EQUALS('p', ret);
|
|---|
| 474 | PCUT_ASSERT_INT_EQUALS(3, optind);
|
|---|
| 475 | PCUT_ASSERT_INT_EQUALS(1, idx);
|
|---|
| 476 | PCUT_ASSERT_NULL(optarg);
|
|---|
| 477 |
|
|---|
| 478 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 479 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | PCUT_TEST(getopt_long_illegal_option)
|
|---|
| 483 | {
|
|---|
| 484 | int argc = 3;
|
|---|
| 485 | const char *argv[] = {
|
|---|
| 486 | "get_opt_test",
|
|---|
| 487 | "--param",
|
|---|
| 488 | "param",
|
|---|
| 489 | };
|
|---|
| 490 |
|
|---|
| 491 | const char *options = "f::";
|
|---|
| 492 |
|
|---|
| 493 | const struct option long_options[] = {
|
|---|
| 494 | { "cflag", required_argument, NULL, 'c' },
|
|---|
| 495 | { "flag", required_argument, NULL, 'f' },
|
|---|
| 496 | { 0, 0, 0, 0 }
|
|---|
| 497 | };
|
|---|
| 498 |
|
|---|
| 499 | int ret;
|
|---|
| 500 | int idx;
|
|---|
| 501 | optreset = 1;
|
|---|
| 502 | opterr = 0;
|
|---|
| 503 |
|
|---|
| 504 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 505 | PCUT_ASSERT_INT_EQUALS('?', ret);
|
|---|
| 506 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 507 | PCUT_ASSERT_INT_EQUALS(0, idx);
|
|---|
| 508 | PCUT_ASSERT_INT_EQUALS(0, optopt);
|
|---|
| 509 |
|
|---|
| 510 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 511 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| 514 | PCUT_TEST(getopt_long_ambiguous_param)
|
|---|
| 515 | {
|
|---|
| 516 | int argc = 3;
|
|---|
| 517 | const char *argv[] = {
|
|---|
| 518 | "get_opt_test",
|
|---|
| 519 | "--flag",
|
|---|
| 520 | "param",
|
|---|
| 521 | };
|
|---|
| 522 |
|
|---|
| 523 | const char *options = "f::";
|
|---|
| 524 |
|
|---|
| 525 | const struct option long_options[] = {
|
|---|
| 526 | { "flag1", optional_argument, NULL, 'f' },
|
|---|
| 527 | { "flag2", required_argument, NULL, 'f' },
|
|---|
| 528 | { 0, 0, 0, 0 }
|
|---|
| 529 | };
|
|---|
| 530 |
|
|---|
| 531 | int ret;
|
|---|
| 532 | int idx;
|
|---|
| 533 | optreset = 1;
|
|---|
| 534 | opterr = 0;
|
|---|
| 535 |
|
|---|
| 536 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 537 | PCUT_ASSERT_INT_EQUALS('?', ret);
|
|---|
| 538 | PCUT_ASSERT_INT_EQUALS(2, optind);
|
|---|
| 539 | PCUT_ASSERT_INT_EQUALS(0, idx);
|
|---|
| 540 | PCUT_ASSERT_INT_EQUALS(0, optopt);
|
|---|
| 541 |
|
|---|
| 542 | ret = getopt_long(argc, (char *const *)argv, options, long_options, &idx);
|
|---|
| 543 | PCUT_ASSERT_INT_EQUALS(-1, ret);
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | PCUT_EXPORT(getopt);
|
|---|