X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=ucw%2Fdoc%2Fopt.txt;h=e5e5900c67f9d49121c94fd00ab8103ddce061df;hb=50c7c2b64a94a085b20e375bc67180a4470c2564;hp=3cb2592fc4259ae728d30a107a713e9085dcdf14;hpb=0192d9f3a127c82b32131d26ed9b5fb5a90db723;p=libucw.git diff --git a/ucw/doc/opt.txt b/ucw/doc/opt.txt index 3cb2592f..e5e5900c 100644 --- a/ucw/doc/opt.txt +++ b/ucw/doc/opt.txt @@ -1,13 +1,81 @@ Option parser ============= -FIXME! +Libucw contains a parser of command-line options, more versatile and +easier to use than the usual `getopt()` and `getopt_long()`. It follows the +traditional UNIX conventions of option syntax, but the options are defined in +a declarative way, similar in spirit to our <>. + +- <> +- <> +- <> + * <> + * <> + * <> + * <> + * <> + * <> + * <> + * <> + +[[example]] +Example +------- +Let us start with a simple example: a program with several options and +one mandatory positional argument. + + #include + #include + + int english; + int sugar; + int verbose; + char *tea_name; + + static struct opt_section options = { + OPT_ITEMS { + OPT_HELP("A simple tea boiling console."), + OPT_HELP("Usage: teapot [options] name-of-the-tea"), + OPT_HELP(""), + OPT_HELP("Options:"), + OPT_HELP_OPTION, + OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"), + OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "\tAmount of sugar (in teaspoons)"), + OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"), + OPT_STRING(OPT_POSITIONAL(1), NULL, tea_name, OPT_REQUIRED, ""), + OPT_END + } + }; + + int main(int argc, char **argv) + { + opt_parse(&options, argv+1); + return 0; + } + +[[anatomy]] +Anatomy of options +------------------ +Most options have the following properties: + +- <> defining overall behavior of the option +- Short name: one character. Set to 0 if the option has no short form. + Alternatively, the short name can refer to a <>. +- Long name: an arbitrary string. Set to NULL if the option has no long form. +- Variable, where the value of the option shall be stored, together with + its <>. +- Flags further specifying behavior of the option (whether it is mandatory, + whether it carries a value, whether it can be set repeatedly, etc.). + FIXME: Reference to flags and their defaults. +- Help text, from which the help displayed to the user is constructed. + FIXME: Explain tabs and newlines. +- Extra data specific for the particular class. [[opt_h]] ucw/opt.h ----------- +--------- This header file contains the public interface of the option parser module. !!ucw/opt.h -