Getopt modules 01: Getopt::Long

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).

Today, our module is Getopt::Long. I actually have reviewed it in a couple of my Perinci::CmdLine tutorial posts: Getopt::Long and What’s wrong with Getopt::Long. To recap: Getopt::Long is a core module that should be your go-to module for parsing command-line options.

Two things to remember. First, you should start your code with something like this:

Getopt::Long::Configure("bundling", "no_ignore_case", "permute", "no_getopt_compat");

bundling is to enable you to say -abc instead of -a -b -c if you happen to have these short options. Most Unix programs allow this. no_ignore_case is so that Getopt::Long differentiates -v and -V. Most Unix programs also behave like this, they are not case-insensitive when it comes to command-line options. Since there are only so many Latin letters, very often the lowercase letter and uppercase letter are used for different purposes.

permute is to allow you to say --option val --flag arg1 arg2 --another-flag. That is, you intersperse command-line options and arguments. For convenience, many Unix programs behave like this. For example, you can say ls -l A* or ls A* -l. By default (under no_permute mode), Getopt::Options requires a user to specify all options first before any argument, which is rather inconvenient.

no_getopt_compat is to disable interpreting +foo the same as --foo. Most programs nowaday do not interpret + as the start of command-line options anymore. Enabling getopt_compat (the default) only serves to interfere, for example if you have a filename like +foo then you’ll have to write it as ./+foo to avoid it being parsed as command-line options.

These modes should be the default, right? Just like use strict and use warnings should be the default in Perl. But for the sake of backward compatibility, they aren’t.

Tab completion. Another thing I want to add is: if you have a Getopt::Long-based CLI script, aside from modifying your script to use Getopt::Long::Complete instead, your users can now also use shcompgen to get tab completion, because shcompgen now supports detecting Getopt::Long-based scripts and activating tab completion for such scripts.

Advertisements

13 thoughts on “Getopt modules 01: Getopt::Long

  1. Pingback: Getopt modules 02: Getopt::Std | perlancar's blog

  2. Pingback: Getopt modules 08: Getopt::Tiny | perlancar's blog

  3. Pingback: Getopt modules 09: Getopt::Declare, Getopt::Euclid, Docopt | perlancar's blog

  4. Pingback: Getopt modules 10: Getopt::Auto | perlancar's blog

  5. Pingback: Getopt modules 14: MooseX::Getopt | perlancar's blog

  6. Pingback: Getopt modules 15: MooX::Options | perlancar's blog

  7. Pingback: Getopt modules 17: Getopt::Modular | perlancar's blog

  8. Pingback: Getopt modules 18: App::Cmd | perlancar's blog

  9. Pingback: Getopt modules 19: App::Spec | perlancar's blog

  10. Pingback: Getopt modules 20: Smart::Options | perlancar's blog

  11. Pingback: Getopt modules 21: Getopt::ArgParse | perlancar's blog

  12. Pingback: Getopt modules 22: Getopt::Kingpin | perlancar's blog

  13. Pingback: Getopt modules 23: Getopt::Complete | perlancar's blog

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 )

Google+ photo

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

Connecting to %s