|
| 1 | +""" |
| 2 | +CodeClone — AST and CFG-based code clone detector for Python |
| 3 | +focused on architectural duplication. |
| 4 | +
|
| 5 | +Copyright (c) 2026 Den Rozhnovskiy |
| 6 | +Licensed under the MIT License. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +from typing import cast |
| 13 | + |
| 14 | +from . import ui_messages as ui |
| 15 | + |
| 16 | + |
| 17 | +class _HelpFormatter(argparse.ArgumentDefaultsHelpFormatter): |
| 18 | + def _get_help_string(self, action: argparse.Action) -> str: |
| 19 | + if action.dest == "cache_path": |
| 20 | + return action.help or "" |
| 21 | + return cast(str, super()._get_help_string(action)) |
| 22 | + |
| 23 | + |
| 24 | +def build_parser(version: str) -> argparse.ArgumentParser: |
| 25 | + ap = argparse.ArgumentParser( |
| 26 | + prog="codeclone", |
| 27 | + description="AST and CFG-based code clone detector for Python.", |
| 28 | + formatter_class=_HelpFormatter, |
| 29 | + ) |
| 30 | + ap.add_argument( |
| 31 | + "--version", |
| 32 | + action="version", |
| 33 | + version=ui.version_output(version), |
| 34 | + help=ui.HELP_VERSION, |
| 35 | + ) |
| 36 | + |
| 37 | + core_group = ap.add_argument_group("Target") |
| 38 | + core_group.add_argument( |
| 39 | + "root", |
| 40 | + nargs="?", |
| 41 | + default=".", |
| 42 | + help=ui.HELP_ROOT, |
| 43 | + ) |
| 44 | + |
| 45 | + tune_group = ap.add_argument_group("Analysis Tuning") |
| 46 | + tune_group.add_argument( |
| 47 | + "--min-loc", |
| 48 | + type=int, |
| 49 | + default=15, |
| 50 | + help=ui.HELP_MIN_LOC, |
| 51 | + ) |
| 52 | + tune_group.add_argument( |
| 53 | + "--min-stmt", |
| 54 | + type=int, |
| 55 | + default=6, |
| 56 | + help=ui.HELP_MIN_STMT, |
| 57 | + ) |
| 58 | + tune_group.add_argument( |
| 59 | + "--processes", |
| 60 | + type=int, |
| 61 | + default=4, |
| 62 | + help=ui.HELP_PROCESSES, |
| 63 | + ) |
| 64 | + tune_group.add_argument( |
| 65 | + "--cache-path", |
| 66 | + dest="cache_path", |
| 67 | + metavar="FILE", |
| 68 | + default=None, |
| 69 | + help=ui.HELP_CACHE_PATH, |
| 70 | + ) |
| 71 | + tune_group.add_argument( |
| 72 | + "--cache-dir", |
| 73 | + dest="cache_path", |
| 74 | + metavar="FILE", |
| 75 | + default=None, |
| 76 | + help=ui.HELP_CACHE_DIR_LEGACY, |
| 77 | + ) |
| 78 | + tune_group.add_argument( |
| 79 | + "--max-cache-size-mb", |
| 80 | + type=int, |
| 81 | + default=50, |
| 82 | + metavar="MB", |
| 83 | + help=ui.HELP_MAX_CACHE_SIZE_MB, |
| 84 | + ) |
| 85 | + |
| 86 | + ci_group = ap.add_argument_group("Baseline & CI/CD") |
| 87 | + ci_group.add_argument( |
| 88 | + "--baseline", |
| 89 | + default="codeclone.baseline.json", |
| 90 | + help=ui.HELP_BASELINE, |
| 91 | + ) |
| 92 | + ci_group.add_argument( |
| 93 | + "--max-baseline-size-mb", |
| 94 | + type=int, |
| 95 | + default=5, |
| 96 | + metavar="MB", |
| 97 | + help=ui.HELP_MAX_BASELINE_SIZE_MB, |
| 98 | + ) |
| 99 | + ci_group.add_argument( |
| 100 | + "--update-baseline", |
| 101 | + action="store_true", |
| 102 | + help=ui.HELP_UPDATE_BASELINE, |
| 103 | + ) |
| 104 | + ci_group.add_argument( |
| 105 | + "--fail-on-new", |
| 106 | + action="store_true", |
| 107 | + help=ui.HELP_FAIL_ON_NEW, |
| 108 | + ) |
| 109 | + ci_group.add_argument( |
| 110 | + "--fail-threshold", |
| 111 | + type=int, |
| 112 | + default=-1, |
| 113 | + metavar="MAX_CLONES", |
| 114 | + help=ui.HELP_FAIL_THRESHOLD, |
| 115 | + ) |
| 116 | + ci_group.add_argument( |
| 117 | + "--ci", |
| 118 | + action="store_true", |
| 119 | + help=ui.HELP_CI, |
| 120 | + ) |
| 121 | + |
| 122 | + out_group = ap.add_argument_group("Reporting") |
| 123 | + out_group.add_argument( |
| 124 | + "--html", |
| 125 | + dest="html_out", |
| 126 | + metavar="FILE", |
| 127 | + help=ui.HELP_HTML, |
| 128 | + ) |
| 129 | + out_group.add_argument( |
| 130 | + "--json", |
| 131 | + dest="json_out", |
| 132 | + metavar="FILE", |
| 133 | + help=ui.HELP_JSON, |
| 134 | + ) |
| 135 | + out_group.add_argument( |
| 136 | + "--text", |
| 137 | + dest="text_out", |
| 138 | + metavar="FILE", |
| 139 | + help=ui.HELP_TEXT, |
| 140 | + ) |
| 141 | + out_group.add_argument( |
| 142 | + "--no-progress", |
| 143 | + action="store_true", |
| 144 | + help=ui.HELP_NO_PROGRESS, |
| 145 | + ) |
| 146 | + out_group.add_argument( |
| 147 | + "--no-color", |
| 148 | + action="store_true", |
| 149 | + help=ui.HELP_NO_COLOR, |
| 150 | + ) |
| 151 | + out_group.add_argument( |
| 152 | + "--quiet", |
| 153 | + action="store_true", |
| 154 | + help=ui.HELP_QUIET, |
| 155 | + ) |
| 156 | + out_group.add_argument( |
| 157 | + "--verbose", |
| 158 | + action="store_true", |
| 159 | + help=ui.HELP_VERBOSE, |
| 160 | + ) |
| 161 | + return ap |
0 commit comments