Solved

c++ Command line arguments

Posted on 2003-10-23
7
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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