Skip to content

Commit 34e1d81

Browse files
aswatermanTimmmm
andauthored
Use argparse for argument parsing (#331)
This is more maintainable and scalable and gives better errors and a nice `--help` message. The interface is compatible with the previous version. Co-authored-by: Tim Hutt <[email protected]>
1 parent f4aa35e commit 34e1d81

File tree

1 file changed

+60
-29
lines changed

1 file changed

+60
-29
lines changed

parse.py

+60-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python3
22

3+
import argparse
34
import json
45
import logging
56
import pprint
6-
import sys
77

88
from c_utils import make_c
99
from chisel_utils import make_chisel
@@ -21,67 +21,98 @@
2121
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT)
2222

2323

24-
def main():
25-
print(f"Running with args : {sys.argv}")
26-
27-
extensions = sys.argv[1:]
28-
29-
targets = {
30-
"-c",
31-
"-chisel",
32-
"-go",
33-
"-latex",
34-
"-pseudo",
35-
"-rust",
36-
"-spinalhdl",
37-
"-sverilog",
38-
}
39-
40-
extensions = [ext for ext in extensions if ext not in targets]
41-
print(f"Extensions selected : {extensions}")
42-
43-
include_pseudo = "-pseudo" in sys.argv[1:]
44-
24+
def generate_extensions(
25+
extensions: list[str],
26+
include_pseudo: bool,
27+
c: bool,
28+
chisel: bool,
29+
spinalhdl: bool,
30+
sverilog: bool,
31+
rust: bool,
32+
go: bool,
33+
latex: bool,
34+
):
4535
instr_dict = create_inst_dict(extensions, include_pseudo)
4636
instr_dict = dict(sorted(instr_dict.items()))
4737

4838
with open("instr_dict.json", "w", encoding="utf-8") as outfile:
4939
json.dump(add_segmented_vls_insn(instr_dict), outfile, indent=2)
5040

51-
if "-c" in sys.argv[1:]:
41+
if c:
5242
instr_dict_c = create_inst_dict(
5343
extensions, False, include_pseudo_ops=emitted_pseudo_ops
5444
)
5545
instr_dict_c = dict(sorted(instr_dict_c.items()))
5646
make_c(instr_dict_c)
5747
logging.info("encoding.out.h generated successfully")
5848

59-
if "-chisel" in sys.argv[1:]:
49+
if chisel:
6050
make_chisel(instr_dict)
6151
logging.info("inst.chisel generated successfully")
6252

63-
if "-spinalhdl" in sys.argv[1:]:
53+
if spinalhdl:
6454
make_chisel(instr_dict, True)
6555
logging.info("inst.spinalhdl generated successfully")
6656

67-
if "-sverilog" in sys.argv[1:]:
57+
if sverilog:
6858
make_sverilog(instr_dict)
6959
logging.info("inst.sverilog generated successfully")
7060

71-
if "-rust" in sys.argv[1:]:
61+
if rust:
7262
make_rust(instr_dict)
7363
logging.info("inst.rs generated successfully")
7464

75-
if "-go" in sys.argv[1:]:
65+
if go:
7666
make_go(instr_dict)
7767
logging.info("inst.go generated successfully")
7868

79-
if "-latex" in sys.argv[1:]:
69+
if latex:
8070
make_latex_table()
8171
logging.info("instr-table.tex generated successfully")
8272
make_priv_latex_table()
8373
logging.info("priv-instr-table.tex generated successfully")
8474

8575

76+
def main():
77+
parser = argparse.ArgumentParser(description="Generate RISC-V constants headers")
78+
parser.add_argument(
79+
"-pseudo", action="store_true", help="Include pseudo-instructions"
80+
)
81+
parser.add_argument("-c", action="store_true", help="Generate output for C")
82+
parser.add_argument(
83+
"-chisel", action="store_true", help="Generate output for Chisel"
84+
)
85+
parser.add_argument(
86+
"-spinalhdl", action="store_true", help="Generate output for SpinalHDL"
87+
)
88+
parser.add_argument(
89+
"-sverilog", action="store_true", help="Generate output for SystemVerilog"
90+
)
91+
parser.add_argument("-rust", action="store_true", help="Generate output for Rust")
92+
parser.add_argument("-go", action="store_true", help="Generate output for Go")
93+
parser.add_argument("-latex", action="store_true", help="Generate output for Latex")
94+
parser.add_argument(
95+
"extensions",
96+
nargs="*",
97+
help="Extensions to use. This is a glob of the rv_.. files, e.g. 'rv*' will give all extensions.",
98+
)
99+
100+
args = parser.parse_args()
101+
102+
print(f"Extensions selected : {args.extensions}")
103+
104+
generate_extensions(
105+
args.extensions,
106+
args.pseudo,
107+
args.c,
108+
args.chisel,
109+
args.spinalhdl,
110+
args.sverilog,
111+
args.rust,
112+
args.go,
113+
args.latex,
114+
)
115+
116+
86117
if __name__ == "__main__":
87118
main()

0 commit comments

Comments
 (0)