Git fork
at reftables-rust 825 lines 23 kB view raw
1#!/bin/sh 2# 3# Copyright (c) 2007 Johannes Schindelin 4# 5 6test_description='our own option parser' 7 8. ./test-lib.sh 9 10cat >expect <<\EOF 11usage: test-tool parse-options <options> 12 13 A helper function for the parse-options API. 14 15 --[no-]yes get a boolean 16 -D, --no-doubt begins with 'no-' 17 --doubt opposite of --no-doubt 18 -B, --no-fear be brave 19 -b, --[no-]boolean increment by one 20 -4, --[no-]or4 bitwise-or boolean with ...0100 21 --[no-]neg-or4 same as --no-or4 22 23 -i, --[no-]integer <n> 24 get a integer 25 --[no-]i16 <n> get a 16 bit integer 26 -j <n> get a integer, too 27 -u, --unsigned <n> get an unsigned integer 28 --u16 <n> get a 16 bit unsigned integer 29 --[no-]set23 set integer to 23 30 --mode1 set integer to 1 (cmdmode option) 31 --mode2 set integer to 2 (cmdmode option) 32 --[no-]mode34 (3|4) set integer to 3 or 4 (cmdmode option) 33 -L, --[no-]length <str> 34 get length of <str> 35 -F, --[no-]file <file> 36 set file to <file> 37 38String options 39 -s, --[no-]string <string> 40 get a string 41 --[no-]string2 <str> get another string 42 --[no-]st <st> get another string (pervert ordering) 43 -o <str> get another string 44 --longhelp help text of this entry 45 spans multiple lines 46 --[no-]list <str> add str to list 47 48Magic arguments 49 -NUM set integer to NUM 50 + same as -b 51 --ambiguous positive ambiguity 52 --no-ambiguous negative ambiguity 53 54Standard options 55 --[no-]abbrev[=<n>] use <n> digits to display object names 56 -v, --[no-]verbose be verbose 57 -n, --[no-]dry-run dry run 58 -q, --[no-]quiet be quiet 59 --[no-]expect <string> 60 expected output in the variable dump 61 62Alias 63 -A, --[no-]alias-source <string> 64 get a string 65 -Z, --[no-]alias-target <string> 66 alias of --alias-source 67 68EOF 69 70test_expect_success 'test help' ' 71 test_must_fail test-tool parse-options -h >output 2>output.err && 72 test_must_be_empty output.err && 73 test_cmp expect output 74' 75 76mv expect expect.err 77 78check () { 79 what="$1" && 80 shift && 81 expect="$1" && 82 shift && 83 test-tool parse-options --expect="$what $expect" "$@" 84} 85 86check_unknown_i18n() { 87 case "$1" in 88 --*) 89 echo error: unknown option \`${1#--}\' >expect ;; 90 -*) 91 echo error: unknown switch \`${1#-}\' >expect ;; 92 esac && 93 cat expect.err >>expect && 94 test_must_fail test-tool parse-options $* >output 2>output.err && 95 test_must_be_empty output && 96 test_cmp expect output.err 97} 98 99test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes' 100test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt' 101test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D' 102test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear' 103test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B' 104 105test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes' 106test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB' 107 108test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes' 109test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt' 110 111test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear' 112test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear' 113 114test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt' 115 116test_expect_success 'OPT_INTEGER() negative' 'check integer: -2345 -i -2345' 117test_expect_success 'OPT_INTEGER() kilo' 'check integer: 239616 -i 234k' 118test_expect_success 'OPT_INTEGER() negative kilo' 'check integer: -239616 -i -234k' 119 120test_expect_success 'OPT_UNSIGNED() simple' ' 121 check unsigned: 2345678 -u 2345678 122' 123 124test_expect_success 'OPT_UNSIGNED() kilo' ' 125 check unsigned: 239616 -u 234k 126' 127 128test_expect_success 'OPT_UNSIGNED() mega' ' 129 check unsigned: 104857600 -u 100m 130' 131 132test_expect_success 'OPT_UNSIGNED() giga' ' 133 check unsigned: 1073741824 -u 1g 134' 135 136test_expect_success 'OPT_UNSIGNED() 3giga' ' 137 check unsigned: 3221225472 -u 3g 138' 139 140cat >expect <<\EOF 141boolean: 2 142integer: 1729 143i16: 0 144unsigned: 16384 145u16: 0 146timestamp: 0 147string: 123 148abbrev: 7 149verbose: 2 150quiet: 0 151dry run: yes 152file: prefix/my.file 153EOF 154 155test_expect_success 'short options' ' 156 test-tool parse-options -s123 -b -i 1729 -u 16k -b -vv -n -F my.file \ 157 >output 2>output.err && 158 test_cmp expect output && 159 test_must_be_empty output.err 160' 161 162cat >expect <<\EOF 163boolean: 2 164integer: 1729 165i16: 9000 166unsigned: 16384 167u16: 32768 168timestamp: 0 169string: 321 170abbrev: 10 171verbose: 2 172quiet: 0 173dry run: no 174file: prefix/fi.le 175EOF 176 177test_expect_success 'long options' ' 178 test-tool parse-options --boolean --integer 1729 --i16 9000 --unsigned 16k \ 179 --u16 32k --boolean --string2=321 --verbose --verbose --no-dry-run \ 180 --abbrev=10 --file fi.le --obsolete \ 181 >output 2>output.err && 182 test_must_be_empty output.err && 183 test_cmp expect output 184' 185 186test_expect_success 'abbreviate to something longer than SHA1 length' ' 187 cat >expect <<-EOF && 188 boolean: 0 189 integer: 0 190 i16: 0 191 unsigned: 0 192 u16: 0 193 timestamp: 0 194 string: (not set) 195 abbrev: 100 196 verbose: -1 197 quiet: 0 198 dry run: no 199 file: (not set) 200 EOF 201 test-tool parse-options --abbrev=100 >output && 202 test_cmp expect output 203' 204 205test_expect_success 'missing required value' ' 206 cat >expect <<-\EOF && 207 error: switch `s'\'' requires a value 208 EOF 209 test_expect_code 129 test-tool parse-options -s 2>actual && 210 test_cmp expect actual && 211 212 cat >expect <<-\EOF && 213 error: option `string'\'' requires a value 214 EOF 215 test_expect_code 129 test-tool parse-options --string 2>actual && 216 test_cmp expect actual && 217 218 cat >expect <<-\EOF && 219 error: option `file'\'' requires a value 220 EOF 221 test_expect_code 129 test-tool parse-options --file 2>actual && 222 test_cmp expect actual 223' 224 225test_expect_success 'superfluous value provided: boolean' ' 226 cat >expect <<-\EOF && 227 error: option `yes'\'' takes no value 228 EOF 229 test_expect_code 129 test-tool parse-options --yes=hi 2>actual && 230 test_cmp expect actual && 231 232 cat >expect <<-\EOF && 233 error: option `no-yes'\'' takes no value 234 EOF 235 test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual && 236 test_cmp expect actual 237' 238 239test_expect_success 'superfluous value provided: boolean, abbreviated' ' 240 cat >expect <<-\EOF && 241 error: option `yes'\'' takes no value 242 EOF 243 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 244 test-tool parse-options --ye=hi 2>actual && 245 test_cmp expect actual && 246 247 cat >expect <<-\EOF && 248 error: option `no-yes'\'' takes no value 249 EOF 250 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 251 test-tool parse-options --no-ye=hi 2>actual && 252 test_cmp expect actual 253' 254 255test_expect_success 'superfluous value provided: cmdmode' ' 256 cat >expect <<-\EOF && 257 error: option `mode1'\'' takes no value 258 EOF 259 test_expect_code 129 test-tool parse-options --mode1=hi 2>actual && 260 test_cmp expect actual 261' 262 263cat >expect <<\EOF 264boolean: 1 265integer: 13 266i16: 0 267unsigned: 0 268u16: 0 269timestamp: 0 270string: 123 271abbrev: 7 272verbose: -1 273quiet: 0 274dry run: no 275file: (not set) 276arg 00: a1 277arg 01: b1 278arg 02: --boolean 279EOF 280 281test_expect_success 'intermingled arguments' ' 282 test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \ 283 >output 2>output.err && 284 test_must_be_empty output.err && 285 test_cmp expect output 286' 287 288cat >expect <<\EOF 289boolean: 0 290integer: 2 291i16: 0 292unsigned: 0 293u16: 0 294timestamp: 0 295string: (not set) 296abbrev: 7 297verbose: -1 298quiet: 0 299dry run: no 300file: (not set) 301EOF 302 303test_expect_success 'unambiguously abbreviated option' ' 304 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 305 test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err && 306 test_must_be_empty output.err && 307 test_cmp expect output 308' 309 310test_expect_success 'unambiguously abbreviated option with "="' ' 311 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 312 test-tool parse-options --expect="integer: 2" --int=2 313' 314 315test_expect_success 'ambiguously abbreviated option' ' 316 test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 317 test-tool parse-options --strin 123 318' 319 320test_expect_success 'non ambiguous option (after two options it abbreviates)' ' 321 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 322 test-tool parse-options --expect="string: 123" --st 123 323' 324 325test_expect_success 'Alias options do not contribute to abbreviation' ' 326 test-tool parse-options --alias-source 123 >output && 327 grep "^string: 123" output && 328 test-tool parse-options --alias-target 123 >output && 329 grep "^string: 123" output && 330 test_must_fail test-tool parse-options --alias && 331 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 332 test-tool parse-options --alias 123 >output && 333 grep "^string: 123" output 334' 335 336cat >typo.err <<\EOF 337error: did you mean `--boolean` (with two dashes)? 338EOF 339 340test_expect_success 'detect possible typos' ' 341 test_must_fail test-tool parse-options -boolean >output 2>output.err && 342 test_must_be_empty output && 343 test_cmp typo.err output.err 344' 345 346cat >typo.err <<\EOF 347error: did you mean `--ambiguous` (with two dashes)? 348EOF 349 350test_expect_success 'detect possible typos' ' 351 test_must_fail test-tool parse-options -ambiguous >output 2>output.err && 352 test_must_be_empty output && 353 test_cmp typo.err output.err 354' 355 356cat >expect <<\EOF 357Callback: "four", 0 358boolean: 5 359integer: 4 360i16: 0 361unsigned: 0 362u16: 0 363timestamp: 0 364string: (not set) 365abbrev: 7 366verbose: -1 367quiet: 0 368dry run: no 369file: (not set) 370EOF 371 372test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' ' 373 test-tool parse-options --length=four -b -4 >output 2>output.err && 374 test_must_be_empty output.err && 375 test_cmp expect output 376' 377 378test_expect_success 'OPT_CALLBACK() and callback errors work' ' 379 test_must_fail test-tool parse-options --no-length >output 2>output.err && 380 test_must_be_empty output && 381 test_must_be_empty output.err 382' 383 384cat >expect <<\EOF 385boolean: 1 386integer: 23 387i16: 0 388unsigned: 0 389u16: 0 390timestamp: 0 391string: (not set) 392abbrev: 7 393verbose: -1 394quiet: 0 395dry run: no 396file: (not set) 397EOF 398 399test_expect_success 'OPT_BIT() and OPT_SET_INT() work' ' 400 test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err && 401 test_must_be_empty output.err && 402 test_cmp expect output 403' 404 405test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' ' 406 test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err && 407 test_must_be_empty output.err && 408 test_cmp expect output 409' 410 411test_expect_success 'OPT_BIT() works' ' 412 test-tool parse-options --expect="boolean: 6" -bb --or4 413' 414 415test_expect_success 'OPT_NEGBIT() works' ' 416 test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4 417' 418 419test_expect_success 'OPT_CMDMODE() works' ' 420 test-tool parse-options --expect="integer: 1" --mode1 && 421 test-tool parse-options --expect="integer: 3" --mode34=3 422' 423 424test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' ' 425 test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err && 426 test_must_be_empty output && 427 test_grep "mode1" output.err && 428 test_grep "mode2" output.err && 429 test_grep "cannot be used together" output.err 430' 431 432test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' ' 433 test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err && 434 test_must_be_empty output && 435 test_grep "mode2" output.err && 436 test_grep "set23" output.err && 437 test_grep "cannot be used together" output.err 438' 439 440test_expect_success 'OPT_CMDMODE() detects incompatibility (3)' ' 441 test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err && 442 test_must_be_empty output && 443 test_grep "mode2" output.err && 444 test_grep "set23" output.err && 445 test_grep "cannot be used together" output.err 446' 447 448test_expect_success 'OPT_CMDMODE() detects incompatibility (4)' ' 449 test_must_fail test-tool parse-options --mode2 --mode34=3 \ 450 >output 2>output.err && 451 test_must_be_empty output && 452 test_grep "mode2" output.err && 453 test_grep "mode34.3" output.err && 454 test_grep "cannot be used together" output.err 455' 456 457test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' ' 458 test-tool parse-options --expect="boolean: 6" + + + + + + 459' 460 461test_expect_success 'OPT_NUMBER_CALLBACK() works' ' 462 test-tool parse-options --expect="integer: 12345" -12345 463' 464 465cat >expect <<\EOF 466boolean: 0 467integer: 0 468i16: 0 469unsigned: 0 470u16: 0 471timestamp: 0 472string: (not set) 473abbrev: 7 474verbose: -1 475quiet: 0 476dry run: no 477file: (not set) 478EOF 479 480test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' 481 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 482 test-tool parse-options --no-ambig >output 2>output.err && 483 test_must_be_empty output.err && 484 test_cmp expect output 485' 486 487cat >>expect <<\EOF 488list: foo 489list: bar 490list: baz 491EOF 492test_expect_success '--list keeps list of strings' ' 493 test-tool parse-options --list foo --list=bar --list=baz >output && 494 test_cmp expect output 495' 496 497test_expect_success '--no-list resets list' ' 498 test-tool parse-options --list=other --list=irrelevant --list=options \ 499 --no-list --list=foo --list=bar --list=baz >output && 500 test_cmp expect output 501' 502 503test_expect_success 'multiple quiet levels' ' 504 test-tool parse-options --expect="quiet: 3" -q -q -q 505' 506 507test_expect_success 'multiple verbose levels' ' 508 test-tool parse-options --expect="verbose: 3" -v -v -v 509' 510 511test_expect_success '--no-quiet sets --quiet to 0' ' 512 test-tool parse-options --expect="quiet: 0" --no-quiet 513' 514 515test_expect_success '--no-quiet resets multiple -q to 0' ' 516 test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet 517' 518 519test_expect_success '--no-verbose sets verbose to 0' ' 520 test-tool parse-options --expect="verbose: 0" --no-verbose 521' 522 523test_expect_success '--no-verbose resets multiple verbose to 0' ' 524 test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose 525' 526 527test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' ' 528 GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \ 529 test-tool parse-options --ye && 530 test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \ 531 test-tool parse-options --ye 532' 533 534test_expect_success '--end-of-options treats remainder as args' ' 535 test-tool parse-options \ 536 --expect="verbose: -1" \ 537 --expect="arg 00: --verbose" \ 538 --end-of-options --verbose 539' 540 541test_expect_success 'KEEP_DASHDASH works' ' 542 test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual && 543 cat >expect <<-\EOF && 544 opt: 1 545 arg 00: -- 546 arg 01: --opt=2 547 arg 02: --unknown 548 EOF 549 test_cmp expect actual 550' 551 552test_expect_success 'KEEP_ARGV0 works' ' 553 test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual && 554 cat >expect <<-\EOF && 555 opt: 3 556 arg 00: cmd 557 arg 01: arg0 558 EOF 559 test_cmp expect actual 560' 561 562test_expect_success 'STOP_AT_NON_OPTION works' ' 563 test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual && 564 cat >expect <<-\EOF && 565 opt: 4 566 arg 00: arg0 567 arg 01: --opt=5 568 arg 02: --unknown 569 EOF 570 test_cmp expect actual 571' 572 573test_expect_success 'KEEP_UNKNOWN_OPT works' ' 574 test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual && 575 cat >expect <<-\EOF && 576 opt: 6 577 arg 00: --unknown=1 578 arg 01: -u2 579 EOF 580 test_cmp expect actual 581' 582 583test_expect_success 'NO_INTERNAL_HELP works for -h' ' 584 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err && 585 grep "^error: unknown switch \`h$SQ" err && 586 grep "^usage: " err 587' 588 589for help_opt in help help-all 590do 591 test_expect_success "NO_INTERNAL_HELP works for --$help_opt" " 592 test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err && 593 grep '^error: unknown option \`'$help_opt\' err && 594 grep '^usage: ' err 595 " 596done 597 598test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' ' 599 test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual && 600 cat >expect <<-\EOF && 601 opt: 0 602 arg 00: -h 603 arg 01: --help 604 arg 02: --help-all 605 EOF 606 test_cmp expect actual 607' 608 609test_expect_success 'subcommand - no subcommand shows error and usage' ' 610 test_expect_code 129 test-tool parse-subcommand cmd 2>err && 611 grep "^error: need a subcommand" err && 612 grep ^usage: err 613' 614 615test_expect_success 'subcommand - subcommand after -- shows error and usage' ' 616 test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err && 617 grep "^error: need a subcommand" err && 618 grep ^usage: err 619' 620 621test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' ' 622 test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err && 623 grep "^error: need a subcommand" err && 624 grep ^usage: err 625' 626 627test_expect_success 'subcommand - unknown subcommand shows error and usage' ' 628 test_expect_code 129 test-tool parse-subcommand cmd nope 2>err && 629 grep "^error: unknown subcommand: \`nope$SQ" err && 630 grep ^usage: err 631' 632 633test_expect_success 'subcommand - subcommands cannot be abbreviated' ' 634 test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err && 635 grep "^error: unknown subcommand: \`subcmd-o$SQ$" err && 636 grep ^usage: err 637' 638 639test_expect_success 'subcommand - no negated subcommands' ' 640 test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err && 641 grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err && 642 grep ^usage: err 643' 644 645test_expect_success 'subcommand - simple' ' 646 test-tool parse-subcommand cmd subcmd-two >actual && 647 cat >expect <<-\EOF && 648 opt: 0 649 fn: subcmd_two 650 arg 00: subcmd-two 651 EOF 652 test_cmp expect actual 653' 654 655test_expect_success 'subcommand - stop parsing at the first subcommand' ' 656 test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual && 657 cat >expect <<-\EOF && 658 opt: 1 659 fn: subcmd_two 660 arg 00: subcmd-two 661 arg 01: subcmd-one 662 arg 02: --opt=2 663 EOF 664 test_cmp expect actual 665' 666 667test_expect_success 'subcommand - KEEP_ARGV0' ' 668 test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual && 669 cat >expect <<-\EOF && 670 opt: 0 671 fn: subcmd_two 672 arg 00: cmd 673 arg 01: subcmd-two 674 EOF 675 test_cmp expect actual 676' 677 678test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' ' 679 test-tool parse-subcommand --subcommand-optional cmd >actual && 680 cat >expect <<-\EOF && 681 opt: 0 682 fn: subcmd_one 683 EOF 684 test_cmp expect actual 685' 686 687test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' ' 688 test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual && 689 cat >expect <<-\EOF && 690 opt: 0 691 fn: subcmd_two 692 arg 00: subcmd-two 693 arg 01: branch 694 arg 02: file 695 EOF 696 test_cmp expect actual 697' 698 699test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' ' 700 test-tool parse-subcommand --subcommand-optional cmd branch file >actual && 701 cat >expect <<-\EOF && 702 opt: 0 703 fn: subcmd_one 704 arg 00: branch 705 arg 01: file 706 EOF 707 test_cmp expect actual 708' 709 710test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' ' 711 test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err && 712 grep "^error: unknown option" err && 713 grep ^usage: err 714' 715 716test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' ' 717 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual && 718 cat >expect <<-\EOF && 719 opt: 0 720 fn: subcmd_one 721 arg 00: --subcommand-opt 722 EOF 723 test_cmp expect actual 724' 725 726test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' ' 727 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual && 728 cat >expect <<-\EOF && 729 opt: 0 730 fn: subcmd_one 731 arg 00: --subcommand-opt 732 arg 01: subcmd-two 733 EOF 734 test_cmp expect actual 735' 736 737test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' ' 738 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual && 739 cat >expect <<-\EOF && 740 opt: 0 741 fn: subcmd_one 742 arg 00: --subcommand-opt 743 arg 01: branch 744 arg 02: --opt=1 745 EOF 746 test_cmp expect actual 747' 748 749test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' ' 750 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual && 751 cat >expect <<-\EOF && 752 opt: 0 753 fn: subcmd_one 754 arg 00: cmd 755 arg 01: --subcommand-opt 756 arg 02: branch 757 EOF 758 test_cmp expect actual 759' 760 761test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' ' 762 test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual && 763 cat >expect <<-\EOF && 764 opt: 0 765 fn: subcmd_one 766 arg 00: -- 767 arg 01: --subcommand-opt 768 arg 02: file 769 EOF 770 test_cmp expect actual 771' 772 773test_expect_success 'subcommand - completion helper' ' 774 test-tool parse-subcommand cmd --git-completion-helper >actual && 775 echo "subcmd-one subcmd-two --opt= --no-opt" >expect && 776 test_cmp expect actual 777' 778 779test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' ' 780 test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err && 781 grep ^BUG err 782' 783 784test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' ' 785 test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err && 786 grep ^BUG err 787' 788 789test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' ' 790 test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err && 791 grep ^BUG err 792' 793 794test_expect_success 'negative unsigned' ' 795 test_must_fail test-tool parse-options --unsigned -1 >out 2>err && 796 grep "non-negative integer" err && 797 test_must_be_empty out 798' 799 800test_expect_success 'unsigned with units but no numbers' ' 801 test_must_fail test-tool parse-options --unsigned m >out 2>err && 802 grep "non-negative integer" err && 803 test_must_be_empty out 804' 805 806test_expect_success 'i16 limits range' ' 807 test-tool parse-options --i16 32767 >out && 808 test_grep "i16: 32767" out && 809 test_must_fail test-tool parse-options --i16 32768 2>err && 810 test_grep "value 32768 for option .i16. not in range \[-32768,32767\]" err && 811 812 test-tool parse-options --i16 -32768 >out && 813 test_grep "i16: -32768" out && 814 test_must_fail test-tool parse-options --i16 -32769 2>err && 815 test_grep "value -32769 for option .i16. not in range \[-32768,32767\]" err 816' 817 818test_expect_success 'u16 limits range' ' 819 test-tool parse-options --u16 65535 >out && 820 test_grep "u16: 65535" out && 821 test_must_fail test-tool parse-options --u16 65536 2>err && 822 test_grep "value 65536 for option .u16. not in range \[0,65535\]" err 823' 824 825test_done