Hi everyone,
Here is a quick share about how to get argparse to work with a launch file. To start create a function in your launch file or python node file that contains the following.
import sys
import argparse
def get_args():
""" Get arguments for individual tb3 deployment. """
parser = argparse.ArgumentParser(
description="TB3 Gesture Controller Launch File."
)
# Required arguments
parser.add_argument("-n", "--number",
action="store",
type=int,
required=False,
help="Add TB3 node namespace number.",
default=0)
return parser.parse_args(sys.argv[4:])
As you can see on the last line, you must index the sys.argv[4:] to the fourth value. If you don’t and just pass sys.argv, you get the following if you attempt to run ros2 launch pkg_name some_launch.launch.py -n 0:
['/opt/ros/dashing/bin/ros2', 'launch', 'pkg_name', 'some_launch.launch.py', '-n', '0']
The first 4 values passed to sys.argv are the original commands that you run. If you don’t want to import the sys library, you can use the rclpy library and replace the value in parser.parse_args() with:
return parser.parse_args(rclpy.sys.argv[4:])
In your generate_launch_description() function, you can get your parsed launch or node arguments with:
def generate_launch_description():
...
input_args = get_args()
...
Hope that helps!