blog/content/post/2009/11/04/2009-11-04-00001258.md

53 lines
3.8 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: Getopt::Long と Pod::Usage の使用例
author: kazu634
date: 2009-11-04
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4903;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
CPANでの説明そのものですけど、貼り付けておきますね。
</p>
<pre class="syntax-highlight">
<span class="synComment"># === Libraries ===</span>
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>Getopt::Long;
<span class="synStatement">use </span>Pod::Usage;
<span class="synStatement">my</span> <span class="synIdentifier">$man</span> = <span class="synConstant"></span>;
<span class="synStatement">my</span> <span class="synIdentifier">$help</span> = <span class="synConstant"></span>;
<span class="synComment">## Parse options and print usage if there is a syntax error,</span>
<span class="synComment">## or if usage was explicitly requested.</span>
GetOptions( <span class="synConstant">'help|?'</span> =&#62; <span class="synIdentifier">\$help</span>, <span class="synConstant">man </span>=&#62; <span class="synIdentifier">\$man</span> ) <span class="synStatement">or</span> pod2usage(<span class="synConstant">2</span>);
pod2usage(<span class="synConstant">1</span>) <span class="synStatement">if</span> <span class="synIdentifier">$help</span>;
pod2usage( -<span class="synConstant">verbose </span>=&#62; <span class="synConstant">2</span> ) <span class="synStatement">if</span> <span class="synIdentifier">$man</span>;
<span class="synComment">## If no arguments were given, then allow STDIN to be used only</span>
<span class="synComment">## if it's not connected to a terminal (otherwise print usage)</span>
pod2usage(<span class="synConstant">&#34;</span><span class="synIdentifier">$0</span><span class="synConstant">: No files given.&#34;</span>) <span class="synStatement">if</span> ( ( <span class="synIdentifier">@ARGV</span> == <span class="synConstant"></span> ) &#38;&#38; ( <span class="synStatement">-t</span> STDIN ) );
<span class="synComment">__END__</span>
<span class="synStatement">=head1</span><span class="synConstant"> NAME</span>
<span class="synComment">sample - Using GetOpt::Long and Pod::Usage</span>
<span class="synStatement">=head1</span><span class="synConstant"> SYNOPSIS</span>
<span class="synComment">sample [options] [file ...]</span>
<span class="synComment">Options:</span>
<span class="synComment">-help brief help message</span>
<span class="synComment">-man full documentation</span>
<span class="synStatement">=head1</span><span class="synConstant"> OPTIONS</span>
<span class="synStatement">=over</span><span class="synComment"> </span><span class="synConstant">8</span>
<span class="synStatement">=item</span><span class="synConstant"> </span><span class="synIdentifier">B&#60;-help&#62;</span>
<span class="synComment">Print a brief help message and exits.</span>
<span class="synStatement">=item</span><span class="synConstant"> </span><span class="synIdentifier">B&#60;-man&#62;</span>
<span class="synComment">Prints the manual page and exits.</span>
<span class="synStatement">=back</span>
<span class="synStatement">=head1</span><span class="synConstant"> DESCRIPTION</span>
<span class="synIdentifier">B&#60;This program&#62;</span><span class="synComment"> will read the given input </span><span class="synIdentifier">file(s)</span><span class="synComment"> and do something</span>
<span class="synComment">useful with the contents thereof.</span>
<span class="synStatement">=cut</span>
</pre>
</div>