|
1 | 1 | import sys
|
2 |
| -import time |
| 2 | +import json |
| 3 | +import argparse |
3 | 4 | from . import parse, ValidationError
|
4 | 5 |
|
5 |
| -if __name__ == "__main__": |
6 |
| - args = [x for x in sys.argv[1:] if not x.startswith("-")] |
7 |
| - flags = [x for x in sys.argv[1:] if x.startswith("-")] |
8 |
| - |
9 |
| - filename = args[0] |
10 |
| - start_time = time.time() |
11 |
| - |
12 |
| - with_progress = "--progress" in flags |
13 |
| - json_output = "--json" in flags |
14 |
| - only_header = "--header-only" in flags |
15 |
| - validate_data_only = "--data-only" in flags |
16 |
| - |
| 6 | +def main(): |
| 7 | + parser = argparse.ArgumentParser(description="Parse and validate STEP file.") |
| 8 | + parser.add_argument("filename", help="The STEP file to validate.") |
| 9 | + parser.add_argument("--progress", action="store_true", help="Show progress during validation.") |
| 10 | + parser.add_argument("--json", action="store_true", help="Output errors in JSON format.") |
| 11 | + parser.add_argument("--only-header", action="store_true", help="Validate only the header section.") |
| 12 | + parser.add_argument("--only-data", action="store_true", help="Validate only the data section.") |
17 | 13 |
|
18 |
| - # Sanity check: can't use both at once |
19 |
| - if only_header and validate_data_only: |
20 |
| - print("Cannot use both --header-only and --data-only at the same time", file=sys.stderr) |
| 14 | + args = parser.parse_args() |
| 15 | + if args.only_header and args.only_data: |
| 16 | + print("Cannot use both --only-header and --only-data at the same time", file=sys.stderr) |
21 | 17 | sys.exit(2)
|
22 |
| - |
| 18 | + |
23 | 19 | try:
|
24 | 20 | parse(
|
25 |
| - filename=filename, |
26 |
| - with_progress=with_progress, |
27 |
| - with_tree=False, |
28 |
| - only_header=only_header, |
29 |
| - validate_data_only=validate_data_only, |
| 21 | + filename=args.filename, |
| 22 | + with_progress = args.progress, |
| 23 | + with_tree = False, |
| 24 | + only_header=args.only_header, |
| 25 | + validate_data_only = args.only_data |
30 | 26 | )
|
31 |
| - if not json_output: |
| 27 | + if not args.json: |
32 | 28 | print("Valid", file=sys.stderr)
|
33 | 29 | exit(0)
|
34 | 30 | except ValidationError as exc:
|
35 |
| - if not json_output: |
| 31 | + if not args.json: |
36 | 32 | print(exc, file=sys.stderr)
|
37 | 33 | else:
|
38 |
| - import sys |
39 |
| - import json |
40 |
| - |
41 | 34 | json.dump(exc.asdict(), sys.stdout)
|
42 | 35 | exit(1)
|
| 36 | + |
| 37 | +if __name__ == '__main__': |
| 38 | + main() |
0 commit comments