Python argparse with --help flag

asked 2020-01-29 14:32:34 -0500

mhubii gravatar image

As explained by zmk5 in ROS Discourse, we can argparse to launch files. However with the --help flag

ros2 launch file.launch.py --help

the thread gets stuck. Could one solve this?

edit retag flag offensive close merge delete

Comments

What do you mean by the thread getting stuck? Does it output an error?

zmk5 gravatar image zmk5  ( 2020-01-29 14:54:35 -0500 )edit

steps to reproduce. Take this launch file

import sys
import argparse

from launch import LaunchDescription

def generate_launch_description():
    parser = argparse.ArgumentParser()
    args = parser.parse_args(sys.argv[4:])

    return LaunchDescription()

move it to demo_nodes_cpp

sudo mv test.launch.py /opt/ros/dashing/share/demo_nodes_cpp/launch

execute it

ros2 launch demo_nodes_cpp test.launch.py --help

The expected behavior is that --help calls exit, which in this case is somehow locked, and one has to ctrl+c the way out

mhubii gravatar image mhubii  ( 2020-01-30 05:11:09 -0500 )edit

I believe you need to add a description="some argument description" argument to your argparse.ArgumentParser().

Something like this:

parser = argparse.ArgumentParser(description="some description")

zmk5 gravatar image zmk5  ( 2020-01-30 09:10:29 -0500 )edit

no thats unrelated. The error reads stuff like

File "/usr/lib/python3.6/threading.py", line 1294, in _shutdown
t.join()
KeyboardInterrupt
Task was destroyed but it is pending!
File "/usr/lib/python3.6/argparse.py", line 1034, in __call__ parser.exit()
File "/usr/lib/python3.6/argparse.py", line 2389, in exit _sys.exit(status)
Segmentation fault (core dumped)
mhubii gravatar image mhubii  ( 2020-01-30 09:32:20 -0500 )edit