# Argc argv example?

**URL:** <https://root-forum.cern.ch/t/argc-argv-example/37791>\
**Category:** Newbie\
**Created:** [January 30, 2020, 7:57pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791 "2020-01-30T19:57:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [January 30, 2020, 7:57pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/1 "2020-01-30T19:57:15Z")

</div>

Hi all,

is there a simple example for command line arguments in the tutorials?

Regards and thanks.

Tim

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [January 30, 2020, 10:20pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/2 "2020-01-30T22:20:04Z")

</div>

Hi Tim,

I’m not aware of any specific tutorial on command line argument handling. If you have a compiled application with a `main()` function that links against ROOT, the argument handling is not different from any other program. You’d define `main` as

```
main(int argc, char **argv) {
}

```

and you find the executable in itself in `argv[0]` and the arguments in `argv[1]`- `argv[argc - 1]`.

If you are instead working with a ROOT macro that you call like `root -l myMacro.C`, then you can define arguments like for any C++ function, for instance

```
void myMacro(std::string title, int xmax, int ymax)
{
   ...
}

```

which you’d call like `root -l 'myMacro.C("a title", 42, 137)'`. The single quotes around the macro name and arguments are important in order to prevent the shell from interpreting special characters.

Does this answer your question?

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [January 31, 2020, 6:36pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/3 "2020-01-31T18:36:11Z")

</div>

Hi Jakob,

many thanks for your reply. I’ll try your suggestion this weekend.

Regards and all the best for the weekend.

Tim

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 2, 2020, 3:35pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/4 "2020-02-02T15:35:53Z")

</div>

Thanks Jakob. That worked. So the second and third arguments are, in my case, redundant and dummies?

Regards

Tim

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [February 3, 2020, 8:09am UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/5 "2020-02-03T08:09:26Z")

</div>

Hi Tim,

In the ROOT macro case, you’re free to select the number of arguments. So if you don’t need the extra two, you can simply remove them from the macro definition.

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 4, 2020, 8:18pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/6 "2020-02-04T20:18:52Z")

</div>

Hi Jakob, thanks again.

I apologise for my slow response but I’m doing this part time and have a full time job. But I’ll get back to you.

Regards

Tim

---

<div class="post-metadata">

**Author:** ![TimFulcher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/timfulcher/32/37354_2.png) [@TimFulcher](https://root-forum.cern.ch/u/TimFulcher)\
**Post date:** [February 14, 2020, 7:57pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/7 "2020-02-14T19:57:44Z")

</div>

Hi Jakob, It’s working but only if I use the additional arguments. That’s enough for me currently. But I would eventually like to tidy it up. I’ll get back to this when I can.

Regards

Tim

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [July 8, 2020, 3:19pm UTC](https://root-forum.cern.ch/t/argc-argv-example/37791/8 "2020-07-08T15:19:22Z")

</div>

I recommend `man 3 getopt` if you are compiling your code on Linux. Cheers,
