Skip to content

Commit 8ff9b78

Browse files
committed
args for hooks
1 parent cf06f9d commit 8ff9b78

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

pre_commit_rust.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,58 @@
1010

1111
def main() -> int:
1212
parser = argparse.ArgumentParser()
13-
parser.add_argument(
14-
"action",
15-
choices=ACTIONS,
13+
parser.set_defaults(func=None)
14+
sps = parser.add_subparsers(dest="cmd")
15+
16+
sp = sps.add_parser("fmt")
17+
sp.add_argument(
18+
"--config",
19+
type=str,
20+
help="Comma-separated key=value config pairs for rustfmt",
1621
)
17-
parser.add_argument(
18-
"files",
19-
nargs="*",
20-
type=Path,
22+
sp.set_defaults(func=run_fmt)
23+
add_files_nargs(sp)
24+
25+
sp = sps.add_parser("check")
26+
sp.add_argument(
27+
"--features",
28+
type=str,
29+
help="Space or comma-separated list of features to check",
30+
)
31+
sp.add_argument(
32+
"--all-features",
33+
action="store_true",
34+
help="Activate all available features",
2135
)
36+
sp.set_defaults(func=run_check)
37+
add_files_nargs(sp)
38+
39+
sp = sps.add_parser("clippy")
40+
sp.set_defaults(func=run_clippy)
41+
add_files_nargs(sp)
42+
2243
args = parser.parse_args()
2344

45+
if args.func is None:
46+
parser.print_help()
47+
return 1
48+
2449
run_dirs = get_run_dirs(args.files)
2550
if not run_dirs:
2651
return 0
2752

28-
failed = sum(run_action(args.action, d) for d in run_dirs)
53+
failed = sum(args.func(args, d) for d in run_dirs)
2954
return int(failed > 0)
3055

3156

57+
def add_files_nargs(parser: argparse.ArgumentParser):
58+
parser.add_argument(
59+
"files",
60+
nargs="*",
61+
type=Path,
62+
)
63+
64+
3265
def get_run_dirs(changed_files: list[Path]) -> set[Path]:
3366
root_dirs = find_cargo_root_dirs()
3467
run_dirs: set[Path] = set()
@@ -60,17 +93,29 @@ def path_len(path: Path) -> int:
6093
return len(path.parts)
6194

6295

63-
def run_action(action: str, directory: Path) -> int:
64-
if action == "fmt":
65-
cmd = "cargo fmt --"
66-
elif action == "check":
67-
cmd = "cargo check"
68-
elif action == "clippy":
69-
cmd = "cargo clippy -- -D warnings"
70-
else:
71-
raise ValueError(f"Invalid action {action!r}, expected one of: {ACTIONS}")
96+
def run_fmt(args: argparse.Namespace, directory: Path) -> int:
97+
cmd = "cargo fmt --"
98+
if args.config:
99+
cmd += f" --config {args.config}"
100+
return run_action(cmd, directory)
101+
102+
103+
def run_check(args: argparse.Namespace, directory: Path) -> int:
104+
cmd = "cargo check"
105+
if args.features is not None:
106+
cmd += f" --features={args.features}"
107+
if args.all_features:
108+
cmd += f" --all-features"
109+
return run_action(cmd, directory)
110+
111+
112+
def run_clippy(_: argparse.Namespace, directory: Path) -> int:
113+
cmd = "cargo clippy -- -D warnings"
114+
return run_action(cmd, directory)
115+
72116

73-
proc = subprocess.run(f"{cmd}", cwd=directory, shell=True)
117+
def run_action(cmd: str, directory: Path) -> int:
118+
proc = subprocess.run(cmd, cwd=directory, shell=True)
74119
return proc.returncode
75120

76121

0 commit comments

Comments
 (0)