From cb2ed2442e8c33a68a2a850c9fca25674f85672b Mon Sep 17 00:00:00 2001 From: frederik-uni <147479464+frederik-uni@users.noreply.github.com> Date: Sat, 30 Nov 2024 22:56:10 +0100 Subject: [PATCH] why does the order matter? --- manga_translator/__main__.py | 5 ++++- manga_translator/args.py | 39 +++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/manga_translator/__main__.py b/manga_translator/__main__.py index 4edeec4ff..9e4970ba1 100644 --- a/manga_translator/__main__.py +++ b/manga_translator/__main__.py @@ -4,6 +4,7 @@ from argparse import Namespace from manga_translator import Config +from manga_translator.args import parser, reparse from .manga_translator import ( set_main_logger, load_dictionary, apply_dictionary, ) @@ -85,7 +86,9 @@ async def dispatch(args: Namespace): args = None init_logging() try: - args = parser.parse_args() + args, unknown = parser.parse_known_args() + parser.add_argument_group() + args = Namespace(**{**vars(args), **vars(reparse(unknown))}) set_log_level(level=logging.DEBUG if args.verbose else logging.INFO) logger = get_logger(args.mode) set_main_logger(logger) diff --git a/manga_translator/args.py b/manga_translator/args.py index 72a717a1c..5b5a3ec83 100644 --- a/manga_translator/args.py +++ b/manga_translator/args.py @@ -76,22 +76,37 @@ def _format_action_invocation(self, action: argparse.Action) -> str: else: return super()._format_action_invocation(action) +def general_parser(g_parser): + g_parser.add_argument('-v', '--verbose', action='store_true', + help='Print debug info and save intermediate images in result folder') + g_parser.add_argument('--attempts', default=0, type=int, + help='Retry attempts on encountered error. -1 means infinite times.') + g_parser.add_argument('--ignore-errors', action='store_true', help='Skip image on encountered error.') + g_parser.add_argument('--model-dir', default=None, type=dir_path, + help='Model directory (by default ./models in project root)') + g = g_parser.add_mutually_exclusive_group() + g.add_argument('--use-gpu', action='store_true', help='Turn on/off gpu (auto switch between mps and cuda)') + g.add_argument('--use-gpu-limited', action='store_true', help='Turn on/off gpu (excluding offline translator)') + g_parser.add_argument('--font-path', default='', type=file_path, help='Path to font file') + g_parser.add_argument('--pre-dict', default=None, type=file_path, help='Path to the pre-translation dictionary file') + g_parser.add_argument('--post-dict', default=None, type=file_path, + help='Path to the post-translation dictionary file') + g_parser.add_argument('--kernel-size', default=3, type=int, + help='Set the convolution kernel size of the text erasure area to completely clean up text residues') + + + +def reparse(arr: list): + p = argparse.ArgumentParser(prog='manga_translator', + description='Seamlessly translate mangas into a chosen language', + formatter_class=HelpFormatter) + general_parser(p) + return p.parse_args(arr) parser = argparse.ArgumentParser(prog='manga_translator', description='Seamlessly translate mangas into a chosen language', formatter_class=HelpFormatter) +general_parser(parser) subparsers = parser.add_subparsers(dest='mode', required=True, help='Mode of operation') -parser.add_argument('-v', '--verbose', action='store_true', help='Print debug info and save intermediate images in result folder') -parser.add_argument('--attempts', default=0, type=int, help='Retry attempts on encountered error. -1 means infinite times.') -parser.add_argument('--ignore-errors', action='store_true', help='Skip image on encountered error.') -parser.add_argument('--model-dir', default=None, type=dir_path, help='Model directory (by default ./models in project root)') -g = parser.add_mutually_exclusive_group() -g.add_argument('--use-gpu', action='store_true', help='Turn on/off gpu (auto switch between mps and cuda)') -g.add_argument('--use-gpu-limited', action='store_true', help='Turn on/off gpu (excluding offline translator)') -parser.add_argument('--font-path', default='', type=file_path, help='Path to font file') -parser.add_argument('--pre-dict', default=None, type=file_path, help='Path to the pre-translation dictionary file') -parser.add_argument('--post-dict', default=None, type=file_path, help='Path to the post-translation dictionary file') -parser.add_argument('--kernel-size', default=3, type=int, help='Set the convolution kernel size of the text erasure area to completely clean up text residues') - # Batch mode parser_batch = subparsers.add_parser('local', help='Run in batch translation mode') parser_batch.add_argument('-i', '--input', required=True, type=path, nargs='+', help='Path to an image folder')