1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
|
# -*- coding: utf-8 -*-
from functools import wraps
import pytest
import click
from click_option_group import (
optgroup,
OptionGroup,
GroupedOption,
RequiredAnyOptionGroup,
AllOptionGroup,
RequiredAllOptionGroup,
MutuallyExclusiveOptionGroup,
RequiredMutuallyExclusiveOptionGroup,
)
def test_basic_functionality_first_api(runner):
@click.command()
@click.option('--hello')
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo1')
@optgroup.option('--bar1')
@click.option('--lol')
@optgroup.group('Group 2', help='Group 2 description')
@optgroup.option('--foo2')
@optgroup.option('--bar2')
@click.option('--goodbye')
def cli(hello, foo1, bar1, lol, foo2, bar2, goodbye):
click.echo(f'{foo1},{bar1},{foo2},{bar2}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' in result.output
assert 'Group 1 description' in result.output
assert 'Group 2:' in result.output
assert 'Group 2 description' in result.output
result = runner.invoke(cli, [
'--foo1', 'foo1', '--bar1', 'bar1',
'--foo2', 'foo2', '--bar2', 'bar2'])
assert not result.exception
assert 'foo1,bar1,foo2,bar2' in result.output
def test_noname_group(runner):
@click.command()
@optgroup()
@optgroup.option('--foo')
def cli(foo):
pass
result = runner.invoke(cli, ['--help'])
assert 'Options:\n --foo' in result.output
@click.command()
@optgroup(help='Group description')
@optgroup.option('--foo')
def cli(foo):
pass
result = runner.invoke(cli, ['--help'])
assert 'Group description' in result.output
def test_mix_decl_first_api():
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\]"):
@click.command()
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo')
@click.option('--hello')
@optgroup.option('--bar')
def cli1(**params):
pass
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\]"):
@click.command()
@optgroup('Group 1', help='Group 1 description')
@click.option('--hello')
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli2(**params):
pass
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello2'\]"):
@click.command()
@optgroup('Group 1', help='Group 1 description')
@click.option('--hello1')
@optgroup.option('--foo')
@click.option('--hello2')
@optgroup.option('--bar')
def cli3(**params):
pass
def test_missing_group_decl_first_api(runner):
@click.command()
@click.option('--hello1')
@optgroup.option('--foo')
@optgroup.option('--bar')
@click.option('--hello2')
def cli(**params):
pass
# FIXME: https://github.com/click-contrib/click-option-group/issues/65
# Now we just do not check not attached options for --help
# because handle_parse_result is not called in click >=8.1.8 for --help
# result = runner.invoke(cli, ['--help'])
#
# assert result.exception
# assert TypeError == result.exc_info[0]
# assert 'Missing option group decorator' in str(result.exc_info[1])
# assert '--foo' in str(result.exc_info[1])
# assert '--bar' in str(result.exc_info[1])
result = runner.invoke(cli, [])
assert result.exception
assert TypeError == result.exc_info[0]
assert 'Missing option group decorator' in str(result.exc_info[1])
assert '--foo' in str(result.exc_info[1])
assert '--bar' in str(result.exc_info[1])
result = runner.invoke(cli, ['--hello1', 'hello1'])
assert result.exception
assert TypeError == result.exc_info[0]
assert 'Missing option group decorator' in str(result.exc_info[1])
assert '--foo' in str(result.exc_info[1])
assert '--bar' in str(result.exc_info[1])
result = runner.invoke(cli, ['--foo', 'foo'])
assert result.exception
assert TypeError == result.exc_info[0]
assert 'Missing option group decorator' in str(result.exc_info[1])
assert '--foo' in str(result.exc_info[1])
assert '--bar' in str(result.exc_info[1])
def test_missing_grouped_options_decl_first_api(runner):
with pytest.warns(RuntimeWarning, match=r'The empty option group "Group 1"'):
@click.command()
@click.option('--hello1')
@optgroup('Group 1', help='Group 1 description')
@click.option('--hello2')
def cli(**params):
pass
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' not in result.output
assert 'Group 1 description' not in result.output
assert '--hello1' in result.output
assert '--hello2' in result.output
def test_incorrect_option_group_cls():
with pytest.raises(TypeError, match=r"must be a subclass of 'OptionGroup' class"):
@click.command()
@optgroup(cls=object)
@optgroup.option('--foo')
def cli(**params):
pass
def test_option_group_unexpected_arguments():
with pytest.raises(TypeError, match=r"'OptionGroup' constructor got an unexpected keyword argument 'oops'"):
@click.command()
@optgroup(oops=True)
@optgroup.option('--foo')
def cli(**params):
pass
def test_incorrect_grouped_option_cls():
@click.command()
@optgroup()
@optgroup.option('--foo', cls=GroupedOption)
def cli1(**params):
pass
with pytest.raises(TypeError, match=r"must be a subclass of 'GroupedOption' class"):
@click.command()
@optgroup()
@optgroup.option('--foo', cls=click.Option)
def cli2(**params):
pass
def test_option_group_name_help():
group = OptionGroup()
assert group.name == ''
assert group.help == ''
group = OptionGroup('Group Name', help='Group description')
assert group.name == 'Group Name'
assert group.help == 'Group description'
def test_basic_functionality_second_api(runner):
group1 = OptionGroup('Group 1', help='Group 1 description')
group2 = OptionGroup('Group 2', help='Group 2 description')
@click.command()
@click.option('--hello')
@group1.option('--foo1')
@group1.option('--bar1')
@click.option('--lol')
@group2.option('--foo2')
@group2.option('--bar2')
@click.option('--goodbye')
def cli(hello, foo1, bar1, lol, foo2, bar2, goodbye):
click.echo(f'{foo1},{bar1},{foo2},{bar2}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' in result.output
assert 'Group 1 description' in result.output
assert 'Group 2:' in result.output
assert 'Group 2 description' in result.output
result = runner.invoke(cli, [
'--foo1', 'foo1', '--bar1', 'bar1',
'--foo2', 'foo2', '--bar2', 'bar2'])
assert not result.exception
assert 'foo1,bar1,foo2,bar2' in result.output
def test_mix_decl_second_api():
group1 = OptionGroup('Group 1', help='Group 1 description')
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello2'\]"):
@click.command()
@click.option('--hello1')
@group1.option('--foo')
@click.option('--hello2')
@group1.option('--bar')
@click.option('--hello3')
def cli(**params):
pass
def test_required_any_option_group(runner):
group = RequiredAnyOptionGroup()
assert group.name_extra == ['required_any']
@click.command()
@optgroup(cls=RequiredAnyOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert '[required_any]' in result.output
result = runner.invoke(cli, [])
assert result.exception
assert result.exit_code == 2
assert 'At least one of the following options' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo'])
assert not result.exception
assert result.exit_code == 0
assert 'foo,None' in result.output
result = runner.invoke(cli, ['--bar', 'bar'])
assert not result.exception
assert result.exit_code == 0
assert 'None,bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar'])
assert not result.exception
assert result.exit_code == 0
assert 'foo,bar' in result.output
def test_all_option_group(runner):
group = AllOptionGroup()
assert group.name_extra == ['all_or_none']
@click.command()
@optgroup.group(cls=AllOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert '[all_or_none]' in result.output
result = runner.invoke(cli, [])
assert not result.exception
assert result.exit_code == 0
result = runner.invoke(cli, ['--foo', 'foo'])
assert result.exception
assert result.exit_code == 2
assert 'All options from' in result.output
assert 'should be specified or none should be specified' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar'])
assert not result.exception
assert result.exit_code == 0
assert 'foo,bar' in result.output
def test_required_all_option_group(runner):
group = RequiredAllOptionGroup()
assert group.name_extra == ['required_all']
@click.command()
@optgroup(cls=RequiredAllOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert '[required_all]' in result.output
result = runner.invoke(cli, [])
assert result.exception
assert result.exit_code == 2
assert 'Missing required options from' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo'])
assert result.exception
assert result.exit_code == 2
assert 'Missing required options from' in result.output
assert '--foo' not in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--bar', 'bar'])
assert result.exception
assert result.exit_code == 2
assert 'Missing required options from' in result.output
assert '--foo' in result.output
assert '--bar' not in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar'])
assert not result.exception
assert result.exit_code == 0
assert 'foo,bar' in result.output
def test_mutually_exclusive_option_group(runner):
group = MutuallyExclusiveOptionGroup()
assert group.name_extra == ['mutually_exclusive']
@click.command()
@optgroup(cls=MutuallyExclusiveOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar')
@optgroup.option('--spam')
def cli(foo, bar, spam):
click.echo(f'{foo},{bar},{spam}')
result = runner.invoke(cli, ['--help'])
assert '[mutually_exclusive]' in result.output
result = runner.invoke(cli, [])
assert not result.exception
assert result.exit_code == 0
assert 'None,None,None' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar'])
assert result.exception
assert result.exit_code == 2
assert 'Mutually exclusive options from' in result.output
assert 'cannot be used at the same time' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--spam', 'spam'])
assert result.exception
assert result.exit_code == 2
assert 'Mutually exclusive options from' in result.output
assert 'cannot be used at the same time' in result.output
assert '--foo' in result.output
assert '--spam' in result.output
result = runner.invoke(cli, ['--bar', 'bar', '--spam', 'spam'])
assert result.exception
assert result.exit_code == 2
assert 'Mutually exclusive options from' in result.output
assert 'cannot be used at the same time' in result.output
assert '--bar' in result.output
assert '--spam' in result.output
result = runner.invoke(cli, ['--foo', 'foo'])
assert not result.exception
assert result.exit_code == 0
assert 'foo,None,None' in result.output
result = runner.invoke(cli, ['--bar', 'bar'])
assert not result.exception
assert result.exit_code == 0
assert 'None,bar,None' in result.output
result = runner.invoke(cli, ['--spam', 'spam'])
assert not result.exception
assert result.exit_code == 0
assert 'None,None,spam' in result.output
def test_required_mutually_exclusive_option_group(runner):
group = RequiredMutuallyExclusiveOptionGroup()
assert group.name_extra == ['mutually_exclusive', 'required']
@click.command()
@optgroup(cls=RequiredMutuallyExclusiveOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar')
@optgroup.option('--spam')
def cli(foo, bar, spam):
click.echo(f'{foo},{bar},{spam}')
result = runner.invoke(cli, ['--help'])
assert '[mutually_exclusive, required]' in result.output
result = runner.invoke(cli, [])
assert result.exception
assert result.exit_code == 2
assert 'Missing one of the required mutually exclusive options' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
assert '--spam' in result.output
@pytest.mark.parametrize('cls', [
RequiredAnyOptionGroup,
RequiredAllOptionGroup,
MutuallyExclusiveOptionGroup,
RequiredMutuallyExclusiveOptionGroup,
])
def test_forbidden_option_attrs(cls):
with pytest.raises(TypeError, match=f"'required' attribute is not allowed for '{cls.__name__}' option `foo'"):
@click.command()
@optgroup(cls=cls)
@optgroup.option('--foo', required=True)
@optgroup.option('--bar')
def cli(foo):
pass
def test_subcommand_first_api(runner):
@click.group()
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
@cli.command()
@optgroup('Group 2', help='Group 2 description')
@optgroup.option('--foo')
@optgroup.option('--bar')
def command(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' in result.output
assert 'Group 1 description' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['command', '--help'])
assert not result.exception
assert 'Group 2:' in result.output
assert 'Group 2 description' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar', 'command', '--foo', 'foo1', '--bar', 'bar1'])
assert not result.exception
assert 'foo,bar\nfoo1,bar1' in result.output
def test_subcommand_mix_decl_first_api():
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\] option in 'cli1'"):
@click.group()
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo')
@click.option('--hello')
@optgroup.option('--bar')
def cli1(**params):
pass
@cli1.command()
@optgroup('Group 2', help='Group 2 description')
@optgroup.option('--foo')
@optgroup.option('--bar')
def command1(**params):
pass
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\] option in 'command2'"):
@click.group()
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli2(**params):
pass
@cli2.command()
@optgroup('Group 2', help='Group 2 description')
@optgroup.option('--foo')
@click.option('--hello')
@optgroup.option('--bar')
def command2(**params):
pass
def test_subcommand_second_api(runner):
group = OptionGroup('Group', help='Group description')
@click.group()
@group.option('--foo1')
@group.option('--bar1')
def cli(foo1, bar1):
click.echo(f'{foo1},{bar1}')
@cli.command()
@group.option('--foo2')
@group.option('--bar2')
def command(foo2, bar2):
click.echo(f'{foo2},{bar2}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group:' in result.output
assert 'Group description' in result.output
assert '--foo1' in result.output
assert '--bar1' in result.output
result = runner.invoke(cli, ['command', '--help'])
assert not result.exception
assert 'Group:' in result.output
assert 'Group description' in result.output
assert '--foo2' in result.output
assert '--bar2' in result.output
result = runner.invoke(cli, ['--foo1', 'foo1', '--bar1', 'bar1', 'command', '--foo2', 'foo2', '--bar2', 'bar2'])
assert not result.exception
assert 'foo1,bar1\nfoo2,bar2' in result.output
def test_group_context_second_api(runner):
group = OptionGroup('My Group')
@click.command()
@group.option('--foo1')
@group.option('--bar1')
def cli1(foo1, bar1):
click.echo(f'{foo1},{bar1}')
@click.command()
@group.option('--foo2')
@group.option('--bar2')
def cli2(foo2, bar2):
click.echo(f'{foo2},{bar2}')
result = runner.invoke(cli1, ['--help'])
assert not result.exception
assert 'My Group:' in result.output
assert '--foo1' in result.output
assert '--bar1' in result.output
result = runner.invoke(cli2, ['--help'])
assert not result.exception
assert 'My Group:' in result.output
assert '--foo2' in result.output
assert '--bar2' in result.output
result = runner.invoke(cli1, ['--foo1', 'foo1', '--bar1', 'bar1'])
assert not result.exception
assert 'foo1,bar1' in result.output
result = runner.invoke(cli2, ['--foo2', 'foo2', '--bar2', 'bar2'])
assert not result.exception
assert 'foo2,bar2' in result.output
def test_subcommand_mix_decl_second_api():
group = OptionGroup()
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\] option in 'cli1'"):
@click.group()
@group.option('--foo')
@click.option('--hello')
@group.option('--bar')
def cli1(**params):
pass
@cli1.command()
@group.option('--foo')
@group.option('--bar')
def command1(**params):
pass
with pytest.raises(TypeError, match=r"Check decorator position for \['--hello'\] option in 'command2'"):
@click.group()
@group.option('--foo')
@group.option('--bar')
def cli2(**params):
pass
@cli2.command()
@group.option('--foo')
@click.option('--hello')
@group.option('--bar')
def command2(**params):
pass
def test_command_first_api(runner):
@optgroup('Group 1')
@optgroup.option('--foo')
@optgroup.option('--bar')
@click.command()
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' in result.output
assert '--foo' in result.output
assert '--bar' in result.output
result = runner.invoke(cli, ['--foo', 'foo', '--bar', 'bar'])
assert not result.exception
assert 'foo,bar' in result.output
def test_hidden_option(runner):
@click.command()
@click.option('--hello')
@optgroup('Group 1', help='Group 1 description')
@optgroup.option('--foo1')
@optgroup.option('--bar1', hidden=True)
@click.option('--goodbye')
def cli(hello, foo1, bar1, goodbye):
click.echo(f'{foo1},{bar1}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert 'Group 1:' in result.output
assert 'Group 1 description' in result.output
assert 'bar1' not in result.output
result = runner.invoke(cli, [
'--foo1', 'foo1', '--bar1', 'bar1',
])
assert not result.exception
assert 'foo1,bar1' in result.output
with pytest.raises(
TypeError, match="'hidden' attribute is not allowed for 'RequiredAllOptionGroup' option `bar'"):
@click.command()
@optgroup(cls=RequiredAllOptionGroup)
@optgroup.option('--foo')
@optgroup.option('--bar', hidden=True)
def cli(foo, bar):
click.echo(f'{foo},{bar}')
@click.command()
@optgroup(cls=RequiredAnyOptionGroup)
@optgroup.option('--foo', hidden=True)
@optgroup.option('--bar', hidden=True)
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli,)
assert isinstance(result.exception, TypeError)
assert "Need at least one non-hidden" in str(result.exception)
@click.command()
@optgroup("Group 1", help="Group 1 description")
@optgroup.option('--foo', hidden=True)
@optgroup.option('--bar', hidden=True)
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert "Group 1" not in result.output
@click.command()
@optgroup("Group 1", help="Group 1 description", hidden=True)
@optgroup.option('--foo')
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert "Group 1" not in result.output
assert "foo" not in result.output
assert "bar" not in result.output
@click.command()
@optgroup("Group 1", help="Group 1 description", hidden=True)
@optgroup.option('--foo', hidden=False) # override hidden of group
@optgroup.option('--bar')
def cli(foo, bar):
click.echo(f'{foo},{bar}')
result = runner.invoke(cli, ['--help'])
assert not result.exception
assert "Group 1" in result.output
assert "foo" in result.output
assert "bar" not in result.output
@pytest.mark.parametrize('param_decls, options, output', [
((), ['--help'], '--help'),
(('-h', '--help'), ['-h'], '-h, --help'),
(('-h', '--help'), ['--help'], '-h, --help'),
])
def test_help_option(runner, param_decls, options, output):
@click.command()
@optgroup('Help Options')
@optgroup.help_option(*param_decls)
def cli() -> None:
click.echo('Running command.')
result = runner.invoke(cli)
assert not result.exception
assert 'Running command.' in result.output
assert 'Usage:' not in result.output
result = runner.invoke(cli, options)
assert not result.exception
assert 'Running command.' not in result.output
assert 'Usage:' in result.output
assert output in result.output
def test_wrapped_functions(runner):
def make_z():
"""A unified option interface for making a `z`."""
def decorator(f):
@optgroup.group("Group xyz")
@optgroup.option("-x", type=int)
@optgroup.option("-y", type=int)
@wraps(f)
def new_func(*args, x=0, y=0, **kwargs):
# Here we handle every detail about how to make a `z` from the given options
f(*args, z=x + y, **kwargs)
return new_func
return decorator
def make_c():
"""A unified option interface for making a `c`."""
def decorator(f):
@optgroup.group("Group abc")
@optgroup.option("-a", type=int)
@optgroup.option("-b", type=int)
@wraps(f)
def new_func(*args, a=0, b=0, **kwargs):
# Here we do the same, but for another object `c` (and many others to come)
f(*args, c=a * b, **kwargs)
return new_func
return decorator
# Here I want to create a script that has a commen UI to make a `z`.
# I want to reuse a common set of options for how to make a `z` and don't want
# to sweat the details. Also, I've decided that I also want a `c` for this script.
@click.command()
@make_z()
@make_c()
def f(z, c):
print(z, c)
# Test
result = runner.invoke(f, ["--help"])
assert "Group xyz" in result.output
assert "-x" in result.output
assert "-y" in result.output
assert "Group abc" in result.output
assert "-a" in result.output
assert "-b" in result.output
|