|
10 | 10 |
|
11 | 11 | def main() -> int: |
12 | 12 | 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", |
16 | 21 | ) |
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", |
21 | 35 | ) |
| 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 | + |
22 | 43 | args = parser.parse_args() |
23 | 44 |
|
| 45 | + if args.func is None: |
| 46 | + parser.print_help() |
| 47 | + return 1 |
| 48 | + |
24 | 49 | run_dirs = get_run_dirs(args.files) |
25 | 50 | if not run_dirs: |
26 | 51 | return 0 |
27 | 52 |
|
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) |
29 | 54 | return int(failed > 0) |
30 | 55 |
|
31 | 56 |
|
| 57 | +def add_files_nargs(parser: argparse.ArgumentParser): |
| 58 | + parser.add_argument( |
| 59 | + "files", |
| 60 | + nargs="*", |
| 61 | + type=Path, |
| 62 | + ) |
| 63 | + |
| 64 | + |
32 | 65 | def get_run_dirs(changed_files: list[Path]) -> set[Path]: |
33 | 66 | root_dirs = find_cargo_root_dirs() |
34 | 67 | run_dirs: set[Path] = set() |
@@ -60,17 +93,29 @@ def path_len(path: Path) -> int: |
60 | 93 | return len(path.parts) |
61 | 94 |
|
62 | 95 |
|
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 | + |
72 | 116 |
|
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) |
74 | 119 | return proc.returncode |
75 | 120 |
|
76 | 121 |
|
|
0 commit comments