[Toybox] Allow --help after other arguments?

Rob Landley rob at landley.net
Sun Jun 13 03:05:40 PDT 2021


On 6/11/21 4:51 PM, Ryan Prichard wrote:
> On Thu, Jun 10, 2021 at 7:05 PM Rob Landley <rob at landley.net
> <mailto:rob at landley.net>> wrote:
>     to show the help for ls. And DON'T want "chroot dir ls --help" to show help
>     output for chroot. (Or time. Or nice. Or nohup...)
> 
> 
> Yeah, I think my ideal would be for --help to apply to the last (sub)command
> preceding it. e.g. When it appears after a subcommand name, it would be parsed
> like any other subcommand flag. For "time", flag parsing (for -p and -v) seems
> to stop at the first non-hyphenated argument.

Which pulls in the argument parsing as a prerequisite for --help support in
commands like dd, expr, test...

Sigh. It's possible this is an acceptable cost, I'd just really like to avoid it
if I can. It's that whole "project serving two masters" thing again. The
embedded space is weird. Yes you can fit the computing power of a supercomputer
into a wristwatch, at the expense of the watch's battery life being about 3
hours of actual usage and 12 hours on standby. My mental model has been the
"free toy inside" of a cereal box running two chips off a watch battery. A
modern version of the blinking LED that was in everything in the 90's. [1]

> Aside: "toybox toybox toybox toybox ls" doesn't seem to work for me:
> 
> $ toybox toybox toybox toybox ls --help
> toybox: Unknown command toybox (see "toybox --help")

Ah, I forgot commit 28c9710edf8b changed that.

>     The reason that --help isn't in the argument parsing plumbing is because then
>     commands that don't have arguments wouldn't support --help, not without the
>     argument parsing plumbing being unable to drop out when building small binaries.
> 
> 
> I'm wondering when it's useful to drop the argument parsing plumbing. Is there a
> configuration where CFG_TOYBOX is off, and there is one binary for one command?

You can make individual command names, or "make change" to do each individual
command in a loop. (Both are listed in "make help".)

  $ make crc32
  ...
  $ nm --size-sort generated/unstripped/crc32

(Might wanna pipe that to grep -v ' [Bb] ' because bss only exists at runtime,
not in the binary...)

> Most of the commands take arguments, so I'd think most builds of the toybox
> binary would need some amount of the plumbing.

Most builds use the multiplexer where the infrastructure is shared between all
the commands, but a lot of small systems cherry pick just the commands they
need. Still, the list of commands that when build standalone don't include
lib/args.c is currently:

  $ grep 'NEWTOY(' toys/*/*.c | grep -v '["]' | grep -v NOHELP | \
    grep -v NOFORK | sed 's/.*NEWTOY(\([^,]*\),.*/\1/p' | sort -u | xargs
  arch ascii bootchartd bzcat clear count crc32 dd demo_scankey demo_utf8towc
  dos2unix expr factor groups hello ip lsmod lsusb more readahead reset rev sync
  tac unix2dos w xzcat yes

Which isn't a huge list. (And half that's in pending or example anyway.)

I suppose I could make if/else logic for the drop out case still supporting
--help. I wince at this but lemme think about it.

>     > $ ./tar -c README > --help && ./tar -tvf --help
>     > <prints "toybox tar" usage with the new --help behavior. previously toybox
>     > behaved like GNU tar above.>
> 
>     And now you know why I had it the way it was. It sounds like what you're saying
>     is that the toysh "alias" command should be --help aware for builtins.
> 
> Ah, I don't think I'd make a special case for alias... not sure how that would work?

I'm writing a shell as we speak, so... :)

(Alias is in the near term todo heap but I need to get through job control
first. Job control is a gordian knot I don't get to cut, but there are some
interesting solvents...)

> So... I don't actually use alias on Android devices. I had suggested it as an
> example that might motivate "ls -l --help" more strongly than convenience and
> expectation. I'm happy enough just editing the --help into the right place on
> the command line.

I try to have multiple ways:

  toybox --help ls
  toybox ls --help
  ls --help
  help ls
  toybox help ls

Plus the "man" command someday...

> -Ryan
> 

Rob

Footnote 1:

We've run j-core with an ice-40 wired to an spi flash chip and had it export an
spi interface to read from a calculator keyboard/display running off a stack of
3 watch batteries. We haven't YET taped the ice40/spi pair to the front a watch
battery and added an LED and IR sensor as a proof of concept to have a coin cell
run gnuchess all by itself, but that's mostly because you'd have to stack 3 coin
cells to get the voltage the ice40 wants (and adding voltage converter circuitry
would defeat the "two chips scotch taped to a battery bigger than the chips are"
bragging rights thing), so that'll have to wait for an asic SOC version...

You can get tiny processors for 3 cents
(https://www.youtube.com/watch?v=VYhAGnsnO7w) and a quick search found coin cell
batteries for 36 cents each retail
(https://www.amazon.com/Panasonic-Cr2032-Lithium-Battery-Ecr2032/dp/B00REWNF2M/ref=asc_df_B00REWNF2M/)
presumably cheaper in bulk. (I occasionally look at
htts://landley.net/notes-2017.html#30-09-2017 and such but "woman invents a
thing, white dude takes credit, his company gets sued for treating employees
terribly and goes bankrupt" is a sadly common story...)


More information about the Toybox mailing list