1

Following is my small code snippet using getopt that works well on my linux machine, but not on solaris machine. It is standard code snippet that I found else where in the internet.

while ((c = getopt(argc, argv, "ab:")) != -1) {
  cout << "I am solaris, I dont come here \n";
  switch(c) {
    case 'a':
    case 'b':
  }
}

Now there is no problem on my linux machine. It does good job. But on my solaris machine, it does not even go inside the while loop, so it does not parse anything for me. I checked "man getopt" on my solaris machine (as I think getopt in shell is used), It just says getopt will not be supported in the next major release.

So How I could i make it work on my solaris machine. I dont want to use boost.

Thanks D. L. Kumar

3
  • Try writing your own function to parse the command line? Commented Oct 17, 2011 at 19:31
  • "I dont want to use boost." There's a cross-platform library already written that does what you want, and you go out of your way to avoid it? Some things I'll never understand... Commented Oct 17, 2011 at 19:35
  • Please revise your code; what you've posted here won't even compile. When man getopt on Solaris tells you getopt won't be supported in the next major release, it's talking about the shell command, not the C function. For that, run man -s3c getopt. Commented Oct 17, 2011 at 19:47

1 Answer 1

1

If, as you say, Solaris isn't going to support getopt in the next major release, you'll need to use your own with an IF/DEF macro when not compiling on GNU/Linux. Something along this lines:

#IFDEF _SOLARIS_
for (int index=0; index < argv; ++index)
{
  c = argc[index];  
  switch(c) {
    case 'a':
     // do your code
    case 'b':
     index++;
     if (index < argc)
       PARAMATER = arg[index]; // plucks the parameter
     else
       HANDLE MISSING ERROR
     // do your code
  }
}
#ELSE
while ((c = getopt(argc, argv, "ab:")) != -1) {
  cout << "I am solaris, I dont come here \n";
  switch(c) {
    case 'a':
    case 'b':
  }
}
#ENDIF
Sign up to request clarification or add additional context in comments.

2 Comments

If having to make code to manually handle command line options, why use getopt at all?
There wouldn't be a need so much as the question was how to handle Solaris which didn't have it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.