0
$\begingroup$

Rosanswers logo

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.


Originally posted by june2473 on ROS Answers with karma: 83 on 2019-11-26

Post score: 0


Original comments

Comment by Delb on 2019-11-26:
Have you tried using simple quotes instead ?

Comment by june2473 on 2019-11-26:
yes, was not working

Comment by Delb on 2019-11-27:\

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) ?

Comment by june2473 on 2019-11-27:
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

Comment by gvdhoorn on 2019-11-27:
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.

Comment by gvdhoorn on 2019-11-27:
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.

Comment by june2473 on 2019-11-27:
ok, done!

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by Delb with karma: 3907 on 2019-11-27

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by june2473 on 2019-11-28:
thanks! it worked for me!

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2019-11-27

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.