diff --git a/src/linktools/__main__.py b/src/linktools/__main__.py index c88c0fe3..35a5c415 100644 --- a/src/linktools/__main__.py +++ b/src/linktools/__main__.py @@ -33,6 +33,7 @@ from rich import get_console from rich.tree import Tree +from linktools.cli.argparse import add_subparsers from ._environ import environ from .cli import BaseCommand, walk_commands from .decorator import cached_property @@ -91,13 +92,13 @@ def command_infos(self) -> Dict[CategoryInfo, List[CommandInfo]]: return command_infos def init_arguments(self, parser: ArgumentParser) -> None: - catalog_parsers = parser.add_subparsers(metavar="CATALOG", required=True) + catalog_parsers = add_subparsers(parser, metavar="CATALOG", required=True) for category_info, command_infos in self.command_infos.items(): catalog_parser = catalog_parsers.add_parser( category_info.name, help=category_info.description ) - command_parsers = catalog_parser.add_subparsers(metavar="COMMAND", required=True) + command_parsers = add_subparsers(catalog_parser, metavar="COMMAND", required=True) for command_info in command_infos: command_parser = command_info.command.create_argument_parser( command_info.name, diff --git a/src/linktools/cli/__init__.py b/src/linktools/cli/__init__.py index 9708bfd9..90fdf07f 100644 --- a/src/linktools/cli/__init__.py +++ b/src/linktools/cli/__init__.py @@ -27,6 +27,8 @@ /_==__==========__==_ooo__ooo=_/' /___________," """ +from . import argparse + from .command import \ LogCommandMixin, \ BaseCommand, CommandError, \ diff --git a/src/linktools/cli/argparse.py b/src/linktools/cli/argparse.py index 17d2a20f..0bfaa40f 100644 --- a/src/linktools/cli/argparse.py +++ b/src/linktools/cli/argparse.py @@ -122,3 +122,9 @@ def wrapper(o): raise ValueError("value not in range %s-%s" % (min, max)) return wrapper + + +def add_subparsers(parser: argparse.ArgumentParser, **kwargs): + if sys.version_info < (3, 7): + kwargs.pop("required", None) + return parser.add_subparsers(**kwargs) diff --git a/src/linktools/cli/command.py b/src/linktools/cli/command.py index 6388c2ab..8902ac0c 100644 --- a/src/linktools/cli/command.py +++ b/src/linktools/cli/command.py @@ -42,7 +42,7 @@ import rich from rich import get_console -from .argparse import BooleanOptionalAction +from .argparse import BooleanOptionalAction, add_subparsers from .._environ import BaseEnviron, environ from .._logging import LogHandler from ..decorator import cached_property @@ -275,7 +275,7 @@ def add_subcommands(self: "BaseCommand", parser: ArgumentParser = None, target: parser = parser or self._argument_parser target = target or self - subparsers = parser.add_subparsers(metavar="COMMAND", help="Command Help", required=True) + subparsers = add_subparsers(parser, metavar="COMMAND", help="Command Help", required=True) for _, command_info in SubCommandMixin._find_command_infos(target): command_actions = [] command_func = getattr(target, command_info.func.__name__) diff --git a/src/linktools/cli/commands/common/tools.py b/src/linktools/cli/commands/common/tools.py index 3d428bda..248416eb 100755 --- a/src/linktools/cli/commands/common/tools.py +++ b/src/linktools/cli/commands/common/tools.py @@ -33,7 +33,7 @@ from linktools import ToolError from linktools.cli import BaseCommand -from linktools.cli.argparse import KeyValueAction +from linktools.cli.argparse import KeyValueAction, add_subparsers from linktools.utils import DownloadError @@ -61,7 +61,7 @@ def init_arguments(self, parser: ArgumentParser) -> None: help="execute tools as a daemon") tool_names = sorted([tool.name for tool in iter(self.environ.tools)]) - subparsers = parser.add_subparsers(metavar="TOOL", help=f"{{{','.join(tool_names)}}}", required=True) + subparsers = add_subparsers(parser, metavar="TOOL", help=f"{{{','.join(tool_names)}}}", required=True) for tool_name in tool_names: tool_parser = subparsers.add_parser(tool_name, prefix_chars=chr(0)) tool_parser.add_argument("tool_args", metavar="args", nargs="...") diff --git a/src/linktools/utils/_utils.py b/src/linktools/utils/_utils.py index d9e7e2af..127c7e61 100755 --- a/src/linktools/utils/_utils.py +++ b/src/linktools/utils/_utils.py @@ -34,6 +34,7 @@ import random import re import socket +import sys import threading import time import typing @@ -406,20 +407,21 @@ def get_path(root_path: str, *paths: [str], create: bool = False, create_parent: return target_path -@typing.overload -def read_file(path: str) -> bytes: ... +if sys.version_info >= (3, 8): + @typing.overload + def read_file(path: str) -> bytes: ... -@typing.overload -def read_file(path: str, binary: typing.Literal[True]) -> bytes: ... + @typing.overload + def read_file(path: str, binary: typing.Literal[True]) -> bytes: ... -@typing.overload -def read_file(path: str, binary: typing.Literal[False]) -> str: ... + @typing.overload + def read_file(path: str, binary: typing.Literal[False]) -> str: ... -@typing.overload -def read_file(path: str, binary: bool) -> Union[str, bytes]: ... + @typing.overload + def read_file(path: str, binary: bool) -> Union[str, bytes]: ... def read_file(path: str, binary: bool = True) -> Union[str, bytes]: