help-gengetopt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[help-gengetopt] gengetopt: initialization of args_info_help[]


From: Andre Noll
Subject: [help-gengetopt] gengetopt: initialization of args_info_help[]
Date: Tue, 2 Dec 2008 19:18:24 +0100
User-agent: Mutt/1.5.9i

Hi Lorenzo,

Here's a question about the initialization of the args_info_help[]
array created by gengetopt:

In my application I'm using several commandline parsers, all generated
by gengetopt. I'm trying to implement a kind of complete help that
ties together all the help texts of the individual parsers.

That looks like an easy thing to do given that each parser's header
file contains entries of the form

        extern const char *xxx_args_info_help[];
and
        extern const char *xxx_args_info_detailed_help[];

So one just has to do loop over all parsers and over all lines in
the corresponding xxx_args_info_help array. Similarly for the detailed
help. Right?

To my surprise, this approach works just fine for the detailed help
but fails for the short help. By looking at the generated code,
the reason becomes clear: The array for the detailed help is being
initialized directly by using string literals while the short help
is defined as a subarray of the detailed_help array. This subarray
is extracted and initialized by the init_help_array() function:

        static void
        init_help_array(void)
        {
          xxx_args_info_help[0] = xxx_info_detailed_help[0];
          xxx_args_info_help[1] = xxx_info_detailed_help[1];
          xxx_args_info_help[2] = xxx_info_detailed_help[2];
          xxx_args_info_help[3] = xxx_info_detailed_help[3];
          xxx_args_info_help[4] = xxx_info_detailed_help[5];
          xxx_args_info_help[0] = 0;
        }

(in this example xxx_info_detailed_help[4] is not contained in the
short help)

Before that function gets called, the contents of xxx_args_info_help
will be undefined. Since init_help_array() is a static function, it
is not possible to call it directly from the application that uses
gengetopt.  The only way to get an initialized xxx_args_info_help
array is to call xxx_cmdline_parser_init() which implicitly calls
init_help_array().

So for N parsers, I have to allocate N args_info structures and call
N init functions even in the case I just want to print the short help.
Is this behaviour intended?

An alternative would be to initialize also the short help by using
string literals, just like the detailed help. The duplicated strings
would not increase code size because the compiler should be smart
enough to put identical strings into memory only once.

Regards
Andre
-- 
The only person who always got his work done by Friday was Robinson Crusoe

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]