Re: show help if no input
Duke <[email protected]> Wed, 18 Aug 2010 10:26:30 -0400
| Newsgroups | gmane.comp.gnu.gengetopt.general |
|---|---|
| Message-ID | <[email protected]> |
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?
>
> 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.
>> * 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?
Thanks,
D.