Home | News | Documentation | Download

Argc argv example?

Hi all,

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

Regards and thanks.

Tim

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

Hi Jakob,

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

Regards and all the best for the weekend.

Tim

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

Regards

Tim

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

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

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