Getopt modules 03: Getopt::Long::Descriptive

About this mini-article series. Each day for 24 days, I will be reviewing a module that parses command-line options (such module is usually under the Getopt::* namespace). First article is here.

Note that from this day on, all the reviewed modules are non-core since only Getopt::Std and Getopt::Long are core modules. The choice of using these modules must take into account this factor, as your user must bear an additional cost of installing the module from CPAN (unless your application bundles the module).

Some of these modules are wrappers for Getopt::Long, either because the author wants to offer a different interface and/or add some missing features. Some of the modules are higher-level: they are more than mere option parsing modules, usually a CLI framework.

Among the missing features often added is the ability to generate usage message (and the other common one is the ability to parse commands/subcommands). When using Getopt::Long, one already specifies a list of options. But there is no way to add a summary string for each option, making it impossible to create a useful/nice usage message. The modules solve this problem either by allowing user to specify the per-option summary string, or using/parsing user-supplied usage text/POD.

Getopt::Long::Descriptive is one such module: it allows you to specify per-option summary string, as well as default value for an option and whether an option is required. Judging from the number of reverse dependencies, Getopt::Long::Descriptive is the fourth most popular option parsing module on CPAN with 64 reverse dependencies (after Getopt::Long with 1127, Getopt::Std with 167, and MooseX::Getopt with 134). I also have actually reviewed Getopt::Long::Descriptive in one of my Perinci::CmdLine tutorial posts.

Aside my minor nitpick as described in the linked post, there are two additional notes: Getopt::Long::Descriptive depends on another non-core module Sub::Exporter, and its startup is ~twice that of Getopt::Long:

| participant               | time (ms) | mod_overhead_time (ms) |
|—————————+———–+————————|
| Getopt::Long::Descriptive | 36 | 33.9 |
| Getopt::Long | 15 | 12.9 |
| Getopt::Std | 3.8 | 1.7 |
| perl -e1 (baseline) | 2.1 | 0 |

Not that this should be a concern to most. If you use Getopt::Long, Getopt::Long::Descriptive is pretty recommended.

Tab completion. if you have a Getopt::Long::Descriptive-based CLI script, your users can now also use shcompgen to get tab completion, because shcompgen now supports detecting Getopt::Long::Descriptive-based scripts and activating tab completion for such scripts. In shells like fish and zsh, the description for each option will even be shown.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s