Skip to content

Commit

Permalink
Make the -v and -q flags incremental (#35)
Browse files Browse the repository at this point in the history
The -v and -q flags can be provided multiple times to increase/decrease verbosity by an arbitrary amount relative to the starting point.

This makes it is possible to specify normal verbosity, even if a default verbosity is configured, and will make it easier to support more verbosity levels than just -1, 0 and 1 in the future.
  • Loading branch information
Artemis21 authored Aug 24, 2021
1 parent 582f5a6 commit cb17707
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
19 changes: 13 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -393,18 +393,25 @@ You can also specify an env file (with bashlike syntax) to load for all tasks li
Default command verbosity
-------------------------

You can configure the verbosity level for poe commands by passing `--quiet` or
`--verbose` on the CLI. If you want to change the default verbosity level for
all commands, you can use the :toml:`tool.poe.verbose` option in pyproject.toml
like so:
You can alter the verbosity level for poe commands by passing :bash:`--quiet` /
:bash:`-q` (which decreases verbosity) or :bash:`--verbose` / :bash:`-v` (which
increases verbosity) on the CLI.

If you want to change the default verbosity level for all commands, you can use
the :toml:`tool.poe.verbose` option in pyproject.toml like so:

.. code-block:: toml
[tool.poe]
verbosity = -1
:toml:`-1` is equivalent to :bash:`--quiet` and :toml:`1` is equivalent to
:bash:`--verbose`. :toml:`0` is the default.
:toml:`-1` is the quietest and :toml:`1` is the most verbose. :toml:`0` is the
default.

Note that the command line arguments are incremental: :bash:`-q` subtracts one
from the default verbosity, and :bash:`-v` adds one. So setting the default
verbosity to :toml:`-1` and passing :bash:`-v -v` on the command line is
equivalent to setting the verbosity to :toml:`0` and just passing :bash:`-v`.

Run poe from anywhere
---------------------
Expand Down
2 changes: 1 addition & 1 deletion poethepoet/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.0b4"
__version__ = "0.11.0b5"
28 changes: 12 additions & 16 deletions poethepoet/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,22 @@ def build_parser(self) -> argparse.ArgumentParser:
help="Print the version and exit",
)

verbosity_group = parser.add_mutually_exclusive_group()
verbosity_group.add_argument(
parser.add_argument(
"-v",
"--verbose",
dest="verbosity",
action="store_const",
metavar="verbose_mode",
dest="increase_verbosity",
action="count",
default=0,
const=1,
help="More console spam",
help="Increase command output (repeatable)",
)
verbosity_group.add_argument(

parser.add_argument(
"-q",
"--quiet",
dest="verbosity",
action="store_const",
metavar="quiet_mode",
const=-1,
help="Less console spam",
dest="decrease_verbosity",
action="count",
default=0,
help="Decrease command output (repeatable)",
)

parser.add_argument(
Expand Down Expand Up @@ -129,12 +126,11 @@ def build_parser(self) -> argparse.ArgumentParser:
def parse_args(self, cli_args: Sequence[str]):
self.parser = self.build_parser()
self.args = self.parser.parse_args(cli_args)
self.verbosity: int = self["verbosity"] or 0
self.verbosity: int = self["increase_verbosity"] - self["decrease_verbosity"]
self._color.with_colors(self.args.ansi)

def set_default_verbosity(self, default_verbosity: int):
if not self.verbosity:
self.verbosity = default_verbosity
self.verbosity += default_verbosity

def print_help(
self,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "poethepoet"
version = "0.11.0b4"
version = "0.11.0b5"
description = "A task runner that works well with poetry."
authors = ["Nat Noordanus <[email protected]>"]
readme = "README.rst"
Expand Down
9 changes: 7 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ def scripts_project_path():


@pytest.fixture
def verbosity_default_project_path():
return PROJECT_ROOT.joinpath("tests", "fixtures", "verbosity_default")
def low_verbosity_project_path():
return PROJECT_ROOT.joinpath("tests", "fixtures", "low_verbosity")


@pytest.fixture
def high_verbosity_project_path():
return PROJECT_ROOT.joinpath("tests", "fixtures", "high_verbosity")


@pytest.fixture(scope="function")
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/high_verbosity/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.poe]
verbosity = 1

[tool.poe.tasks]
test = "echo Hello there!"
File renamed without changes.
27 changes: 23 additions & 4 deletions tests/test_poe_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,34 @@ def test_setting_global_env_vars(run_poe_subproc, is_windows):
assert result.stderr == ""


def test_setting_default_verbosity(run_poe_subproc, verbosity_default_project_path):
result = run_poe_subproc("test", cwd=verbosity_default_project_path,)
def test_setting_default_verbosity(run_poe_subproc, low_verbosity_project_path):
result = run_poe_subproc("test", cwd=low_verbosity_project_path,)
assert result.capture == ""
assert result.stdout == "Hello there!\n"
assert result.stderr == ""


def test_override_default_verbosity(run_poe_subproc, verbosity_default_project_path):
result = run_poe_subproc("-v", "test", cwd=verbosity_default_project_path,)
def test_override_default_verbosity(run_poe_subproc, low_verbosity_project_path):
result = run_poe_subproc("-v", "-v", "test", cwd=low_verbosity_project_path,)
assert result.capture == "Poe => echo Hello there!\n"
assert result.stdout == "Hello there!\n"
assert result.stderr == ""


def test_partially_decrease_verbosity(run_poe_subproc, high_verbosity_project_path):
result = run_poe_subproc("-q", "test", cwd=high_verbosity_project_path,)
assert result.capture == "Poe => echo Hello there!\n"
assert result.stdout == "Hello there!\n"
assert result.stderr == ""


def test_decrease_verbosity(run_poe_subproc, dummy_project_path, is_windows):
result = run_poe_subproc("-q", "part1", cwd=dummy_project_path,)
assert result.capture == ""
assert result.stderr == ""
if is_windows:
# On Windows, "echo 'Hello'" results in "'Hello'".
assert result.stdout == "'Hello'\n"
else:
# On UNIX, "echo 'Hello'" results in just "Hello".
assert result.stdout == "Hello\n"

0 comments on commit cb17707

Please sign in to comment.