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

Revision history [back]

click to hide/show revision 1
initial version

There's no convenient way to do this. I recommend you explain what you're trying to do. There's likely a better way than using if(my_arg == "hello"): style logic.

Also, I recommend using XML where you can. The Python API was not designed to be used directly, and as a result it is not very ergonomic.

There's no convenient way to do this. I recommend you explain what you're trying to do. There's likely a better way than using if(my_arg == "hello"): style logic.

Also, I recommend using XML where you can. The Python API was not designed to be used directly, and as a result it is not very ergonomic.

Edit:

You can control when things are actually included using "conditionals", for example (based on your comment):

<launch>
  <arg name="a_not_b" default="true" />
  <!-- argument a_option is required because it does not have a default value -->
  <arg name="a_option" if="$(var a_not_b)" />
  <group unless="$(var a_not_b)">
    <arg name="b_option1" default="true" />
    <arg name="b_option2" default="true" />
  </group>

</launch>

In this example, you have a required argument a_option, which if you don't supply it you get:

% ros2 launch /tmp/example.launch.xml
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-17-55-361895-markook.local-67414
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch.actions.declare_launch_argument]: Required launch argument "a_option" (description: "no description given") was not provided
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Required launch argument "a_option" was not provided.

But then, due to the condition "if=" attribute on it, you can avoid that with the option a_not_b:

% ros2 launch /tmp/example.launch.xml a_not_b:=false
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-18-03-013500-markook.local-67418
[INFO] [launch]: Default logging verbosity is set to INFO

However, since launch cannot know which arguments will be included until you actually run it, when you ask it to show the arguments all of the arguments will be shown, with only a * for the ones that may be conditionally used:

% ros2 launch /tmp/example.launch.xml --show-arguments
Arguments (pass arguments as '<name>:=<value>'):

    'a_not_b':
        no description given
        (default: 'true')

    'a_option':*
        no description given

    'b_option1':*
        no description given
        (default: 'true')

    'b_option2':*
        no description given
        (default: 'true')

* argument(s) which are only used if specific conditions occur

There's no convenient way to do this. I recommend you explain what you're trying to do. There's likely a better way than using if(my_arg == "hello"): style logic.

Also, I recommend using XML where you can. The Python API was not designed to be used directly, and as a result it is not very ergonomic.

Edit:

You can control when things are actually included using "conditionals", for example (based on your comment):

<launch>
  <arg name="a_not_b" default="true" />
  <!-- argument a_option is required because it does not have a default value -->
  <arg name="a_option" if="$(var a_not_b)" />
  <group unless="$(var a_not_b)">
    <arg name="b_option1" default="true" />
    <arg name="b_option2" default="true" />
  </group>

</launch>

In this example, you have a required argument a_option, which if you don't supply it you get:

% ros2 launch /tmp/example.launch.xml
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-17-55-361895-markook.local-67414
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch.actions.declare_launch_argument]: Required launch argument "a_option" (description: "no description given") was not provided
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Required launch argument "a_option" was not provided.

But then, due to the condition "if=" attribute on it, you can avoid that with the option a_not_b:

% ros2 launch /tmp/example.launch.xml a_not_b:=false
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-18-03-013500-markook.local-67418
[INFO] [launch]: Default logging verbosity is set to INFO

However, since launch cannot know which arguments will be included until you actually run it, when you ask it to show the arguments all of the arguments arguments, every possible argument will be shown, with only a * for the ones that may be conditionally used:

% ros2 launch /tmp/example.launch.xml --show-arguments
Arguments (pass arguments as '<name>:=<value>'):

    'a_not_b':
        no description given
        (default: 'true')

    'a_option':*
        no description given

    'b_option1':*
        no description given
        (default: 'true')

    'b_option2':*
        no description given
        (default: 'true')

* argument(s) which are only used if specific conditions occur