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
|
from typing import cast
from unittest.mock import Mock
import click
from click import Context
from pytest import fixture
import cloup
from cloup import Command
@fixture()
def dummy_ctx():
"""Useful for testing methods that needs a Context but don't actually use it."""
dummy = Mock(spec_set=Context(Command('name', params=[])))
dummy.command.__class__ = Command
return dummy
@fixture()
def sample_cmd() -> Command:
"""Useful for testing constraints against a variety of parameter kinds.
Parameters have names that should make easy to remember their "kind"
without the need for looking up this code."""
@cloup.command()
# Optional arguments
@click.argument('arg1', required=False)
@click.argument('arg2', required=False)
# Plain options without default
@cloup.option('--str-opt')
@cloup.option('--int-opt', type=int)
@cloup.option('--bool-opt', type=bool)
# Flags
@cloup.option('--flag / --no-flag')
@cloup.option('--flag2', is_flag=True)
# Options with default
@cloup.option('--def1', default=1)
@cloup.option('--def2', default=2)
# Options that take a tuple
@cloup.option('--tuple', nargs=2, type=int)
# Options that can be specified multiple times
@cloup.option('--mul1', type=int, multiple=True)
@cloup.option('--mul2', type=int, multiple=True)
def f(**kwargs):
print('It works')
return cast(Command, f)
|