Laxkit
0.0.7.1
|
Class to simplify parsing command line options, and outputing help text. More...
Public Member Functions | |
int | Verify () |
void | HelpHeader (const char *header_text) |
Create the top portion of help text, not option specific. Multiple calls will append. | |
const char * | HelpHeader () |
void | UsageLine (const char *usage_line) |
void | OptionsHeader (const char *usage_line) |
int | NewGroup (const char *group_header_text) |
int | Add (const char *long_option, int short_option, int has_param, const char *nhelp_text, int nid=0, const char *opt_example=NULL) |
void | Help (FILE *file=NULL, int columns=-1) |
Output the help to the given file as plain text. Defaults to stderr if NULL. | |
void | HelpHtml (FILE *file=NULL) |
Output a man page formatted template, listing the options. | |
void | HelpMan (FILE *file=NULL) |
Output an html snippet formatted template, listing the options. | |
int | Parse (int argc, char **argv, int *argerr) |
Parse options. | |
int | Review (int which) |
Set which set of arguments to review, 1 for remaining non-options, 0 for options. | |
LaxOption * | error () |
When a missing parameter of unknown option is encountered in Parse(), use this to return the object for more info. | |
LaxOption * | start () |
Start looping with any option arguments, and return the first of those, if any. | |
LaxOption * | remaining () |
Start looping with any non-option arguments, and return the first of those, if any. | |
LaxOption * | next () |
LaxOption * | find (const char *long_option, int short_option) |
int | more () |
Return number of remaining options. |
Protected Attributes | |
char * | helpheader |
char * | optionsheader |
char * | usageline |
int | curgroup |
int | curitem |
int | loop_which |
int | first_remaining |
int | erred |
Protected Attributes inherited from Laxkit::PtrStack< LaxOption > | |
char * | islocal |
int | n |
LaxOption ** | e |
int | max |
int | delta |
char | arrays |
Additional Inherited Members | |
Protected Member Functions inherited from Laxkit::PtrStack< LaxOption > | |
PtrStack (char nar=1) | |
virtual LaxOption * | operator[] (int i) |
virtual void | flush () |
virtual int | howmany () |
virtual void | swap (int i1, int i2) |
virtual int | push (LaxOption *nd, char local=-1, int where=-1) |
virtual int | popp (LaxOption *topop, int *local=NULL) |
virtual int | pop (LaxOption *&popped, int which=-1, int *local=NULL) |
virtual LaxOption * | pop (int which=-1, int *local=NULL) |
virtual int | findindex (LaxOption *t) |
virtual int | remove (int which=-1) |
virtual int | pushnodup (LaxOption *nd, char local, int where=-1) |
virtual void | Delta (int ndelta) |
virtual int | Delta () |
virtual LaxOption ** | extractArrays (char **local=NULL, int *nn=NULL) |
virtual int | insertArrays (LaxOption **a, char *nl, int nn) |
Class to simplify parsing command line options, and outputing help text.
This class currently assumes that each option can appear once. They can have long and short options with descriptions, and simple grouping of options.
In outputting, it is currently assumed that the option example is short, and contained on one line. The help text per option is wrapped to fit within a certain number of columns, and is assumed to be able to be read as a single line (or a wrapped single line).
You would Add() new options to a LaxOptions object, then when done, call Parse(). Afterwards you can loop over parsed options with start(), next(), and remaining().
int Laxkit::LaxOptions::Add | ( | const char * | long_option, |
int | short_option, | ||
int | has_param, | ||
const char * | nhelp_text, | ||
int | nid = 0 , |
||
const char * | opt_example = NULL |
||
) |
If has_param==1, then opt_example is used as the text to indicate the argument in the help text. If opt_example is NULL, then use "arg" instead. If has_param==2, then opt_example is used as the whole option example.
has_param | nonzero if the option takes a parameter, else it doesn't |
opt_example | like '–list, -l "all"' instead of just "--list, -l" |
LaxOption * Laxkit::LaxOptions::error | ( | ) |
When a missing parameter of unknown option is encountered in Parse(), use this to return the object for more info.
Particularly, for missing parameters, this gives you easy access to the help text corresponding to that option.
Find an option corresponding to either long_option or short_option (when non-null).
void Laxkit::LaxOptions::Help | ( | FILE * | file = NULL , |
int | columns = -1 |
||
) |
Output the help to the given file as plain text. Defaults to stderr if NULL.
If columns<=0, then use ioctl(0, TIOCGWINSZ, &w), w.ws_col for the number of columns.
References newstr().
void Laxkit::LaxOptions::HelpHtml | ( | FILE * | file = NULL | ) |
Output a man page formatted template, listing the options.
The idea here is to simplify creation of man pages, to automatically get a formatted option list, which is otherwise very tedious to produce.
If file==NULL, then use stdout.
void Laxkit::LaxOptions::HelpMan | ( | FILE * | file = NULL | ) |
Output an html snippet formatted template, listing the options.
Output a table with 4 columns: long form, short form, parameter example, description
If file==NULL, then use stdout.
Parse options.
This follows basic conventions that long options are preceeded with "--". Any number of short options can follow a "-" unless it expects a parameter. An argument of only "--" is ignored and signifies that all following arguments are not options. Parameters can be specified with "--long-option arg", '-s arg' for short options, or -abcd for multiple short options.
The original argv is not modified.
On success, returns the number of options found and parsed, which will be 0 or more. If there is a an unknown option, then parsing stops, argerr is set to the index of argv for the offending item and -1 is returned. If there is a missing parameter, then parsing stops, argerr is set to the index of argv for the offending item and -2 is returned.
Note that argv[0] is ignored.
References makestr().
Set which set of arguments to review, 1 for remaining non-options, 0 for options.
Sets loop counter to 0.