What are the best-practices for implementing a CLI tool in Perl?

后端 未结 7 575
刺人心
刺人心 2021-01-30 00:30

I am implementing a CLI tool using Perl. What are the best-practices we can follow here?

相关标签:
7条回答
  • 2021-01-30 00:42

    The most important thing is to have standard options.

    Don't try to be clever, be simply consistent with already existing tools.

    How to achieve this is also important, but only comes second.

    Actually, this is quite generic to all CLI interfaces.

    0 讨论(0)
  • 2021-01-30 00:43

    The following points aren't specific to Perl but I've found many Perl CL scripts to be deficient in these areas:

    1. Use common command line options. To show the version number implement -v or --version not --ver. For recursive processing -r (or perhaps -R although in my Gnu/Linux experience -r is more common) not --rec. People will use your script if they can remember the parameters. It's easy to learn a new command if you can remember "it works like grep" or some other familiar utility.

    2. Many command line tools process "things" (files or directories) within the "current directory". While this can be convenient make sure you also add command line options for explicitly identifying the files or directories to process. This makes it easier to put your utility in a pipeline without developers having to issue a bunch of cd commands and remember which directory they're in.

    0 讨论(0)
  • 2021-01-30 00:44

    Use POD to document your tool, follow the guidelines of manpages; include at least the following sections: NAME, SYNOPSIS, DESCRIPTION, AUTHOR. Once you have proper POD you can generate a man page with pod2man, view the documentation at the console with perldoc your-script.pl.

    Use a module that handles command line options for you. I really like using Getopt::Long in conjunction with Pod::Usage this way invoking --help will display a nice help message.

    Make sure that your scripts returns a proper exit value if it was successful or not.

    Here's a small skeleton of a script that does all of these:

    #!/usr/bin/perl
    
    =head1 NAME
    
    simplee - simple program
    
    =head1 SYNOPSIS
    
        simple [OPTION]... FILE...
    
        -v, --verbose  use verbose mode
        --help         print this help message
    
    Where I<FILE> is a file name.
    
    Examples:
    
        simple /etc/passwd /dev/null
    
    =head1 DESCRIPTION
    
    This is as simple program.
    
    =head1 AUTHOR
    
    Me.
    
    =cut
    
    use strict;
    use warnings;
    
    use Getopt::Long qw(:config auto_help);
    use Pod::Usage;
    
    exit main();
    
    sub main {
    
        # Argument parsing
        my $verbose;
        GetOptions(
            'verbose'  => \$verbose,
        ) or pod2usage(1);
        pod2usage(1) unless @ARGV;
        my (@files) = @ARGV;
    
        foreach my $file (@files) {
            if (-e $file) {
                printf "File $file exists\n" if $verbose;
            }
            else {
                print "File $file doesn't exist\n";
            }
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-30 00:50

    You should use Perl modules to make your code reusable and easy to understand.
    should have a look at Perl best practices

    0 讨论(0)
  • 2021-01-30 00:51

    There are a couple of modules on CPAN that will make writing CLI programs a lot easier:

    • App::CLI
    • App::Cmd


    If you app is Moose based also have a look at MooseX::Getopt and MooseX::Runnable

    0 讨论(0)
  • 2021-01-30 00:52

    Some lessons I've learned:

    1) Always use Getopt::Long

    2) Provide help on usage via --help, ideally with examples of common scenarios. It helps people don't know or have forgotten how to use the tool. (I.e., you in six months).

    3) Unless it's pretty obvious to the user as why, don't go for long period (>5s) without output to the user. Something like 'print "Row $row...\n" unless ($row % 1000)' goes a long way.

    4) For long running operations, allow the user to recover if possible. It really sucks to get through 500k of a million, die, and start over again.

    5) Separate the logic of what you're doing into modules and leave the actual .pl script as barebones as possible; parsing options, display help, invoking basic methods, etc. You're inevitably going to find something you want to reuse, and this makes it a heck of a lot easier.

    0 讨论(0)
提交回复
热议问题