Skip to content

Commit 206281b

Browse files
committed
Parse cli args with argparse
Signed-off-by: Tyler Weaver <tyler@picknik.ai>
1 parent 673b663 commit 206281b

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

generate_parameter_library_py/generate_parameter_library_py/generate_cpp_header.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3030
# POSSIBILITY OF SUCH DAMAGE.
3131

32+
import argparse
3233
import yaml
3334
from yaml.parser import ParserError
3435
from yaml.scanner import ScannerError
@@ -987,24 +988,13 @@ def __str__(self):
987988
code = j2_template.render(data, trim_blocks=True)
988989
return code
989990

990-
def run(self):
991-
if 3 > len(sys.argv) > 4:
992-
raise compile_error(
993-
"generate_parameter_library_py expects three input argument: output_file, "
994-
"yaml file path, [validate include header]"
995-
)
996-
997-
param_gen_directory = sys.argv[0].split("/")
998-
param_gen_directory = "".join(x + "/" for x in param_gen_directory[:-1])
999-
if param_gen_directory[-1] != "/":
1000-
param_gen_directory += "/"
1001-
1002-
output_file = sys.argv[1]
991+
def run(self, args):
992+
output_file = args.output_cpp_header_file
1003993
output_dir = os.path.dirname(output_file)
1004994
if not os.path.isdir(output_dir):
1005995
os.makedirs(output_dir)
1006996

1007-
yaml_file = sys.argv[2]
997+
yaml_file = args.input_yaml_file
1008998
with open(yaml_file) as f:
1009999
try:
10101000
docs = yaml.load_all(f, Loader=yaml.Loader)
@@ -1021,17 +1011,25 @@ def run(self):
10211011
self.namespace = list(doc.keys())[0]
10221012
self.parse_dict(self.namespace, doc[self.namespace], [])
10231013

1024-
if len(sys.argv) > 3:
1025-
self.user_validation_file = sys.argv[3]
1014+
self.user_validation_file = args.validate_header
10261015

10271016
code = str(self)
10281017
with open(output_file, "w") as f:
10291018
f.write(code)
10301019

10311020

1021+
def parse_args():
1022+
parser = argparse.ArgumentParser()
1023+
parser.add_argument("output_cpp_header_file")
1024+
parser.add_argument("input_yaml_file")
1025+
parser.add_argument("validate_header", nargs="?", default="")
1026+
return parser.parse_args()
1027+
1028+
10321029
def main():
1030+
args = parse_args()
10331031
gen_param_struct = GenerateCode()
1034-
gen_param_struct.run()
1032+
gen_param_struct.run(args)
10351033

10361034

10371035
if __name__ == "__main__":

generate_parameter_library_py/generate_parameter_library_py/test/YAML_parse_error_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
from unittest.mock import patch
2020
import sys
2121
import os
22-
from generate_parameter_library_py.main import GenerateCode, YAMLSyntaxError
22+
from generate_parameter_library_py.generate_cpp_header import (
23+
GenerateCode,
24+
YAMLSyntaxError,
25+
parse_args,
26+
)
2327
from ament_index_python.packages import get_package_share_path
2428

2529

@@ -30,8 +34,9 @@ def set_up(yaml_test_file):
3034
testargs = [sys.argv[0], "/tmp/admittance_controller.h", full_file_path]
3135

3236
with patch.object(sys, "argv", testargs):
37+
args = parse_args()
3338
gen_param_struct = GenerateCode()
34-
gen_param_struct.run()
39+
gen_param_struct.run(args)
3540

3641

3742
# class TestViewValidCodeGen(unittest.TestCase):

0 commit comments

Comments
 (0)