📄 argp-help.c
字号:
/* Hierarchial argument parsing help output Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Miles Bader <miles@gnu.ai.mit.edu>. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stddef.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <stdarg.h>#include <malloc.h>#include <ctype.h>#if HAVE_STRINGS_H#include <strings.h>#endif#ifndef _/* This is for other GNU distributions with internationalized messages. When compiling libc, the _ macro is predefined. */#ifdef HAVE_LIBINTL_H# include <libintl.h># define _(msgid) gettext (msgid)#else# define _(msgid) (msgid)# define gettext(msgid) (msgid)#endif#endif#include "argp.h"#include "argp-fmtstream.h"#include "argp-namefrob.h"#if !HAVE_STRNDUPchar *strndup (const char *s, size_t n);#endif/* User-selectable (using an environment variable) formatting parameters. These may be specified in an environment variable called `ARGP_HELP_FMT', with a contents like: VAR1=VAL1,VAR2=VAL2,BOOLVAR2,no-BOOLVAR2 Where VALn must be a positive integer. The list of variables is in the UPARAM_NAMES vector, below. *//* Default parameters. */#define DUP_ARGS 0 /* True if option argument can be duplicated. */#define DUP_ARGS_NOTE 1 /* True to print a note about duplicate args. */#define SHORT_OPT_COL 2 /* column in which short options start */#define LONG_OPT_COL 6 /* column in which long options start */#define DOC_OPT_COL 2 /* column in which doc options start */#define OPT_DOC_COL 29 /* column in which option text starts */#define HEADER_COL 1 /* column in which group headers are printed */#define USAGE_INDENT 12 /* indentation of wrapped usage lines */#define RMARGIN 79 /* right margin used for wrapping *//* User-selectable (using an environment variable) formatting parameters. They must all be of type `int' for the parsing code to work. */struct uparams{ /* If true, arguments for an option are shown with both short and long options, even when a given option has both, e.g. `-x ARG, --longx=ARG'. If false, then if an option has both, the argument is only shown with the long one, e.g., `-x, --longx=ARG', and a message indicating that this really means both is printed below the options. */ int dup_args; /* This is true if when DUP_ARGS is false, and some duplicate arguments have been suppressed, an explanatory message should be printed. */ int dup_args_note; /* Various output columns. */ int short_opt_col; int long_opt_col; int doc_opt_col; int opt_doc_col; int header_col; int usage_indent; int rmargin; int valid; /* True when the values in here are valid. */};/* This is a global variable, as user options are only ever read once. */static struct uparams uparams = { DUP_ARGS, DUP_ARGS_NOTE, SHORT_OPT_COL, LONG_OPT_COL, DOC_OPT_COL, OPT_DOC_COL, HEADER_COL, USAGE_INDENT, RMARGIN, 0};/* A particular uparam, and what the user name is. */struct uparam_name{ const char *name; /* User name. */ int is_bool; /* Whether it's `boolean'. */ size_t uparams_offs; /* Location of the (int) field in UPARAMS. */};/* The name-field mappings we know about. */static const struct uparam_name uparam_names[] ={ { "dup-args", 1, offsetof (struct uparams, dup_args) }, { "dup-args-note", 1, offsetof (struct uparams, dup_args_note) }, { "short-opt-col", 0, offsetof (struct uparams, short_opt_col) }, { "long-opt-col", 0, offsetof (struct uparams, long_opt_col) }, { "doc-opt-col", 0, offsetof (struct uparams, doc_opt_col) }, { "opt-doc-col", 0, offsetof (struct uparams, opt_doc_col) }, { "header-col", 0, offsetof (struct uparams, header_col) }, { "usage-indent", 0, offsetof (struct uparams, usage_indent) }, { "rmargin", 0, offsetof (struct uparams, rmargin) }, { 0 }};/* Read user options from the environment, and fill in UPARAMS appropiately. */static voidfill_in_uparams (const struct argp_state *state){ const char *var = getenv ("ARGP_HELP_FMT");#define SKIPWS(p) do { while (isspace (*p)) p++; } while (0); if (var) /* Parse var. */ while (*var) { SKIPWS (var); if (isalpha (*var)) { size_t var_len; const struct uparam_name *un; int unspec = 0, val = 0; const char *arg = var; while (isalnum (*arg) || *arg == '-' || *arg == '_') arg++; var_len = arg - var; SKIPWS (arg); if (*arg == '\0' || *arg == ',') unspec = 1; else if (*arg == '=') { arg++; SKIPWS (arg); } if (unspec) if (var[0] == 'n' && var[1] == 'o' && var[2] == '-') { val = 0; var += 3; var_len -= 3; } else val = 1; else if (isdigit (*arg)) { val = atoi (arg); while (isdigit (*arg)) arg++; SKIPWS (arg); } for (un = uparam_names; un->name; un++) if (strlen (un->name) == var_len && strncmp (var, un->name, var_len) == 0) { if (unspec && !un->is_bool) __argp_failure (state, 0, 0, _("%.*s: ARGP_HELP_FMT parameter requires a value"), (int)var_len, var); else *(int *)((char *)&uparams + un->uparams_offs) = val; break; } if (! un->name) __argp_failure (state, 0, 0, _("%.*s: Unknown ARGP_HELP_FMT parameter"), (int)var_len, var); var = arg; if (*var == ',') var++; } else if (*var) { __argp_failure (state, 0, 0, _("Garbage in ARGP_HELP_FMT: %s"), var); break; } }}/* Returns true if OPT hasn't been marked invisible. Visibility only affects whether OPT is displayed or used in sorting, not option shadowing. */#define ovisible(opt) (! ((opt)->flags & OPTION_HIDDEN))/* Returns true if OPT is an alias for an earlier option. */#define oalias(opt) ((opt)->flags & OPTION_ALIAS)/* Returns true if OPT is an documentation-only entry. */#define odoc(opt) ((opt)->flags & OPTION_DOC)/* Returns true if OPT is the end-of-list marker for a list of options. */#define oend(opt) __option_is_end (opt)/* Returns true if OPT has a short option. */#define oshort(opt) __option_is_short (opt)/* The help format for a particular option is like: -xARG, -yARG, --long1=ARG, --long2=ARG Documentation... Where ARG will be omitted if there's no argument, for this option, or will be surrounded by "[" and "]" appropiately if the argument is optional. The documentation string is word-wrapped appropiately, and if the list of options is long enough, it will be started on a separate line. If there are no short options for a given option, the first long option is indented slighly in a way that's supposed to make most long options appear to be in a separate column. For example, the following output (from ps): -p PID, --pid=PID List the process PID --pgrp=PGRP List processes in the process group PGRP -P, -x, --no-parent Include processes without parents -Q, --all-fields Don't elide unusable fields (normally if there's some reason ps can't print a field for any process, it's removed from the output entirely) -r, --reverse, --gratuitously-long-reverse-option Reverse the order of any sort --session[=SID] Add the processes from the session SID (which defaults to the sid of the current process) Here are some more options: -f ZOT, --foonly=ZOT Glork a foonly -z, --zaza Snit a zar -?, --help Give this help list --usage Give a short usage message -V, --version Print program version The struct argp_option array for the above could look like: { {"pid", 'p', "PID", 0, "List the process PID"}, {"pgrp", OPT_PGRP, "PGRP", 0, "List processes in the process group PGRP"}, {"no-parent", 'P', 0, 0, "Include processes without parents"}, {0, 'x', 0, OPTION_ALIAS}, {"all-fields",'Q', 0, 0, "Don't elide unusable fields (normally" " if there's some reason ps can't" " print a field for any process, it's" " removed from the output entirely)" }, {"reverse", 'r', 0, 0, "Reverse the order of any sort"}, {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS}, {"session", OPT_SESS, "SID", OPTION_ARG_OPTIONAL, "Add the processes from the session" " SID (which defaults to the sid of" " the current process)" }, {0,0,0,0, "Here are some more options:"}, {"foonly", 'f', "ZOT", 0, "Glork a foonly"}, {"zaza", 'z', 0, 0, "Snit a zar"}, {0} } Note that the last three options are automatically supplied by argp_parse, unless you tell it not to with ARGP_NO_HELP.*//* Returns true if CH occurs between BEG and END. */static intfind_char (char ch, char *beg, char *end){ while (beg < end) if (*beg == ch) return 1; else beg++; return 0;}struct hol_cluster; /* fwd decl */struct hol_entry{ /* First option. */ const struct argp_option *opt; /* Number of options (including aliases). */ unsigned num; /* A pointers into the HOL's short_options field, to the first short option letter for this entry. The order of the characters following this point corresponds to the order of options pointed to by OPT, and there are at most NUM. A short option recorded in a option following OPT is only valid if it occurs in the right place in SHORT_OPTIONS (otherwise it's probably been shadowed by some other entry). */ char *short_options; /* Entries are sorted by their group first, in the order: 1, 2, ..., n, 0, -m, ..., -2, -1 and then alphabetically within each group. The default is 0. */ int group; /* The cluster of options this entry belongs to, or 0 if none. */ struct hol_cluster *cluster; /* The argp from which this option came. */ const struct argp *argp;};/* A cluster of entries to reflect the argp tree structure. */struct hol_cluster{ /* A descriptive header printed before options in this cluster. */ const char *header; /* Used to order clusters within the same group with the same parent, according to the order in which they occured in the parent argp's child list. */ int index; /* How to sort this cluster with respect to options and other clusters at the same depth (clusters always follow options in the same group). */ int group; /* The cluster to which this cluster belongs, or 0 if it's at the base level. */ struct hol_cluster *parent; /* The argp from which this cluster is (eventually) derived. */ const struct argp *argp; /* The distance this cluster is from the root. */ int depth; /* Clusters in a given hol are kept in a linked list, to make freeing them possible. */ struct hol_cluster *next;};/* A list of options for help. */struct hol{ /* An array of hol_entry's. */ struct hol_entry *entries; /* The number of entries in this hol. If this field is zero, the others are undefined. */ unsigned num_entries; /* A string containing all short options in this HOL. Each entry contains pointers into this string, so the order can't be messed with blindly. */ char *short_options; /* Clusters of entries in this hol. */ struct hol_cluster *clusters;};/* Create a struct hol from the options in ARGP. CLUSTER is the hol_cluster in which these entries occur, or 0, if at the root. */static struct hol *make_hol (const struct argp *argp, struct hol_cluster *cluster){ char *so; const struct argp_option *o; const struct argp_option *opts = argp->options; struct hol_entry *entry; unsigned num_short_options = 0; struct hol *hol = malloc (sizeof (struct hol)); assert (hol); hol->num_entries = 0; hol->clusters = 0; if (opts) { int cur_group = 0; /* The first option must not be an alias. */ assert (! oalias (opts)); /* Calculate the space needed. */ for (o = opts; ! oend (o); o++) { if (! oalias (o)) hol->num_entries++; if (oshort (o)) num_short_options++; /* This is an upper bound. */ } hol->entries = malloc (sizeof (struct hol_entry) * hol->num_entries); hol->short_options = malloc (num_short_options + 1); assert (hol->entries && hol->short_options); /* Fill in the entries. */ so = hol->short_options; for (o = opts, entry = hol->entries; ! oend (o); entry++) { entry->opt = o;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -