hzeller / rpi-rgb-led-matrix

Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi GPIO
GNU General Public License v2.0
3.64k stars 1.16k forks source link

Command line processing - how to invoke usage #1701

Open davepl opened 3 weeks ago

davepl commented 3 weeks ago

Below is my code to process options, but my expectation was that if the. user were to provide --help or -? that it would return false and hence print usage.

Is that not the case? Or do I have a bug?

    rgb_matrix::RuntimeOptions runtime_opt;
    if (!rgb_matrix::ParseOptionsFromFlags(&argc, &argv, &matrix_options, &runtime_opt)) 
        return usage(argv[0]);
    runtime_opt.gpio_slowdown = kDefaultGPIOSlowdown;
davepl commented 3 weeks ago

BTW, I'm guessing it's because the options code expects everything to be preceded by --led, so --led-help works, but seems like a bug to me!

hzeller commented 3 weeks ago

the rgb-matrix-flag parsing is done to least interfere with whatever the user needs in addition and processes with getopt(), this is why all flags have the --led-* prefix. The matrix does not go ahead and by itself print their usage of flags, as it is typically used inside another program that has their own flag processing and usage() print. But the library provides a way to print its options that you can call inside your usage():

rgb_matrix::PrintMatrixFlags(stderr);

There are a couple of examples how flags are handled in the utils/ directory that might be helpful.

(BTW in your example I see that you first call the flags then set the runtime_opt.gpio_slowdown. Typically one would do that the other way around as you'd set some useful defaults, but then allow the user to override them on the command line. Depends of course; sometimes one wants not to have a particular value messed up).