[Toybox] --help

Isaac Dunham idunham at lavabit.com
Fri Apr 5 21:18:52 PDT 2013


On Fri, 5 Apr 2013 16:12:18 -0400
Jeremy Huntwork <jhuntwork at lightcubesolutions.com> wrote:

> Hi, I'm a bit confused about how help text should operate. To me it would make sense to be a universal option which toybox handled for every command, but I'm seeing unexpected results.
> 
> For example:
> 
> toybox # ln -s toybox sha1sum
> toybox # ./sha1sum
> ^C
> 
> OK, so sha1sum is expecting a file name as the first argument, seems reasonable. But doesn't it allow specific keywords?
> 
> toybox # ./sha1sum --help
> sha1sum: --help: No such file or directory
(snip)
> Guess not. Maybe toybox does?
> 
> toybox # ./toybox help sha1sum
> usage: md5sum [FILE]...
> 
> Calculate md5 hash for each input file, reading from stdin if none.
> Output one hash (16 hex digits) for each input file, followed by
> filename.
> 
> OK, good, but it's the wrong help text, apparently because md5sum and sha1sum share the same c file:

(snip)

> Also, I find it interesting on some commands that --help is an unknown flag, which then actually does what I want and spits out the usage:
> 
> toybox # ln -s toybox mkdir
> toybox # ./mkdir --help
> usage: mkdir [-p] [-m mode] [dirname...]
> Create one or more directories.
> 
> -p  make parent directories as needed.
> -m  set permissions of directory to mode.
> 
> mkdir: Unknown option help
> 
> Is there an acceptable way to standardize this across all toys?

To some extent, there will be variation.
But I was looking at ifconfig and found it had an interesting approach:
static void show_help(void)
{
  char **arg = xzalloc(sizeof(char*) *3);
  arg[0] = "help";
  arg[1] = xstrdup(toys.which->name);
  toy_exec(arg);
}
..
 if(argv[0] && (strcmp(argv[0], "--help") == 0))
  show_help(); 

It seems to me that show_help() might be suitable for inclusion in lib/.

In theory, one could make the main logic handle <toy> --help like help <toy>, but it would be impossible to implement POSIX and still support 
help with -h in all cases where it's appropriate (I remember one or two commands use -h for other purposes, though I don't remember which).

Of course the 
char **argv = toys.optargs; 
looks rather ugly to me...
-- 
Isaac Dunham <idunham at lavabit.com>


 1365221932.0


More information about the Toybox mailing list