Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c++ Command line arguments

Posted on 2003-10-23
7
Medium Priority
?
336 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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