size_t cmdlen;
int ambig;
int best;
+ struct cmd_item *alias;
/* Allow --long-style options. */
if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
return i;
if (strlen(as->parms[i].name) < cmdlen)
continue;
+
+ /* Check for aliases, which must be full matches */
+ alias = as->parms[i].aliases;
+ while (alias != NULL) {
+ if (strcmp(alias->data, aname) == 0)
+ return i;
+ alias = alias->next;
+ }
+
/* A hidden option must be a full match (no best matches) */
if (as->parms[i].flags & CMD_HIDE || !enableAbbreviation)
continue;
}
/* print the help for a single parameter */
-static void
-PrintParmHelp(struct cmd_parmdesc *aparm)
+static char *
+ParmHelpString(struct cmd_parmdesc *aparm)
{
+ char *str;
if (aparm->type == CMD_FLAG) {
-#ifdef notdef
- /* doc people don't like seeing this information */
- if (aparm->help)
- printf(" (%s)", aparm->help);
-#endif
- } else if (aparm->help) {
- printf(" <%s>", aparm->help);
- if (aparm->type == CMD_LIST)
- printf("+");
- } else if (aparm->type == CMD_SINGLE)
- printf(" <arg>");
- else if (aparm->type == CMD_LIST)
- printf(" <arg>+");
+ return strdup("");
+ } else {
+ asprintf(&str, " %s<%s>%s%s",
+ aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
+ aparm->help?aparm->help:"arg",
+ aparm->type == CMD_LIST?"+":"",
+ aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
+ return str;
+ }
}
extern char *AFSVersion;
{
int i;
struct cmd_parmdesc *tp;
+ char *str;
+ size_t len;
+ size_t xtralen;
/* now print usage, from syntax table */
if (noOpcodes)
- printf("Usage: %s", as->a0name);
+ asprintf(&str, "Usage: %s", as->a0name);
else {
if (!strcmp(as->name, initcmd_opcode))
- printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
+ asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
else
- printf("Usage: %s%s", NName(as->a0name, " "), as->name);
+ asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
}
+ len = strlen(str);
+ printf("%s", str);
+ free(str);
+
for (i = 0; i < CMD_MAXPARMS; i++) {
tp = &as->parms[i];
if (tp->type == 0)
continue; /* seeked over slot */
if (tp->flags & CMD_HIDE)
continue; /* skip hidden options */
- printf(" ");
- if (tp->flags & CMD_OPTIONAL)
- printf("[");
- printf("%s", tp->name);
- PrintParmHelp(tp);
- if (tp->flags & CMD_OPTIONAL)
- printf("]");
+ /* Work out if we can fit what we want to on this line, or if we need to
+ * start a new one */
+ str = ParmHelpString(tp);
+ xtralen = 1 + strlen(tp->name) + strlen(str) +
+ ((tp->flags & CMD_OPTIONAL)? 2: 0);
+
+ if (len + xtralen > 78) {
+ printf("\n ");
+ len = 8;
+ }
+
+ printf(" %s%s%s%s",
+ tp->flags & CMD_OPTIONAL?"[":"",
+ tp->name,
+ str,
+ tp->flags & CMD_OPTIONAL?"]":"");
+ free(str);
+ len+=xtralen;
}
printf("\n");
}
} else
tp->help = NULL;
+ tp->aliases = NULL;
+
if (as->nParms <= ref)
as->nParms = ref+1;
return cmd_AddParmAtOffset(as, aname, atype, aflags, ahelp, as->nParms++);
}
+int
+cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
+{
+ struct cmd_item *item;
+
+ if (pos > as->nParms)
+ return CMD_EXCESSPARMS;
+
+ item = calloc(1, sizeof(struct cmd_item));
+ item->data = strdup(alias);
+ item->next = as->parms[pos].aliases;
+ as->parms[pos].aliases = item;
+
+ return 0;
+}
+
/* add a text item to the end of the parameter list */
static int
AddItem(struct cmd_parmdesc *aparm, char *aval)
tp = as->parms;
for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
switch (tp->type) {
+ case CMD_SINGLE_OR_FLAG:
+ if (tp->items == &dummy)
+ break;
+ /* Deliberately fall through here */
case CMD_SINGLE:
case CMD_LIST:
/* free whole list in both cases, just for fun */
continue;
}
- if (tparm->type == CMD_SINGLE) {
+ if (tparm->type == CMD_SINGLE ||
+ tparm->type == CMD_SINGLE_OR_FLAG) {
if (tparm->items) {
fprintf(stderr, "%sToo many values after switch %s\n",
NName(pname, ": "), tparm->name);
if (tparm->type == 0)
continue; /* Skipped parm slot */
if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
- fprintf(stderr, "%s The field '%s' isn't completed properly\n",
+ if (tparm->type == CMD_SINGLE_OR_FLAG) {
+ tparm->items = &dummy;
+ } else {
+ fprintf(stderr, "%s The field '%s' isn't completed properly\n",
NName(pname, ": "), tparm->name);
- code = CMD_TOOFEW;
- goto out;
+ code = CMD_TOOFEW;
+ goto out;
+ }
}
if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
fprintf(stderr, "%sMissing required parameter '%s'\n",
if (syn->parms[pos].items == NULL ||
syn->parms[pos].items->data == NULL)
return CMD_MISSING;
+ if (syn->parms[pos].items == &dummy)
+ return 0;
+
*value = strtol(syn->parms[pos].items->data, NULL, 10);
return 0;
return CMD_EXCESSPARMS;
if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
return CMD_MISSING;
+ if (syn->parms[pos].items == &dummy)
+ return 0;
if (*value)
free(*value);
+
*value = strdup(syn->parms[pos].items->data);
return 0;