-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from gardenlinux/real-cli
Provide a real script with multiple sub commands
- Loading branch information
Showing
8 changed files
with
253 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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,83 @@ | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
import argparse | ||
import dataclasses | ||
from collections.abc import ( | ||
Callable, | ||
Iterable, | ||
) | ||
|
||
|
||
@dataclasses.dataclass | ||
class _ActionWrapper: | ||
args: tuple | ||
kw: dict | ||
|
||
|
||
class Cli: | ||
parser: argparse.ArgumentParser | ||
subparsers: argparse._SubParsersAction | ||
|
||
def __init__(self) -> None: | ||
self.parser = argparse.ArgumentParser( | ||
allow_abbrev=False, | ||
prog='glvd', | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
self.subparsers = self.parser.add_subparsers( | ||
help='sub-command help', | ||
) | ||
|
||
def add_argument(self, *args, **kw) -> _ActionWrapper: | ||
return _ActionWrapper(args, kw) | ||
|
||
def register( | ||
self, | ||
name: str, | ||
arguments: Iterable[_ActionWrapper], | ||
usage: str = '%(prog)s', | ||
epilog: str | None = None, | ||
) -> Callable: | ||
parser_main = argparse.ArgumentParser( | ||
allow_abbrev=False, | ||
prog=f'glvd.{name}', | ||
usage=usage, | ||
epilog=epilog, | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
parser_sub = self.subparsers.add_parser( | ||
name=name, | ||
usage=usage, | ||
epilog=epilog, | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
|
||
for p in (parser_main, parser_sub): | ||
for w in arguments: | ||
p.add_argument(*w.args, **w.kw) | ||
|
||
def wrap(func: Callable) -> Callable: | ||
parser_sub.set_defaults(func=func) | ||
|
||
def run() -> None: | ||
args = parser_main.parse_args() | ||
func(**vars(args)) | ||
|
||
return run | ||
|
||
return wrap | ||
|
||
def main(self) -> None: | ||
args = self.parser.parse_args() | ||
v = vars(args) | ||
func = v.pop('func', None) | ||
if func: | ||
func(**v) | ||
else: | ||
self.parser.print_help() | ||
|
||
|
||
cli = Cli() |
This file contains 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,22 @@ | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
from . import cli | ||
|
||
# Import to register all the commands | ||
from . import ( # noqa: F401 | ||
combine_all, | ||
combine_deb, | ||
ingest_debsec, | ||
ingest_debsrc, | ||
ingest_nvd, | ||
) | ||
|
||
|
||
def main() -> None: | ||
cli.main() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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 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 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 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.