Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

Mutiple options with getopt

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
komlaaa
Asked:
komlaaa
  • 5
  • 4
  • 4
1 Solution
 
van_dyCommented:
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
 
van_dyCommented:
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
 
komlaaaAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
komlaaaAuthor Commented:
If no options is provided, i would like all the options to be executed.

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

-ssnkumar
0
 
komlaaaAuthor Commented:
Nope the user want to run the command like:
./wc.out -t  -d  -l -c file


~komlaaa
0
 
komlaaaAuthor Commented:
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
 
van_dyCommented:
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
 
van_dyCommented:
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
 
ssnkumarCommented:
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
 
van_dyCommented:
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
 
ssnkumarCommented:
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
 
ssnkumarCommented:
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

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!

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now