public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: Where did the output go and question regarding return values of getopt_long()
@ 2004-12-19 20:26 Mikael Åsberg
  0 siblings, 0 replies; 3+ messages in thread
From: Mikael Åsberg @ 2004-12-19 20:26 UTC (permalink / raw)
  To: Cygwin List

"Larry Hall" wrote
> At 10:14 AM 12/19/2004, you wrote:
>>Hello, consider this c++-function:
>>
>>void
>>handle_command_line(int argc,
>>                   char* const argv[],
>>                   bool& verbose,
>>                   vector<string>& numbers)
>>{
>>  const int id_numbers = 4711;
>>  const int id_verbose = 4712;
>>  const int id_help = 4713;
>>
>>  const option long_options[] =
>>  {
>>     {"numbers", 1, NULL, id_numbers},
>>     {"verbose", 0, NULL, id_verbose},
>>     {"help", 0, NULL, id_help},
>>     {NULL, 0, NULL, 0}
>>  };
>>
>>  const char* const short_options = "";
>>
>>  int c = 0;
>>
>>  while((c = getopt_long(
>>            argc,
>>            argv,
>>            short_options,
>>            long_options,
>>            NULL)) != -1)
>>  {
>>     switch(c)
>>     {
>>        cout << "c = " << c << endl; /* Never shown on screen... */
>>
>>        case id_numbers:
>>        {
>>           cout << "Calling extract_numbers() with " << optarg << endl;
>>
>>           extract_numbers(optarg, numbers);
>>
>>           break;
>>        }
>>        case id_verbose:
>>        {
>>           cout << "Setting verbose = true" << endl;
>>
>>           verbose = true;
>>
>>           break;
>>        }
>>        case id_help:
>>        {
>>           cout << "TODO: Add short description regarding usage here." << 
>> endl;
>>
>>           exit(EXIT_SUCCESS);
>>        }
>>        /* c seems to equal '?' for missing parameter as well... */
>>        case ':':
>>        {
>>           cerr << "Missing parameter." << endl;
>>
>>           break;
>>        }
>>        /* Only triggers for unknown options beginning with -- */
>>        case '?':
>>        {
>>           cerr << "Ignoring unknown option." << endl;
>>
>>           break;
>>        }
>>        default:
>>        {
>>           /* Throw exception? */
>>           cerr << "Should not be reached." << endl;
>>        }
>>     }
>>  }
>>}
>>
>>My first question is about the
>>cout << "c = " << c << endl;
>>inside the switch. I never see this output, why? I tried writing the 
>>output to a file as well but that file was empty when the program 
>>finished. All other "output-statements" are displayed just fine in this 
>>function (and the rest of the program).
>
> Did you look at the warning you get from the line in question?
>
> warning: unreachable code at beginning of switch statement
>
>

Lol, of course! It's not reachable...I should've seen that. Anyway, I always 
compile with -Wall -W -ansi -pedantic and my homebrewn gcc-3.4.3 didn't 
yield any warnings at all.

Thanks for the help, Larry! One question answered. I'm still wondering about 
the return values of getopt_long(), though, and I will try the same code on 
Fedora as soon as I get it setup properly on my laptop.

/ Mikael
>
> --
> Larry Hall                              http://www.rfk.com
> RFK Partners, Inc.                      (508) 893-9779 - RFK Office
> 838 Washington Street                   (508) 893-9889 - FAX
> Holliston, MA 01746
>
> 



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Where did the output go and question regarding return values of getopt_long()
  2004-12-19 15:15 Mikael Åsberg
@ 2004-12-19 19:43 ` Larry Hall
  0 siblings, 0 replies; 3+ messages in thread
From: Larry Hall @ 2004-12-19 19:43 UTC (permalink / raw)
  To: Mikael Åsberg, cygwin

At 10:14 AM 12/19/2004, you wrote:
>Hello, consider this c++-function:
>
>void
>handle_command_line(int argc,
>                   char* const argv[],
>                   bool& verbose,
>                   vector<string>& numbers)
>{
>  const int id_numbers = 4711;
>  const int id_verbose = 4712;
>  const int id_help = 4713;
>
>  const option long_options[] =
>  {
>     {"numbers", 1, NULL, id_numbers},
>     {"verbose", 0, NULL, id_verbose},
>     {"help", 0, NULL, id_help},
>     {NULL, 0, NULL, 0}
>  };
>
>  const char* const short_options = "";
>
>  int c = 0;
>
>  while((c = getopt_long(
>            argc,
>            argv,
>            short_options,
>            long_options,
>            NULL)) != -1)
>  {
>     switch(c)
>     {
>        cout << "c = " << c << endl; /* Never shown on screen... */
>
>        case id_numbers:
>        {
>           cout << "Calling extract_numbers() with " << optarg << endl;
>
>           extract_numbers(optarg, numbers);
>
>           break;
>        }
>        case id_verbose:
>        {
>           cout << "Setting verbose = true" << endl;
>
>           verbose = true;
>
>           break;
>        }
>        case id_help:
>        {
>           cout << "TODO: Add short description regarding usage here." << endl;
>
>           exit(EXIT_SUCCESS);
>        }
>        /* c seems to equal '?' for missing parameter as well... */
>        case ':':
>        {
>           cerr << "Missing parameter." << endl;
>
>           break;
>        }
>        /* Only triggers for unknown options beginning with -- */
>        case '?':
>        {
>           cerr << "Ignoring unknown option." << endl;
>
>           break;
>        }
>        default:
>        {
>           /* Throw exception? */
>           cerr << "Should not be reached." << endl;
>        }
>     }
>  }
>}
>
>My first question is about the
>cout << "c = " << c << endl;
>inside the switch. I never see this output, why? I tried writing the output to a file as well but that file was empty when the program finished. All other "output-statements" are displayed just fine in this function (and the rest of the program).

Did you look at the warning you get from the line in question?

warning: unreachable code at beginning of switch statement



--
Larry Hall                              http://www.rfk.com
RFK Partners, Inc.                      (508) 893-9779 - RFK Office
838 Washington Street                   (508) 893-9889 - FAX
Holliston, MA 01746                     


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Where did the output go and question regarding return values of getopt_long()
@ 2004-12-19 15:15 Mikael Åsberg
  2004-12-19 19:43 ` Larry Hall
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Åsberg @ 2004-12-19 15:15 UTC (permalink / raw)
  To: cygwin

Hello, consider this c++-function:

void
handle_command_line(int argc,
                    char* const argv[],
                    bool& verbose,
                    vector<string>& numbers)
{
   const int id_numbers = 4711;
   const int id_verbose = 4712;
   const int id_help = 4713;

   const option long_options[] =
   {
      {"numbers", 1, NULL, id_numbers},
      {"verbose", 0, NULL, id_verbose},
      {"help", 0, NULL, id_help},
      {NULL, 0, NULL, 0}
   };

   const char* const short_options = "";

   int c = 0;

   while((c = getopt_long(
             argc,
             argv,
             short_options,
             long_options,
             NULL)) != -1)
   {
      switch(c)
      {
         cout << "c = " << c << endl; /* Never shown on screen... */

         case id_numbers:
         {
            cout << "Calling extract_numbers() with " << optarg << endl;

            extract_numbers(optarg, numbers);

            break;
         }
         case id_verbose:
         {
            cout << "Setting verbose = true" << endl;

            verbose = true;

            break;
         }
         case id_help:
         {
            cout << "TODO: Add short description regarding usage here." << 
endl;

            exit(EXIT_SUCCESS);
         }
         /* c seems to equal '?' for missing parameter as well... */
         case ':':
         {
            cerr << "Missing parameter." << endl;

            break;
         }
         /* Only triggers for unknown options beginning with -- */
         case '?':
         {
            cerr << "Ignoring unknown option." << endl;

            break;
         }
         default:
         {
            /* Throw exception? */
            cerr << "Should not be reached." << endl;
         }
      }
   }
}

My first question is about the
cout << "c = " << c << endl;
inside the switch. I never see this output, why? I tried writing the output 
to a file as well but that file was empty when the program finished. All 
other "output-statements" are displayed just fine in this function (and the 
rest of the program).

And my second questions is why does getopt_long() return '?' for the error 
when an option requires an argument but none was supplied? I thought that it 
would return ? for unknown option and return : for missing required 
parameters, but it seems to return ? for both those errors (but it ouputs 
the correct diagnostic).

I'm using latest offical cygwin, all relevant packages up-to-date. Running 
fully-updated winxp pro.

Thanks for any replies 



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-12-19 20:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-19 20:26 Where did the output go and question regarding return values of getopt_long() Mikael Åsberg
  -- strict thread matches above, loose matches on Subject: below --
2004-12-19 15:15 Mikael Åsberg
2004-12-19 19:43 ` Larry Hall

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).