I need help with a C++ script.

Sep 30, 2015 at 6:54pm
Hi everyone. I have an idea for a script like a downloader for certain things but thats not the important part. I would like to figure out how to run a c++ program with commands. What I mean by this is take npm for example. A standard command is this: npm -g <module>. the command "npm" is run from /usr/bin (on mac). So my though was to make my c++ script and put the executable in my /usr/mac and run it from terminal. In theory this works but i can run a command like: c++test -g. Because the program needs to be run before anything happens. How can i accomplish what a want?
Sep 30, 2015 at 6:58pm
What do you mean by "the program needs to be run before anything happens"? If you run the program, the program will run.
Sep 30, 2015 at 7:00pm
i mean i cant call the program and run a command within the program in the same line.
Sep 30, 2015 at 8:10pm
In C++ there are at least two arguments passed into the main function from the OS when the process is started. The second argument is an array of plain text, c-style strings that contains the fully qualified path of the program in the first element and the text passed to the program from the command line in the subsequent array elements. The first argument passed into main is an integer indicating the number of elements in that array. To accomplish what you want simply read the text passed in from that second argument.

1
2
3
4
5
6
7
int main(int count, char* args[])
{
    for(int i = 0; i <= count; ++i)
    {
          std::cout << args[i] << "\n";
     }
}


Navigate to the directory that your compiled app is stored in and pass it some stuff on the command line.
Last edited on Sep 30, 2015 at 8:11pm
Sep 30, 2015 at 8:18pm
thanks this is perfect :)
Topic archived. No new replies allowed.