aboutsummaryrefslogtreecommitdiffstats
path: root/src/nvd/cli/main.py
blob: fc90fe5a92e8269b796837210af51e352dc3ff57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Main CLI entry point."""

import sys

import typer
from rich.console import Console
from rich.panel import Panel
from rich.text import Text

from .commands import config, cpe, cve

# Enable -h as shorthand for --help
CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}

app = typer.Typer(
    name="nvdb",
    help="NVD API CLI - Query the US National Vulnerability Database",
    add_completion=False,
    rich_markup_mode="rich",
    context_settings=CONTEXT_SETTINGS,
)

# Console for stderr (help messages, examples)
console = Console(stderr=True)


def show_examples() -> None:
    """Show usage examples."""
    examples = Text()
    examples.append("Quick Examples:\n\n", style="bold cyan")
    examples.append("  # Get a specific CVE\n", style="dim")
    examples.append("  nvdb cve get CVE-2021-44228\n\n", style="green")
    examples.append("  # Search for critical vulnerabilities\n", style="dim")
    examples.append("  nvdb cve search --severity CRITICAL --limit 10\n\n", style="green")
    examples.append("  # Search CVEs in CISA KEV catalog\n", style="dim")
    examples.append("  nvdb cve search --has-kev --limit 20\n\n", style="green")
    examples.append("  # Search CPEs\n", style="dim")
    examples.append("  nvdb cpe search --keyword 'windows 10'\n\n", style="green")
    examples.append("  # Configure API key\n", style="dim")
    examples.append("  nvdb config set-api-key YOUR_API_KEY\n\n", style="green")
    examples.append("Get help for specific commands:\n", style="bold yellow")
    examples.append("  nvdb cve --help\n", style="blue")
    examples.append("  nvdb cpe --help\n", style="blue")
    examples.append("  nvdb config --help\n", style="blue")

    console.print(Panel(examples, title="[bold]nvdb - NVD API CLI[/bold]", border_style="blue"))


@app.callback(invoke_without_command=True)
def main_callback(ctx: typer.Context) -> None:
    """Main callback to show examples when no command is provided."""
    if ctx.invoked_subcommand is None and len(sys.argv) == 1:
        show_examples()
        raise typer.Exit()


# Register command groups
app.add_typer(cve.app, name="cve", help="CVE (vulnerability) commands")
app.add_typer(cpe.app, name="cpe", help="CPE (product) commands")
app.add_typer(config.app, name="config", help="Configuration commands")


@app.command()
def version() -> None:
    """Show version information."""
    from .. import __version__

    console.print(f"nvdb-py version: {__version__}")


if __name__ == "__main__":
    app()