|
1 | | -from mindee.v1.commands.cli_parser import MindeeParser |
| 1 | +import logging |
| 2 | +import sys |
| 3 | + |
| 4 | +from mindee.v2.commands.cli_parser import MindeeParser |
| 5 | + |
| 6 | +_V1_DASHV_PRODUCTS = ("custom", "generated") |
| 7 | + |
| 8 | + |
| 9 | +def _find_v1_dashv_boundary(argv: list[str]) -> int | None: |
| 10 | + """Locate the position after which ``-v`` belongs to V1 ``custom`` / |
| 11 | + ``generated``. |
| 12 | +
|
| 13 | + These two V1 products register ``-v/--version``; tokens at or beyond |
| 14 | + the returned index must be left alone by the verbose pre-scan. |
| 15 | + """ |
| 16 | + for i, token in enumerate(argv): |
| 17 | + if token == "v1" and i + 1 < len(argv) and argv[i + 1] in _V1_DASHV_PRODUCTS: |
| 18 | + return i + 2 |
| 19 | + return None |
| 20 | + |
| 21 | + |
| 22 | +def _extract_verbose_level(argv: list[str]) -> tuple[int, list[str]]: |
| 23 | + """Pre-scan ``argv`` for ``--verbose`` / ``-v`` flags. |
| 24 | +
|
| 25 | + Mirrors ``mindee-api-dotnet``'s ``args.Contains("--verbose")`` check: |
| 26 | + the flag is consumed before argparse runs so it can appear anywhere |
| 27 | + on the command line. |
| 28 | +
|
| 29 | + * ``--verbose`` is consumed at any position (no conflict). |
| 30 | + * ``-v`` is consumed at any position *except* after a ``v1 custom`` |
| 31 | + or ``v1 generated`` invocation, where it is the V1 product's own |
| 32 | + ``--version`` option. |
| 33 | +
|
| 34 | + :returns: ``(level, remaining_argv)`` where ``level`` counts the number |
| 35 | + of recognized verbose-flag occurrences. |
| 36 | + """ |
| 37 | + level = 0 |
| 38 | + remaining: list[str] = [] |
| 39 | + v1_dashv_start = _find_v1_dashv_boundary(argv) |
| 40 | + for i, token in enumerate(argv): |
| 41 | + if token == "--verbose": |
| 42 | + level += 1 |
| 43 | + continue |
| 44 | + if token == "-v" and (v1_dashv_start is None or i < v1_dashv_start): |
| 45 | + level += 1 |
| 46 | + continue |
| 47 | + remaining.append(token) |
| 48 | + return level, remaining |
| 49 | + |
| 50 | + |
| 51 | +def _configure_logging(verbose_level: int) -> None: |
| 52 | + """Set the ``mindee`` logger level based on the verbose count.""" |
| 53 | + if verbose_level <= 0: |
| 54 | + return |
| 55 | + target = logging.INFO if verbose_level == 1 else logging.DEBUG |
| 56 | + logging.getLogger("mindee").setLevel(target) |
| 57 | + logging.getLogger().setLevel(target) |
2 | 58 |
|
3 | 59 |
|
4 | 60 | def main() -> None: |
5 | | - """Run the Command Line Interface.""" |
| 61 | + """Run the Command Line Interface. |
| 62 | +
|
| 63 | + The unified ``mindee`` binary exposes V2 inference commands and the |
| 64 | + ``search-models`` utility at the root, with all V1 product commands |
| 65 | + wrapped under a ``v1`` subcommand — mirroring the canonical |
| 66 | + ``mindee-api-dotnet`` CLI. |
| 67 | +
|
| 68 | + Pass ``--verbose`` (or ``-v``) to enable diagnostic logging; repeat |
| 69 | + the flag (``--verbose --verbose``) for debug-level output. |
| 70 | + """ |
| 71 | + verbose_level, argv = _extract_verbose_level(sys.argv[1:]) |
| 72 | + _configure_logging(verbose_level) |
| 73 | + sys.argv = [sys.argv[0], *argv] |
6 | 74 | parser = MindeeParser() |
7 | | - parser.call_parse() |
| 75 | + sys.exit(parser.call_parse() or 0) |
0 commit comments