向脚本添加帮助命令

use*_*367 5 documentation perl getopt-long command-line-arguments perl-pod

是否有一种向脚本添加帮助功能的标准方法?最简单的方法可能是参数并打印一些文本,如果它是"-help"或其他东西.有没有人有关于如何做到这一点的任何例子?

谢谢!

cre*_*ive 11

考虑Getopt :: Long plus Pod :: Usage.我编写CLI工具的常用模式:

#!/usr/bin/env perl
# ABSTRACT: Short tool description
# PODNAME: toolname
use autodie;
use strict;
use utf8;
use warnings qw(all);

use Getopt::Long;
use Pod::Usage;

# VERSION

=head1 SYNOPSIS

    toolname [options] files

=head1 DESCRIPTION

...

=cut

GetOptions(
    q(help)             => \my $help,
    q(verbose)          => \my $verbose,
) or pod2usage(q(-verbose) => 1);
pod2usage(q(-verbose) => 1) if $help;

# Actual code below
Run Code Online (Sandbox Code Playgroud)


Mat*_*eck 1

我这样做的方法是利用从命令行参数中Getopt::Std查找标志。-h

use strict;
use warnings;
use Getopt::Std;

my %args;
getopts('h', \%args);

my $help = "help goes here. You can use
more than one line to format the text";

die $help if $args{h};
# otherwise continue with script...
Run Code Online (Sandbox Code Playgroud)

更复杂的方法是使用POD::usage,尽管我个人没有尝试过这种方法。