Improve company productivity with a Business Account.Sign Up

x
?
Solved

Mutiple options with getopt

Posted on 2004-10-19
13
Medium Priority
?
313 Views
Last Modified: 2010-04-15
Hi Experts,
i want my program to parse muliple options at time using getopt( ) function. Also, if no option is provided it should parse all the availabe options. Below is the sample output i am having(Not quiet what i want) and the a Code also. Please help.
System: Solaris 2.8,  C++ actually
=================== SAMPLE OUTPUT =======================
[1]+  Stopped                 emacs main.cpp
[komlaaa@linuxbox a4]$ ./wc.out -t file
inputFile:
 file
Reading from file...

Total Count: 6
[komlaaa@linuxbox a4]$ ./wc.out -t -d file
inputFile:
 -d
InputFile failed to open
[komlaaa@linuxbox a4]$ ./wc.out  -d file
inputFile:
 file
Reading from file...

Distinct Words: 5
[komlaaa@linuxbox a4]$ ./wc.out  file
inputFile:
 [komlaaa@linuxbox a4]$
======================= SAMPLE CODE ==============================
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "wc.h"

int getopt(int, char *const *, const char *);

int main( int argc, char * const argv[])
{
  wc w;

  bool checkL = false;
  bool checkT = false;
  bool checkD = false;
  bool checkC = false;
  bool noArgument = false;

  char * inputFile = NULL;
  int ch;


  char options[] = ":l:t:d:c:";

  while ((ch = getopt(argc, argv, options)) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
      inputFile = optarg;
      break;
    case 't':
      checkT = true;
      inputFile = optarg;
      break;
    case 'd':
      checkD = true;
      inputFile = optarg;
      break; case 'c':
      checkC = true;
      inputFile = optarg;
      break;
    case ':':
      inputFile = optarg;
          noArgument = true;
          break;
    case '?':
    default:
      cout<<"Invalid option specified: \n"<< optopt<<"\n";
    w.helpMenu();
    break;
    }
}
cout<<"inputFile:\n " <<inputFile<<"\n";

  ifstream f(inputFile);

  if (!f) {
    cout<<"InputFile failed to open\n";
    exit(0);
  }

 if( true == checkT ){
 cout<<"Total Count: "<<endl;
}

 if( true == checkD ){
 cout<<"Distinct Words: " <<endl;
 }

 if( true == checkC ){
 cout<<"Most Common Words: "<<endl;
            .
            .
            .
            .

0
Comment
Question by:komlaaa
  • 5
  • 4
  • 4
13 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12354743
if all your options expect an argument, and eventually use the same variable(inputFile), why
dont u try using this?

 char options[] = ":ltdc";

  while ((ch = getopt(argc, argv, options)) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
//      inputFile = optarg;      comment out similar lines in the switch;
      break;
  ........
     }
      if(argv[optind])
               inputFile = argv[optind];
      else{
               printf("No input file provided\n");
                exit(1);
      }


hope this helps
van_dy
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12354787
well this part

 if(argv[optind])
               inputFile = argv[optind];
 else{
               printf("No input file provided\n");
                exit(1);
}

 will be outside the while loop obviously
0
 

Author Comment

by:komlaaa
ID: 12354976
Thanks Van dy, your method is working but not for NO ARGUMENT and ALL ARGUMENT AT ONCE cases

============TAKE A LOOK AT THE SAMPLE RUN =====================
[komlaaa@linuxbox a4]$ make
g++ -g -Wall -ansi -c main.cpp wc.cpp
g++ main.o wc.o -o wc.out
[komlaaa@linuxbox a4]$ ./wc.out file
Reading from file...

[komlaaa@linuxbox a4]$ ./wc.out -t  file
Reading from file...
                                                                     
Total Count: 6
[komlaaa@linuxbox a4]$ ./wc.out -t  -d file
Reading from file...

Total Count: 6
Distinct Words: 5
[komlaaa@linuxbox a4]$ ./wc.out -t  -d  -l file
Reading from file...

Total Count: 6
Distinct Words: 5
contents:
   World 1
   hello 2
   helloo 1
   worlds 1
   worlds1 1

[komlaaa@linuxbox a4]$ ./wc.out -t  -d  -l -c file
No input file provided
[komlaaa@linuxbox a4]$ ./wc.out  file
Reading from file...

[komlaaa@linuxbox a4]$
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 

Author Comment

by:komlaaa
ID: 12354980
If no options is provided, i would like all the options to be executed.

komlaaa
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12354982
>[komlaaa@linuxbox a4]$ ./wc.out -t  -d  -l -c file
>No input file provided
Try:
./wc.out -tdlc file

-ssnkumar
0
 

Author Comment

by:komlaaa
ID: 12355004
Nope the user want to run the command like:
./wc.out -t  -d  -l -c file


~komlaaa
0
 

Author Comment

by:komlaaa
ID: 12355011
Even if i do it still not working:

=====SAMPLE OUTPUT=====
[komlaaa@linuxbox a4]$ ./wc.out -tdlc file
No input file provided
[komlaaa@linuxbox a4]$
0
 
LVL 5

Accepted Solution

by:
van_dy earned 2000 total points
ID: 12355029
hi komlaa, what u can do for no-option case is add this after the while loop;

if(optind == 1){
      checkL = true;
 checkT = true;
  checkD = true;
  checkC = true;
}
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12355083
I think it should work for all arguement cases, i dont see a reason for it not to.
may be you mistyped something while giving the arguements like:

wc.out -t -l -d -c file   ///remember the spaces are important
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12355088
For having all options:
change options array to:
char options[] = ":ltdc:l:t:d:c";

After this both the commands:
./wc.out -t  -d  -l -c file
and
./wc.out -tdlc file
will work.

-ssnkumar
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12355114
ssn: well  [./wc.out -tldc file ] should work even with (char options[] = ":ltdc").
however if we use your c(har options), we will be back to getting the same errors as
komlaa was getting earlier on.
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12355165
hi komlaa,

    Try this modified code:

#include <unistd.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int main( int argc, char * const argv[])
{
        bool checkL = false;
        bool checkT = false;
        bool checkD = false;
        bool checkC = false;
        bool noArgument = false;

        char * inputFile = NULL;
        int ch;

        char options[] = ":ltdc:l:t:d:c:";

        while ((ch = getopt(argc, argv, options)) != -1)
        {
                switch (ch)
                {
                case 'l':
                        checkL = true;
                        inputFile = optarg;
                        cout << "Arg L\n";
                        break;
                case 't':
                        checkT = true;
                        inputFile = optarg;
                        cout << "Arg T\n";
                        break;
                case 'd':
                        checkD = true;
                        inputFile = optarg;
                        cout << "Arg D\n";
                        break;
                case 'c':
                        checkC = true;
                        inputFile = optarg;
                        cout << "Arg C\n";
                        break;
                case '?':
                default:
                        cout<<"Invalid option specified: \n"<< optopt<<"\n";
                        break;
                }
        }

        if ((checkL == true) && (checkT == true) && (checkD == true) && (checkD == true))
        {
                cout << "All arguments are true\n";
        }

        if ((checkL == false) && (checkT == false) && (checkD == false) && (checkD == false))
        {
                cout << "No arguments\n";
                inputFile = argv[1];
        }
}
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 12355209
Hi komlaa,

   There is a library built especially for processing command line arguments.
   If you are interested, take a look at: http://256.com/sources/argv/argv.html

-ssnkumar
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

606 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