Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore empty to choice list #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions click_params/miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ def __init__(self, separator: str = ',', ignore_empty: bool = False):
class ChoiceListParamType(ListParamType):
name = 'choice list'

def __init__(self, choices: Sequence[str], separator: str = ',', case_sensitive: bool = True):
super().__init__(click.Choice(choices, case_sensitive=case_sensitive), separator)
def __init__(
self,
choices: Sequence[str],
separator: str = ',',
case_sensitive: bool = True,
ignore_empty: bool = False
):
super().__init__(
click.Choice(choices, case_sensitive=case_sensitive),
separator=separator,
ignore_empty=ignore_empty
)


class UUIDListParamType(ListParamType):
Expand Down
2 changes: 1 addition & 1 deletion docs/usage/miscellaneous.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Your list of preferred fruits:

## ChoiceListParamType

Signature: `ChoiceListParamType(choices: Sequence[str], separator: str = ',', case_sensitive: bool = True)`
Signature: `ChoiceListParamType(choices: Sequence[str], separator: str = ',', case_sensitive: bool = True, ignore_empty: bool = False)`

Converts given string to a list of choices.

Expand Down
12 changes: 8 additions & 4 deletions tests/test_miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ def cli(values):
assert_equals_output(0, expected_output, result)


@pytest.mark.parametrize("param_type", [
StringListParamType, MacAddressListParamType, UUIDListParamType, DateTimeListParamType
@pytest.mark.parametrize(("param_type", "extra_args"), [
(StringListParamType, []),
(ChoiceListParamType, [click.Choice(["a", "b", "c"])]),
(MacAddressListParamType, []),
(UUIDListParamType, []),
(DateTimeListParamType, [])
])
def test_miscellaneous_list_param_types_ignore_empty_string(param_type):
misc_list_type = param_type(ignore_empty=True)
def test_miscellaneous_list_param_types_ignore_empty_string(param_type, extra_args):
misc_list_type = param_type(*extra_args, ignore_empty=True)

assert misc_list_type.convert("", None, None) == []

Expand Down