Skip to content

Commit

Permalink
Add: Add shell completion for all pontos CLI
Browse files Browse the repository at this point in the history
Implement shell completion via shtab and cleanup CLI arguments.
  • Loading branch information
bjoernricks committed Feb 5, 2024
1 parent 511f1db commit e105f3f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 62 deletions.
7 changes: 5 additions & 2 deletions pontos/changelog/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pathlib import Path
from typing import NoReturn, Optional, Sequence

import shtab

from pontos.changelog.conventional_commits import ChangelogBuilder
from pontos.errors import PontosError
from pontos.terminal.null import NullTerminal
Expand All @@ -26,14 +28,15 @@ def parse_args(args: Optional[Sequence[str]] = None) -> Namespace:
" text from conventional commits between the current and next release.",
prog="pontos-changelog",
)
shtab.add_argument_to(parser)

parser.add_argument(
"--config",
"-C",
type=Path,
help="Optional. Conventional commits config file (toml), including "
"conventions. If not provided defaults are used.",
)
).complete = shtab.FILE # type: ignore[attr-defined]

parser.add_argument(
"--project",
Expand Down Expand Up @@ -83,7 +86,7 @@ def parse_args(args: Optional[Sequence[str]] = None) -> Namespace:
"-o",
type=Path,
help="Write changelog to this file.",
)
).complete = shtab.FILE # type: ignore[attr-defined]

parser.add_argument(
"--quiet",
Expand Down
62 changes: 32 additions & 30 deletions pontos/github/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from pathlib import Path
from typing import List, Optional

import shtab

from pontos.enum import enum_choice, enum_type
from pontos.github.cmds import (
create_pull_request,
create_release,
Expand All @@ -32,25 +35,20 @@ def from_env(name: str) -> str:
return os.environ.get(name, name)


def get_repository_type(rtype: str) -> RepositoryType:
return RepositoryType[rtype]


def parse_args(
args: Optional[List[str]] = None,
) -> Namespace:
"""
Parsing args for Pontos GitHub
Arguments:
args The program arguments passed by exec
term The terminal to print
args The program arguments passed by exec
"""

parser = ArgumentParser(
description="Greenbone GitHub API.",
)

shtab.add_argument_to(parser)
parser.add_argument(
"--quiet",
"-q",
Expand All @@ -62,20 +60,21 @@ def parse_args(
"--log-file",
dest="log_file",
type=str,
help="Acivate logging using the given file path",
)
help="Activate logging using the given file path",
).complete = shtab.FILE # type: ignore[attr-defined]

subparsers = parser.add_subparsers(
title="subcommands",
description="valid subcommands",
required=True,
help="additional help",
description="Valid subcommands",
help="Additional help",
dest="command",
)

# create a PR from command line
pr_parser = subparsers.add_parser(
"pull-request", aliases=["pr", "PR", "pullrequest"]
"pull-request",
aliases=["pr", "PR", "pullrequest"],
help="Pull request related commands",
)

pr_parser.set_defaults(func=pull_request)
Expand All @@ -95,8 +94,8 @@ def parse_args(
title="method",
dest="pr_method",
metavar="name",
description="valid pull request method",
help="pull request method",
description="Valid pull request method",
help="Pull request method",
required=True,
)

Expand Down Expand Up @@ -136,7 +135,7 @@ def parse_args(
)

update_pr_parser = pr_subparsers.add_parser(
"update", help="update Pull Request"
"update", help="Update Pull Request"
)

update_pr_parser.set_defaults(pr_func=update_pull_request)
Expand Down Expand Up @@ -166,7 +165,7 @@ def parse_args(

# get files
file_status_parser = subparsers.add_parser(
"file-status", aliases=["status", "FS"]
"file-status", aliases=["status", "FS"], help="File status"
)

file_status_parser.set_defaults(func=file_status)
Expand All @@ -182,7 +181,7 @@ def parse_args(
file_status_parser.add_argument(
"-s",
"--status",
choices=FileStatus,
choices=enum_choice(FileStatus),
default=[FileStatus.ADDED, FileStatus.MODIFIED],
nargs="+",
help="What file status should be returned. Default: %(default)s",
Expand Down Expand Up @@ -212,7 +211,9 @@ def parse_args(
)

# labels
label_parser = subparsers.add_parser("labels", aliases=["L"])
label_parser = subparsers.add_parser(
"labels", aliases=["L"], help="Issue/pull Request label handling"
)

label_parser.set_defaults(func=labels)

Expand Down Expand Up @@ -243,7 +244,9 @@ def parse_args(
)

# orga-repos
repos_parser = subparsers.add_parser("repos", aliases=["R"])
repos_parser = subparsers.add_parser(
"repos", aliases=["R"], help="Repository information"
)

repos_parser.set_defaults(func=repos)

Expand All @@ -262,8 +265,8 @@ def parse_args(

repos_parser.add_argument(
"--type",
choices=RepositoryType,
type=get_repository_type,
choices=enum_choice(RepositoryType),
type=enum_type(RepositoryType),
default=RepositoryType.PUBLIC,
help=(
"Define the type of repositories that should be covered. "
Expand All @@ -279,7 +282,7 @@ def parse_args(

# create a release from command line
re_parser = subparsers.add_parser(
"release", aliases=["re", "RE", "release"]
"release", aliases=["re", "RE", "release"], help="Release commands"
)

re_parser.set_defaults(func=release)
Expand All @@ -299,7 +302,7 @@ def parse_args(
title="method",
dest="re_method",
metavar="name",
description="valid release method",
description="Valid release method",
help="Release method",
required=True,
)
Expand Down Expand Up @@ -353,7 +356,9 @@ def parse_args(
)

# Create a tag from command line
tag_parser = subparsers.add_parser("tag", aliases=["tag", "TAG"])
tag_parser = subparsers.add_parser(
"tag", aliases=["tag", "TAG"], help="Tag commands"
)

tag_parser.set_defaults(func=tag)

Expand All @@ -372,7 +377,7 @@ def parse_args(
title="method",
dest="tag_method",
metavar="name",
description="valid tag method",
description="Valid tag method",
help="Release method",
required=True,
)
Expand Down Expand Up @@ -425,7 +430,4 @@ def parse_args(
" YYYY-MM-DDTHH:MM:SSZ."
),
)

parsed_args = parser.parse_args(args)

return parsed_args
return parser.parse_args(args)
2 changes: 1 addition & 1 deletion pontos/github/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ async def repos(terminal: Terminal, args: Namespace):
exists = await api.organizations.exists(args.orga)
if not exists:
terminal.error(
f"PR {args.orga} is not existing or authorisation failed."
f"Organization {args.orga} is not existing or authorisation failed."
)
sys.exit(1)

Expand Down
1 change: 1 addition & 0 deletions pontos/github/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def main():
child_parser = ArgumentParser(parents=[parser])
run_add_arguments_function(module, child_parser)
args = child_parser.parse_args()

token = args.token
timeout = args.timeout

Expand Down
3 changes: 3 additions & 0 deletions pontos/github/script/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import os
from argparse import ArgumentParser

import shtab

from pontos.github.api.helper import DEFAULT_TIMEOUT

GITHUB_TOKEN = "GITHUB_TOKEN"
Expand All @@ -19,6 +21,7 @@ def create_parser() -> ArgumentParser:
A new ArgumentParser instance add the default arguments
"""
parser = ArgumentParser(add_help=False)
shtab.add_argument_to(parser)
parser.add_argument(
"--token",
default=os.environ.get(GITHUB_TOKEN),
Expand Down
3 changes: 3 additions & 0 deletions pontos/nvd/cpe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Callable

import httpx
import shtab

from pontos.nvd.cpe.api import CPEApi

Expand All @@ -34,6 +35,7 @@ async def query_cpes(args: Namespace) -> None:

def cpe_main() -> None:
parser = ArgumentParser()
shtab.add_argument_to(parser)
parser.add_argument("--token", help="API key to use for querying.")
parser.add_argument(
"cpe_name_id", metavar="CPE Name ID", help="UUID of the CPE"
Expand All @@ -44,6 +46,7 @@ def cpe_main() -> None:

def cpes_main() -> None:
parser = ArgumentParser()
shtab.add_argument_to(parser)
parser.add_argument("--token", help="API key to use for querying.")
parser.add_argument(
"--cpe-match-string",
Expand Down
3 changes: 3 additions & 0 deletions pontos/nvd/cve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Callable

import httpx
import shtab

from pontos.nvd.cve.api import CVEApi

Expand Down Expand Up @@ -36,6 +37,7 @@ async def query_cve(args: Namespace) -> None:

def cves_main() -> None:
parser = ArgumentParser()
shtab.add_argument_to(parser)
parser.add_argument("--token", help="API key to use for querying.")
parser.add_argument(
"--keywords",
Expand Down Expand Up @@ -73,6 +75,7 @@ def cves_main() -> None:

def cve_main() -> None:
parser = ArgumentParser()
shtab.add_argument_to(parser)
parser.add_argument("--token", help="API key to use for querying.")
parser.add_argument("cve_id", metavar="CVE-ID", help="ID of the CVE")

Expand Down
3 changes: 3 additions & 0 deletions pontos/nvd/cve_changes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import asyncio
from argparse import ArgumentParser, Namespace

import shtab

from pontos.nvd.cve_changes.api import CVEChangesApi

__all__ = ("CVEChangesApi",)
Expand All @@ -23,6 +25,7 @@ async def query_changes(args: Namespace) -> None:

def parse_args() -> Namespace:
parser = ArgumentParser()
shtab.add_argument_to(parser)
parser.add_argument("--token", help="API key to use for querying.")
parser.add_argument("--cve-id", help="Get changes for a specific CVE")
parser.add_argument(
Expand Down
Loading

0 comments on commit e105f3f

Please sign in to comment.