Thread: Reading terminal arguments

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    14

    Reading terminal arguments

    So far I have a very basic application that can read simple arguments such as -t or -s. What I want to do is when the program picks up on the argument it will run a function. The two things I am not sure of though is how to read anything after the first argument and then what to store that second argument as to pass it to the function.

    So, what I want to happen is the following pseudo code:

    Terminal: app-name -t userEnteredText
    Application then sees that "-t" was used and then passes "userEnteredText" to argumentT() and then argumentT() returns the results and then the application couts the info.

    All I really need is to know how to read the text entered after -t.

    Below is the code I have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    //This is where we are going to initialize our functions.
    //int x and int y are just space holders for now.
    int argumentT(int x, int y);
    int argumentS(int x, int y);
    
    int main(int argc, char *argv[])
    {
        //Check to see if the user entered any arguments
        //if argc does not equal 2 then they did not.
        if(argc != 2)
        {
            printf("You did not tell me what to do!\n");
        }
        else
        {
            //Assuming that argc does equal 2 then we see what
            //the user entered and then pass that info on to
            //the corresponding function.
            if(strcmp(argv[1],"-t")==0)
            {
                //This was just a test to see if the argument could be read.
                int x = 1;
                int y = 2;
    
                cout << argumentT(x, y) <<"\n";
            }
    
            if(strcmp(argv[1],"-s")==0)
            {
                //This was just a test to see if the argument could be read.
                int x = 1;
                int y = 2;
    
                cout << argumentS(x, y) <<"\n";
    
            }
        }
    }
    
    //This is the function for the -t argument.
    int argumentT(int x, int y)
    {
        return x + y ;
    }
    
    //This is the function for the -s argument.
    int argumentS(int x, int y)
    {
        return x * y;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I prefer to parse command lines something like this...

    Example:
    Code:
    int main(int argc, char **argv)
    {
      int i, (*ptr)(int,int) = 0;
      char *param[2];
    
      for(i = 1; i < argc; ++i)
      {
        param[0] = param[1] = 0;
    
        if(*argv[i] == '-')
        {
          switch(argv[i][1])
          {
             case 't':
               ptr = &argumentT;
               break;
             case 's'
               ptr = &argumentS;
               break;
          }
    
          if(i+2 < argc)
          {
            param[0] = argv[++i]; // The ++i should not be changed to a i++.
            param[1] = argv[++i];
    
            cout << (*ptr)(atoi(param[0]), atoi(param[1])) << std::endl;
          } else
            std::cerr << "Uh oh! You forgot an argument!" << std::endl;
        }
      }
    
      return 0;
    }
    This way you can do stuff like: myprog.exe -t 55 36 -s 21 15 -t 32 35 -s 1
    And it will process everything correctly, including the missing parameter in the last switch.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Guess I should have mentioned that I am a little new to C++.......heh......

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I figured that much. Even the above code that I posted gives the user far too much credit. Its never good to assume that users will ever correctly do anything. Within a few projects where you leave loose ends for things that "the user obviously won't screw up." You will quickly find the first time you have a random person using your program they will immediately do every single input error that you could have fathomed.

    argv contains each of the command line arguments as its elements.

    gcc myprogram.c -o myprogram

    gcc's argv would look something like this:

    Code:
    char argv[4][] = {"gcc", "myprogram.c", "-o", "myprogram"};
    So argc will contain the number 4.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    So, now we are talking about manually setting the command line arguments in the argv array? If not then I do not understand what you were trying to say in your second post.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No. Definitely do not be doing that. I am just trying to express what is being past into the main() function. If I understood your initial question correctly, you were more or less asking how to have -t [number] [number] become useable within your program, right? I am just explaining that argv is just an array that contains all the values that a user typed into the command line.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Yes, basically when the user enters "appName -t input" I want to be able to read "input" as I already know how to read "-t".

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't have to do anything except type "argv[i+1]".

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    In my example it took -t [this number] [this number] and would add those together or whatever -t does. Maybe I am not being visual enough

    -t [i+1] [i+2] [i+3] ... [i+n]

    ^ See?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Well its not that I want to add them together I just want to be able to store whatever they enter as a variable. The variable type will change depending on what option they pick.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    During my switch case statement in my code you can do whatever you want within each case. So if my code isn't doing anything particularly useful for you, then feel free to alter it as necessary. My command line parser is very limited in what it can do.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    I wont lie, I hardly understand your code....

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if they type "myprog -t input" then argv[0] is "myprog", argv[1] is "-t", and argv[2] is "input". What exactly is the difficulty here? (Other than that your argc check is broken.)

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I could simplify the program, but I wanted you to have something to toy with that works as you want, while you try to make yours do the same. I did that intentionally since I am not in the homework for hire business... or in this case, the homework for nothing business.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    14
    Quote Originally Posted by tabstop View Post
    So if they type "myprog -t input" then argv[0] is "myprog", argv[1] is "-t", and argv[2] is "input". What exactly is the difficulty here? (Other than that your argc check is broken.)
    When I enter "appName -s input" I get "You did not tell me what to do!".

    Also, what do you mean that my argc check is broken?

    This is the code I am working with right now:

    Code:
    #include <iostream>
    
    using namespace std;
    
    //This is where we are going to initialize our functions.
    //int x and int y are just space holders for now.
    int argumentT(int x, int y);
    int argumentS(int x, int y);
    
    int main(int argc, char *argv[])
    {
        //Check to see if the user entered any arguments
        //if argc does not equal 2 then they did not.
        if(argc != 2)
        {
            printf("You did not tell me what to do!\n");
        }
        else
        {
            //Assuming that argc does equal 2 then we see what
            //the user entered and then pass that info on to
            //the corresponding function.
            if(strcmp(argv[1],"-t")==0)
            {
                //This was just a test to see if the argument could be read.
                int x = 1;
                int y = 2;
    
                cout << argumentT(x, y) <<"\n";
            }
    
            if(strcmp(argv[1],"-s")==0)
            {
                //This was just a test to see if the argument could be read.
                cout << argv[2];
    
            }
    
        }
    
    }
    
    //This is the function for the -t argument.
    int argumentT(int x, int y)
    {
        return x + y ;
    }
    
    //This is the function for the -s argument.
    int argumentS(int x, int y)
    {
        return x * y;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. commands and arguments problem
    By Martin Kovac in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 04:18 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. command line arguments question
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 09-29-2005, 12:56 PM
  4. seg fault command line arguments
    By bazzano in forum C Programming
    Replies: 17
    Last Post: 09-14-2005, 07:31 PM
  5. reading command line arguments into buffer
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 08:08 PM