?
Solved

Mutiple options with getopt

Posted on 2004-10-19
13
Medium Priority
?
303 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
[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
  • 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
Technology Partners: 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!

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

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