diff options
| author | Rob Landley <rob@landley.net> | 2021-06-18 08:57:07 -0500 | 
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2021-06-18 08:57:07 -0500 | 
| commit | 29e7ed94a1fe2836d38960b9b3ab3e3b7e61c791 (patch) | |
| tree | db99e26587f20bc129add53e7c236ea31c5e55f1 | |
| parent | 39f64538c2f5f56918d3352bc8a03aae6be0d1d9 (diff) | |
| download | toybox-29e7ed94a1fe2836d38960b9b3ab3e3b7e61c791.tar.gz | |
Second attempt at making --help work with alias ls="ls --color"
| -rw-r--r-- | lib/args.c | 3 | ||||
| -rw-r--r-- | main.c | 34 | ||||
| -rw-r--r-- | toys.h | 1 | 
3 files changed, 23 insertions, 15 deletions
| @@ -426,6 +426,9 @@ void get_optflags(void)            continue;          } +        if (CFG_TOYBOX_HELP_DASHDASH && !(toys.which->flags&TOYFLAG_NOHELP)) +          check_help(toys.argv+gof.argc); +          // do we match a known --longopt?          for (lo = gof.longopts; lo; lo = lo->next) {            if (!strncmp(gof.arg, lo->str, lo->len)) { @@ -67,6 +67,21 @@ static void unknown(char *name)    help_exit("Unknown command %s", name);  } +void check_help(char **arg) +{ +  if (!strcmp(*arg, "--help")) { +    if (CFG_TOYBOX && toys.which == toy_list && arg[1]) +      if (!(toys.which = toy_find(arg[1]))) unknown(arg[1]); +    show_help(stdout, 1); +    xexit(); +  } + +  if (!strcmp(*arg, "--version")) { +    xprintf("toybox %s\n", toybox_version); +    xexit(); +  } +} +  // Setup toybox global state for this command.  void toy_singleinit(struct toy_list *which, char *argv[])  { @@ -74,23 +89,12 @@ void toy_singleinit(struct toy_list *which, char *argv[])    toys.argv = argv;    toys.toycount = ARRAY_LEN(toy_list); -  // Parse --help and --version for (almost) all commands -  if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) { -    if (!strcmp(argv[1], "--help")) { -      if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) -        if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]); -      show_help(stdout, 1); -      xexit(); -    } - -    if (!strcmp(argv[1], "--version")) { -      xprintf("toybox %s\n", toybox_version); -      xexit(); -    } -  } -    if (NEED_OPTIONS && which->options) get_optflags();    else { +    // Parse --help and --version for (almost) all commands +    if (CFG_TOYBOX_HELP_DASHDASH && !(which->flags & TOYFLAG_NOHELP) && argv[1]) +      check_help(argv+1); +      toys.optargs = argv+1;      for (toys.optc = 0; toys.optargs[toys.optc]; toys.optc++);    } @@ -83,6 +83,7 @@  struct toy_list *toy_find(char *name);  void toy_init(struct toy_list *which, char *argv[]); +void check_help(char **arg);  void toy_singleinit(struct toy_list *which, char *argv[]);  void toy_exec(char *argv[]); | 
