ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

[easy] launch file syntax: how to pass quotation mark to a python script

asked 2019-11-26 06:54:20 -0500

june2473 gravatar image

updated 2019-11-27 05:26:08 -0500

So, I have this launch file:

<launch>
  <node pkg="some_pkg" type="some_type.py" name="some_name" output="screen" args="--text &quot;some string with spaces&quot;">
</launch>

My python script takes flag --text and then string argument with spaces between quotation marks, like this:

my_python_script.py --text 'some text with spaces'

I have read that in xml I should use &quot; or &apos; but this does not work, I am getting error error: unrecognized arguments. I also tried using simple quotes - does not work.

what should I write in launch file to pass string argument with spaces to a python script?


Edit: I launch this launch file:

<launch>
  <arg name="use_sound"       default="false" doc="Use google voice as sound indication"/>
  <node if="$(arg use_sound)" pkg="sound_indication" type="makeSoundRequest.py" name="makeSoundRequest" output="screen" args="--text 'please say something' --label mode1">
  </node>
</launch>

and terminal output is:

nvidia@tegra-ubuntu:~$ roslaunch turtlebot3_slam_3d sound.launch use_sound:=true
... logging to /home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/roslaunch-tegra-ubuntu-11900.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://JETSON_HOSTNAME:45970/

SUMMARY
========

PARAMETERS
 * /rosdistro: kinetic
 * /rosversion: 1.12.14

NODES
  /
    makeSoundRequest (sound_indication/makeSoundRequest.py)

ROS_MASTER_URI=http://10.42.0.243:11311

process[makeSoundRequest-1]: started with pid [11925]
usage: makeSoundRequest.py [-h] [--text TEXT] [--label [L]] [-p]
makeSoundRequest.py: error: unrecognized arguments: __name:=makeSoundRequest __log:=/home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/makeSoundRequest-1.log
[makeSoundRequest-1] process has died [pid 11925, exit code 2, cmd /home/nvidia/catkin_ws/src/sound_indication/scripts/makeSoundRequest.py --text please say something --label mode1 __name:=makeSoundRequest __log:=/home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/makeSoundRequest-1.log].
log file: /home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/makeSoundRequest-1*.log
all processes on machine have died, roslaunch will exit
shutting down processing monitor...
... shutting down processing monitor complete
done

and log file written there does not exist.

edit retag flag offensive close merge delete

Comments

Have you tried using simple quotes instead ?

Delb gravatar image Delb  ( 2019-11-26 07:27:23 -0500 )edit

yes, was not working

june2473 gravatar image june2473  ( 2019-11-26 22:31:40 -0500 )edit

was not working

Do you have the same error (unrecognized arguments) or nothing happens ? Also can you provide your code (or just the part that is dealing with the argument) ?

Delb gravatar image Delb  ( 2019-11-27 02:18:36 -0500 )edit

yes, the same problem.

here is that python code that takes arguments, where one of them should be in quotes:

if __name__ == "__main__":
                parser = argparse.ArgumentParser()
                parser.add_argument(    '--text', '-t', 
                            type=str, 
                            dest='text', 
                            help='set message to speech')

                parser.add_argument(    '--label', '-l', 
                            metavar='L', 
                            type=str, 
                            nargs='?', 
                            dest='label', 
                            help='set label for this message')

                parser.add_argument(    '-p', 
                            action='store_true', 
                            dest='periodic_flag', 
                            help='set either periodic message or not')

                args = parser.parse_args()
                print args.text
                print args.label
                print args.periodic_f
june2473 gravatar image june2473  ( 2019-11-27 02:26:43 -0500 )edit
1

Please do not reply to questions about which specific errors you observe with "the same problem". It does not give us any information.

Show actual errors, together with the commands used that led to those errors.

Single quotes should work, so please show what you did to test that approach and how it failed.

gvdhoorn gravatar image gvdhoorn  ( 2019-11-27 03:47:40 -0500 )edit
1

Do not post screenshots to show terminal text. It's just text. Copy-paste it into your question.

Also: please provide updates like your previous two comments as edits to your original question text. Edit the post and append the new information. You can use the edit button/link for that.

I've already added your example .launch file with your question.

Please edit it yourself to remove the link to the screenshot and copy-paste the error message directly from the terminal.

gvdhoorn gravatar image gvdhoorn  ( 2019-11-27 05:17:52 -0500 )edit

ok, done!

june2473 gravatar image june2473  ( 2019-11-27 05:33:19 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-11-27 05:22:06 -0500

Delb gravatar image

You just need to change this :

args = parser.parse_args()

to this :

args, unknown = parser.parse_known_args()

According to this stackoverflow question and the argparse doc it can avoid getting an error and it does actually find the arguments given.

edit flag offensive delete link more

Comments

thanks! it worked for me!

june2473 gravatar image june2473  ( 2019-11-28 05:24:21 -0500 )edit
1

answered 2019-11-27 05:31:52 -0500

gvdhoorn gravatar image

Ok. Thanks for showing us the actual error.

This has most likely nothing to do with quotes or not.

The cause is here that you're trying to start a script that is not equipped to deal with roslaunch args with roslaunch.

Notice this part in the error message:

unrecognized arguments: __name:=makeSoundRequest __log:=/home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/makeSoundRequest-1.log

Those arguments (__name and __log) are added by roslaunch to the command line used to invoke your script. So in the end, the complete command line used to invoke your script becomes:

/absolute/path/to/makeSoundRequest.py __name:=makeSoundRequest __log:=/home/nvidia/.ros/log/0681ca48-10f0-11ea-8929-00044bc771c7/makeSoundRequest-1.log --text 'please say something' --label mode1

As your script doesn't expect those extra arguments, it fails with the error message that you show.

You could either do what @Delb suggests (ignore "unknown args"), or properly handle them using an approach similar to what is described in #q272267.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-11-26 06:54:20 -0500

Seen: 1,913 times

Last updated: Nov 27 '19