Re: show help if no input

Lorenzo Bettini <[email protected]> Wed, 18 Aug 2010 20:35:06 +0200
Newsgroups gmane.comp.gnu.gengetopt.general
Message-ID <[email protected]>
On 08/18/2010 04:26 PM, Duke wrote:
> On 8/18/10 5:42 AM, Lorenzo Bettini wrote:
>> On 17/08/2010 22.15, Duke wrote:
>>> Hi folks,
>>>
>> Hi there
>
> Hi Lorenzo,
>
>>> I just heard about gengetopt, tried it and like it very much. I want
>>> to change the default reaction of gengetopt a little bit to suit my
>>> purpose better, so I have two questions below:
>>>
>>> * When Print help, is there a way showing that one option is optional
>>> and the other is required? For now, help shows only options and their
>>> descriptions and values, no way to know if an option is optional or
>>> required until one tries the program and some of its options.
>>>
>> for the moment this is not implemented, although in the summary of
>> command invocation optional options should be printed differently (I
>> should check this, I don't remember it right now and cannot check it
>> now).
>
> I am new to gengetopt, so I am not quite sure what you mentioned. What
> is the "summary of command invocation"? Is it printed when "-h" is used,
> or one has to use another option?

when you use -h|--help, but you must have generated the command line 
parser with

  -l, --long-help               long usage line in help

(this is an option to be passed to gengetopt)

then, when print the output of help you'll have something similar

      Usage: sample1 -iINT|--int-opt=INT [-h|--help] [--detailed-help] 
[--full-help]
               [-V|--version] [-sfilename|--str-opt=filename] 
[-mINT|--my-opt=INT]
               [--flag-opt] [-F|--funct-opt] [--long-opt=LONG] 
[--def-opt=STRING]
               [--enum-opt=STRING] [-DINT|--dependant=INT] [FILES]...


>
>>
>> Surely, this behavior could be added (by using a command line switch
>> of gengetopt itself); I should work on this (or if you want to try to
>> implement it...)
>
> Well, it is quite beyond my ability now. I am just a learner in C/C++.
> But I am willing to assist if possible.

by the way, I've just realized there's already something for this (see 
also the manual):

--show-required
     if --show-required is given, possibly with a string, in the output 
of --help will be made explicit which options are actually required,

>
>>> * When there is no input (no argument or argc < 2), is there a way of
>>> telling gengetopt to show help, even if there are required options?
>>> For now, if there is required option, and if the program is invoked
>>> without argument, then "option required" shows up, not the help.
>>>
>> this could be implemented in your program itself; as long as you init
>> an args_info structure, then you could check argc and in case call the
>> print help generated function directly; have you tried this?
>
> I might have missed something/some options, but here is what I tried:
>
> gengetopt_args_info args_info;
> if ( cmdline_parser (argc, argv, &args_info) != 0 ) {
> return 1;
> }
>
> string Input;
> if ( argc >= 2 && args_info.inputs_num == 1 ) {
> Input = args_info.inputs[0];
> } else {
> if ( argc < 2 ) {
> return usage( args_info, false );
> } else if ( args_info.help_given ) {
> return usage( args_info, true );
> } else if ( args_info.inputs_num == 0 ) {
> cerr << "No input file. Program aborts!" << endl;
> } else if ( args_info.inputs_num > 1 ) {
> cerr << "Too many input files. Program aborts!" << endl;
> }
> return ProgramExit(args_info);
> }
>
> where
>
> int ProgramExit( gengetopt_args_info args_info ) {
> cmdline_parser_free (&args_info);
> return -1;
> }
>
> So basically when there is no input (no argument, or argc < 2), help
> should be printed. This works only if I do not have any required option.
> If there is one required option, "option required" notification will
> show up. I think the condition to show "option required" is not just
> args_info.(some_option)_given, but also argc >= 2 in this situation.
> What do you think?

I meant something different: before invoking

cmdline_parser (argc, argv, &args_info) != 0

you could do something similar (not checked it myself)

if (argc < 2) {
cmdline_parser_init (&args_info);
cmdline_parser_print_version(void);
return -1;
}

does this do what you need?

please, let me know
cheers
	Lorenzo

-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134     (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net