-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat(dispatcher): constrained global arguments #220
Draft
lengau
wants to merge
5
commits into
main
Choose a base branch
from
constrained-global-args
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02deb38
feat(dispatcher): constrained global arguments
lengau 499ed14
test(dispatcher): GlobalArgument tests
lengau 8316c77
Merge branch 'main' into constrained-global-args
lengau ca5e5c3
Merge branch 'main' into constrained-global-args
lengau 9761aec
Merge branch 'main' into constrained-global-args
lengau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program; if not, write to the Free Software Foundation, | ||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
"""Utility functions for craft_cli.""" | ||
from typing import Sequence | ||
|
||
|
||
def humanise_list(values: Sequence[str], conjunction: str = "and") -> str: | ||
"""Convert a collection of values to a string that lists the values. | ||
:param values: The values to turn into a list | ||
:param conjunction: The conjunction to use between the last two items | ||
:returns: A string containing the list phrase ready to insert into a sentence. | ||
""" | ||
if not values: | ||
return "" | ||
start = ", ".join(values[:-1]) | ||
return f"{start} {conjunction} {values[-1]}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ RTD | |
subdirectories | ||
subtree | ||
subfolders | ||
utils | ||
UI | ||
VM | ||
YAML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License version 3 as published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public License | ||
# along with this program; if not, write to the Free Software Foundation, | ||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
"""Unit tests for utility functions.""" | ||
import re | ||
|
||
import pytest | ||
import pytest_check | ||
from hypothesis import given, strategies | ||
|
||
from craft_cli import utils | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"values", | ||
[ | ||
[], | ||
["one-thing"], | ||
["two", "things"], | ||
], | ||
) | ||
@pytest.mark.parametrize("conjunction", ["and", "or", "but not"]) | ||
def test_humanise_list_success(values, conjunction): | ||
actual = utils.humanise_list(values, conjunction) | ||
|
||
pytest_check.equal(actual.count(","), max((len(values) - 2, 0))) | ||
with pytest_check.check: | ||
assert actual == "" or conjunction in actual | ||
for value in values: | ||
pytest_check.is_in(value, actual) | ||
|
||
|
||
@given( | ||
values=strategies.lists(strategies.text()), | ||
conjunction=strategies.text(), | ||
) | ||
def test_humanise_list_fuzzy(values, conjunction): | ||
actual = utils.humanise_list(values, conjunction) | ||
|
||
pytest_check.greater_equal(actual.count(","), max((len(values) - 2, 0))) | ||
with pytest_check.check: | ||
assert actual == "" or conjunction in actual | ||
for value in values: | ||
pytest_check.is_in(value, actual) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
choices
also exists in argparse, willvalidator
andcase_sensitive
options increase the gap between global argument handling and command-specific argument handling instead of reducing it? Ideally we should have an uniform way to handle all arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validator
is equivalent to argparse'stype
- I can rename if you'd like.case_sensitive
is different, but is there for backwards compatibility in the way we handle--verbosity
, which was previously case-insensitive due to special handling. Looking over it again, I wonder if I could just run the validator before limiting choices and make a specialverbosity_validator
function that returns the correct emitter mode.