# Getting python argparse to work with a launch file or python node

**URL:** <https://discourse.openrobotics.org/t/getting-python-argparse-to-work-with-a-launch-file-or-python-node/10606>\
**Category:** ROS General\
**Created:** [September 9, 2019, 8:19pm UTC](https://discourse.openrobotics.org/t/getting-python-argparse-to-work-with-a-launch-file-or-python-node/10606 "2019-09-09T20:19:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zmk5](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/zmk5/32/24981_2.png) [@zmk5](https://discourse.openrobotics.org/u/zmk5)\
**Post date:** [September 9, 2019, 8:19pm UTC](https://discourse.openrobotics.org/t/getting-python-argparse-to-work-with-a-launch-file-or-python-node/10606/1 "2019-09-09T20:19:57Z")

</div>

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.

```python
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`:

```auto
['/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:

```auto
    return parser.parse_args(rclpy.sys.argv[4:])

```

In your `generate_launch_description()` function, you can get your parsed launch or node arguments with:

```python
def generate_launch_description():
    ...
    input_args = get_args()
    ...

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![mhubii](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/mhubii/32/20368_2.png) [@mhubii](https://discourse.openrobotics.org/u/mhubii)\
**Post date:** [January 29, 2020, 11:43am UTC](https://discourse.openrobotics.org/t/getting-python-argparse-to-work-with-a-launch-file-or-python-node/10606/2 "2020-01-29T11:43:50Z")

</div>

very nice @zmk5 , thanks.

When you call help, however, the thread gets stuck. How to solve that issue?

---

<div class="post-metadata">

**Author:** ![zmk5](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/zmk5/32/24981_2.png) [@zmk5](https://discourse.openrobotics.org/u/zmk5)\
**Post date:** [January 29, 2020, 3:15pm UTC](https://discourse.openrobotics.org/t/getting-python-argparse-to-work-with-a-launch-file-or-python-node/10606/3 "2020-01-29T15:15:14Z")

</div>

Hi @mhubii, would you mind making a more detailed question on [https://answers.ros.org](https://answers.ros.org)? Discourse is not meant for these kinds of questions. When you do make it send me a message here and I can answer your question there! 🙂
