Last active
December 21, 2021 01:09
-
-
Save akanehara/f91a368e8b9176564691b9f21ef05f59 to your computer and use it in GitHub Desktop.
CLIツール雛形 (Perl)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# use lib "."; # Module search path | |
use File::Basename qw/basename dirname/; | |
use Pod::Usage; | |
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat); | |
# see http://tagomoris.hatenablog.com/entry/20120918/1347991165 | |
{ $|=1; my $_old = select(STDERR); $|=1; select($_old);} | |
# suppress stdout/stderr buffering | |
my $SCRIPT = basename $0; | |
my $help; | |
my $manual; | |
my $flag = 0; | |
my $integer = 42; | |
my $string = 'string-value'; | |
my @multiple; | |
GetOptions( | |
'help' => \$help, | |
'manual' => \$manual, | |
'flag!' => \$flag, # --noflag available | |
'integer|i=i' => \$integer, | |
'string|s=s' => \$string, | |
'multiple|m=s' => \@multiple, | |
'function' => => sub { ; }, | |
) or pod2usage(exitval => 2); | |
pod2usage(exitval => 0, verbose => 2) if $manual; # use pager | |
pod2usage(exitval => 0, verbose => 1) if $help; | |
my @args = @ARGV; | |
# Entry point | |
{ | |
local $" = ' '; | |
print "Options: integer=$integer flag=$flag string=$string multiple=(@multiple)\n"; | |
print "Args: (@args)\n"; | |
print "done.\n"; | |
} | |
__END__ | |
=head1 NAME | |
$SCRIPT - Perl CLI tool template. | |
=head1 SYNOPSIS | |
$SCRIPT [options] [arg ...] | |
Options: | |
--help | |
-i / --integer | |
-s / --string | |
-m / --multiple | |
--flag | |
--noflag | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment