-
-
Notifications
You must be signed in to change notification settings - Fork 363
global --verbose flag #326 done #361
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
Changes from all commits
c0bff56
8edb271
db3651e
403add3
75ba22a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||||||||||||||||||||||||||||||||||||||||
"""Command utilities for listing available lessons.""" | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
from __future__ import annotations | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||
from typing import Iterable, Optional | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
import typer | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
from state import set_verbose, verbose_active | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
app = typer.Typer(help="List available Linux command lessons.") | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
_CONTENT_DIR = Path(__file__).resolve().parents[2] / "ebook" / "en" / "content" | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
def _get_lessons() -> Iterable[Path]: | ||||||||||||||||||||||||||||||||||||||||||||
if not _CONTENT_DIR.exists(): | ||||||||||||||||||||||||||||||||||||||||||||
return [] | ||||||||||||||||||||||||||||||||||||||||||||
return sorted(_CONTENT_DIR.glob("*.md")) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
def _format_title(path: Path) -> str: | ||||||||||||||||||||||||||||||||||||||||||||
stem = path.stem | ||||||||||||||||||||||||||||||||||||||||||||
prefix, _, slug = stem.partition("-") | ||||||||||||||||||||||||||||||||||||||||||||
title = slug.replace("-", " ").strip().title() if slug else prefix.replace("-", " ") | ||||||||||||||||||||||||||||||||||||||||||||
if prefix.isdigit(): | ||||||||||||||||||||||||||||||||||||||||||||
return f"{prefix} {title}".strip() | ||||||||||||||||||||||||||||||||||||||||||||
return title | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+23
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The title formatting logic is complex and handles multiple cases in a single function. Consider splitting this into separate functions for prefix extraction and title formatting to improve readability and maintainability.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||||||||||
@app.callback(invoke_without_command=True) | ||||||||||||||||||||||||||||||||||||||||||||
def list_commands( | ||||||||||||||||||||||||||||||||||||||||||||
ctx: typer.Context, | ||||||||||||||||||||||||||||||||||||||||||||
limit: Optional[int] = typer.Option( | ||||||||||||||||||||||||||||||||||||||||||||
None, | ||||||||||||||||||||||||||||||||||||||||||||
"--limit", | ||||||||||||||||||||||||||||||||||||||||||||
"-l", | ||||||||||||||||||||||||||||||||||||||||||||
min=1, | ||||||||||||||||||||||||||||||||||||||||||||
help="Limit the number of commands displayed. Shows all when omitted.", | ||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||
verbose: bool = typer.Option( | ||||||||||||||||||||||||||||||||||||||||||||
False, | ||||||||||||||||||||||||||||||||||||||||||||
"--verbose", | ||||||||||||||||||||||||||||||||||||||||||||
"-v", | ||||||||||||||||||||||||||||||||||||||||||||
is_flag=True, | ||||||||||||||||||||||||||||||||||||||||||||
help="Enable verbose output for this command.", | ||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||
) -> None: | ||||||||||||||||||||||||||||||||||||||||||||
"""Display the available Linux command lessons.""" | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if verbose: | ||||||||||||||||||||||||||||||||||||||||||||
set_verbose(ctx, True) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
lessons = list(_get_lessons()) | ||||||||||||||||||||||||||||||||||||||||||||
total = len(lessons) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if verbose_active(ctx): | ||||||||||||||||||||||||||||||||||||||||||||
typer.echo(f"[verbose] Located {total} command lessons", err=True) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if total == 0: | ||||||||||||||||||||||||||||||||||||||||||||
typer.echo("No command lessons found.") | ||||||||||||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
limit_value = total if limit is None else min(limit, total) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if verbose_active(ctx) and limit is not None: | ||||||||||||||||||||||||||||||||||||||||||||
typer.echo(f"[verbose] Limiting output to {limit_value} entries", err=True) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
for path in lessons[:limit_value]: | ||||||||||||||||||||||||||||||||||||||||||||
typer.echo(_format_title(path)) | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
__all__ = ["app"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Shared CLI state helpers for global flags.""" | ||
|
||
from __future__ import annotations | ||
|
||
import typer | ||
|
||
_VERBOSE_KEY = "verbose" | ||
|
||
|
||
def set_verbose(ctx: typer.Context, value: bool) -> None: | ||
"""Persist the verbose flag on this context and all parents.""" | ||
current = ctx | ||
while current is not None: | ||
current.ensure_object(dict) | ||
current.obj[_VERBOSE_KEY] = value | ||
current = current.parent | ||
|
||
|
||
def verbose_active(ctx: typer.Context) -> bool: | ||
"""Check whether verbose mode is enabled anywhere up the chain.""" | ||
current = ctx | ||
while current is not None: | ||
if current.obj and current.obj.get(_VERBOSE_KEY): | ||
return True | ||
current = current.parent | ||
return False | ||
|
||
|
||
__all__ = ["set_verbose", "verbose_active"] |
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.
This line contains duplicated logic for replacing dashes with spaces. Extract this transformation into a variable or helper function to avoid repetition.
Copilot uses AI. Check for mistakes.