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

feat: argparse before running app for CLI args #2

Merged
merged 1 commit into from
Apr 5, 2024
Merged
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
19 changes: 18 additions & 1 deletion antur/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
"""Main module for the Antur application."""

import argparse

from . import __version__
from .app import AnturApp


def parse_args() -> argparse.Namespace:
"""Parse the command line arguments."""
parser = argparse.ArgumentParser(
description="Antur allows you to browse sitemaps from your command line.",
epilog=f"antur v{__version__}",
)
parser.add_argument("url", nargs="?", help="The URL of a sitemap to start with.")

parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
return parser.parse_args()


def main() -> None:
"""Run the Antur application."""
app = AnturApp()
args = parse_args()

app = AnturApp(args.url)
app.run()


Expand Down
10 changes: 8 additions & 2 deletions antur/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ class AnturApp(App):

show_markdown = reactive(False)

def __init__(self: "AnturApp", url: str | None = None) -> None:
"""Initialize the Antur application."""
super().__init__()

self.url = url

def compose(self: "AnturApp") -> ComposeResult:
"""Compose the layout of the app."""
yield Header(False)
yield SearchBar()
yield SearchBar(self.url)
with Vertical(id="contents"):
with Vertical(id="main"):
yield SitemapTree()
yield SitemapTree(self.url)
yield NodeInfo()
yield Footer()

Expand Down
6 changes: 4 additions & 2 deletions antur/widgets/search_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ def is_url(value: str) -> bool:
class SearchBar(Widget):
"""Search bar widget."""

def __init__(self: "SearchBar", *args: tuple, **kwargs: dict) -> None:
def __init__(self: "SearchBar", url: str | None = None, *args: tuple, **kwargs: dict) -> None:
"""Initialize the search bar."""
super().__init__(*args, **kwargs)

self.url = url

def compose(self: "SearchBar") -> ComposeResult:
"""Compose the search bar."""
with Horizontal(id="buttons-container"):
yield Input(
None,
self.url,
"Sitemap URL",
id="search-bar",
validate_on=["changed", "submitted"],
Expand Down
4 changes: 3 additions & 1 deletion antur/widgets/sitemap_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ class SitemapTree(Widget):

BINDINGS: ClassVar = [("o", "open", "Open the selected item in the browser.")]

def __init__(self: "SitemapTree", *args: tuple, **kwargs: dict) -> None:
def __init__(self: "SitemapTree", url: str | None = None, *args: tuple, **kwargs: dict) -> None:
"""Initialize the sitemap tree."""
super().__init__(*args, **kwargs)

self.target = url

def compose(self: "SitemapTree") -> ComposeResult:
"""Compose the sitemap tree."""
with ScrollableContainer():
Expand Down