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