-
Notifications
You must be signed in to change notification settings - Fork 384
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
Closed
Manjunath3155
wants to merge
5
commits into
The-DevOps-Daily:main
from
Manjunath3155:global-verbose-flag
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0bff56
global --verbose flag #326 done
Manjunath3155 8edb271
Clean up whitespace in hello.py
Manjunath3155 db3651e
Remove unnecessary blank line in test_cli.py
Manjunath3155 403add3
Add import statement for additional functionality
Manjunath3155 75ba22a
Merge branch 'main' into global-verbose-flag
Manjunath3155 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,12 +1,33 @@ | ||
| import typer | ||
|
|
||
| from commands import hello | ||
| from commands import hello, listing | ||
| from state import set_verbose | ||
|
|
||
| # Create the root CLI app | ||
| app = typer.Typer(help="101 Linux Commands CLI 🚀") | ||
|
|
||
|
|
||
| @app.callback() | ||
| def main( | ||
| ctx: typer.Context, | ||
| verbose: bool = typer.Option( | ||
| False, | ||
| "--verbose", | ||
| "-v", | ||
| is_flag=True, | ||
| help="Enable verbose debug output for all commands.", | ||
| ), | ||
| ) -> None: | ||
| """Configure application-wide options before subcommands run.""" | ||
|
|
||
| set_verbose(ctx, verbose) | ||
| if verbose: | ||
| typer.echo("[verbose] Verbose mode enabled", err=True) | ||
|
|
||
|
|
||
| # Register subcommands | ||
| app.add_typer(hello.app, name="hello") | ||
| app.add_typer(listing.app, name="list") | ||
|
|
||
| if __name__ == "__main__": | ||
| app() |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,29 @@ | ||
| import typer | ||
|
|
||
| from state import set_verbose, verbose_active | ||
|
|
||
|
|
||
| app = typer.Typer(help="Hello command group") | ||
|
|
||
|
|
||
| @app.command() | ||
| def greet(name: str = "World"): | ||
| def greet( | ||
| ctx: typer.Context, | ||
| name: str = typer.Option("World", "--name", "-n", help="Name to greet."), | ||
| verbose: bool = typer.Option( | ||
| False, | ||
| "--verbose", | ||
| "-v", | ||
| is_flag=True, | ||
| help="Enable verbose output for this command.", | ||
| ), | ||
| ) -> None: | ||
| """Say hello to someone.""" | ||
|
|
||
| if verbose: | ||
| set_verbose(ctx, True) | ||
|
|
||
| if verbose_active(ctx): | ||
| typer.echo(f"[verbose] Preparing greeting for {name}", err=True) | ||
|
|
||
| typer.echo(f"Hello, {name}!") |
This file contains hidden or 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,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
|
||||||||||||||||||||||||||||||||||||||||||||
| 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 | |
| def _extract_prefix_and_slug(stem: str) -> tuple[str, str]: | |
| prefix, _, slug = stem.partition("-") | |
| return prefix, slug | |
| def _format_title_from_parts(prefix: str, slug: str) -> str: | |
| title = slug.replace("-", " ").strip().title() if slug else prefix.replace("-", " ") | |
| if prefix.isdigit(): | |
| return f"{prefix} {title}".strip() | |
| return title | |
| def _format_title(path: Path) -> str: | |
| stem = path.stem | |
| prefix, slug = _extract_prefix_and_slug(stem) | |
| return _format_title_from_parts(prefix, slug) |
This file contains hidden or 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 @@ | ||
| """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"] |
This file contains hidden or 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
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.
This line contains duplicated logic for replacing dashes with spaces. Extract this transformation into a variable or helper function to avoid repetition.