Solved

c++ Command line arguments

Posted on 2003-10-23
7
326 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now