Solved

c++ Command line arguments

Posted on 2003-10-23
7
327 Views
Last Modified: 2010-04-22
I have a c++ program and i need to add some command line arguments ... i only need 1 argument to be passed in. there is only a possiibilty of 2 arguments, -day or -month, i need to have it so only a maximum of 1 arguement can be used but i would also like to be able to have NO arguments used and have it default to a certain string for eg

string test = "month" //defualt test string

//main prog
string arg = argv(1);
if (arg == "day") {
string test = "day";
}

if an arguement is given but does not equal day or month than we quit with an error message, if no argument given than the program continues on like normal and since we allready declared that "test" = month that effectively is the default string.
Any help in getting this right would be great, i am not sure if string is the right thing to be using so i don't care what anyone uses, i just need somesort of variable or something that holds that value "day" or "month".

Thanks





0
Comment
Question by:bulldogs
  • 5
  • 2
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9606128
if ( argc > 2 )
    // more arguments than you expected

else if (argc == 1 )
    //use default arguments

else
     if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 )
                //valid arguments
     else
               //invalid argument
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9606137
you can ofcourse replace if else consructs with
switch (argc)
0
 

Author Comment

by:bulldogs
ID: 9606593
ok we are getting somewhere :-)

only prob now is if i start my program with NO arguments that works ok, if i specify -day or -month that works, if i specify more than 1 argument if gives me my error message :-) but if i specify the wrong command line argument the program just continues on as normal? here is the code i have now

string arg = argv[1];
if ( argc > 2 ) {
// more arguments than you expected
cout << "too many arguments" << endl;
exit(1);
}
else if (argc == 1 )
{
//use default arguments
//string test = "month";
cout << "No arguments given" << endl;
exit(1);
}
else
if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
           if (arg == "-day") {
              cout << "day argument found" << endl;
              //string test = "day"
              exit(1);
             }
             else if (arg == "-month") {
                cout << "month argument found" << endl;
                //string test = "month"
                exit(1);
               }
         else {
           cout << "Don't know that argument" << endl;
           exit(1);
             }
          }
cout << "Oops we should never reach this point" << endl;
exit(1);
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 9606753
>if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
would be true if argv[1] is either -day or -month
try
if ( (strcmp ( argv[1], "-day") == 0) || (strcmp ( argv[1], "-month" ) == 0) ) {

> the program just continues on as normal?
err as normal means? executes code for day/month
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9606813
i just tried the code snippet and it seems to work fine ... are you sure you are not having some = in place of == or something similar
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9606836
found your problem

    else {
          cout << "Don't know that argument" << endl;
          exit(1);

is treated as else of
if (arg == "-month") {

you need to close the

if ( strcmp ( argv[1], "-day") == 0 || strcmp ( argv[1], "-month" ) == 0 ) {
for the said else being treated correctly

 else if (arg == "-month") {
               cout << "month argument found" << endl;
               //string test = "month"
               exit(1);
              }
         }        =========== added
        else {
          cout << "Don't know that argument" << endl;
          exit(1);
            }
         } ========== remove this
0
 

Author Comment

by:bulldogs
ID: 9609810
Thanks sunnycoder that works great :-)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question