diff options
| author | Franck Cuny <franckcuny@gmail.com> | 2016-07-02 20:06:31 -0700 |
|---|---|---|
| committer | Franck Cuny <franckcuny@gmail.com> | 2016-07-02 20:06:31 -0700 |
| commit | 4b8e43f75b394a4e6169884fbfb4c606865c6a22 (patch) | |
| tree | 48cae6b8e8f9b68cae29676d8a15cb3ddbfcccda /content/post/2009-06-22-modules-i-like-getopt-long-and-moosex-getopt.md | |
| parent | Stop using Jekyll. (diff) | |
| download | lumberjaph-4b8e43f75b394a4e6169884fbfb4c606865c6a22.tar.gz | |
Import migration from Jekyll to Hugo.
All the posts were converted, and the layout is created. This looks like
it works just fine.
Diffstat (limited to '')
| -rw-r--r-- | content/post/2009-06-22-modules-i-like-getopt-long-and-moosex-getopt.md | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/content/post/2009-06-22-modules-i-like-getopt-long-and-moosex-getopt.md b/content/post/2009-06-22-modules-i-like-getopt-long-and-moosex-getopt.md new file mode 100644 index 0000000..10cb1b1 --- /dev/null +++ b/content/post/2009-06-22-modules-i-like-getopt-long-and-moosex-getopt.md @@ -0,0 +1,129 @@ +--- +date: 2009-06-22T00:00:00Z +summary: In which I talk about GetOpt::Long and Moosex::Getopt +title: Modules I like Getopt::Long and MooseX::Getopt +--- + +## Getopt::Long + +[Getopt::long](http://search.cpan.org/perldoc?Getopt::Long) is a useful module to parse command line arguements. + +A basic usage is something like this: + +```perl +#!/usr/bin/perl -w +use strict; +use YAML::Syck; +use Getopt::Long; + +GetOptions('config=s' => \my $cfg_file,); + +my $config = LoadFile $cfg_file +``` + +In **GetOptions**, we require a value for config with **config=s**. If we wante an integer, we replace 's' with 'i', and for a floating point, with 'f'. + +Call your script : + +```bash +% script.pl --config=file.yml #this one works +% script.pl --config file.yml #this one too! +% script.pl -c file.yml #and this one too +``` + +The three syntaxes are understood. + +A good practices is to combine this module with [Pod::Usage](http://search.cpan.org/perldoc?Pod::Usage). Let's do some modifications on the example: + +```perl +#!/usr/bin/perl -w +use strict; +use YAML::Syck; +use Getopt::Long; +use Pod::Usage; + +GetOptions('config=s' => \my $cfg_file,) or pod2usage(2); +pod2usage(2) unless @ARGV > 0; + +my $config = LoadFile $cfg_file + +__END__ +=head1 NAME + +uberscript + +=head1 SYNOPSIS + +uberscript [options] + +Options: + + --config config file + +=head1 Options + +=over 4 + +=item B<config> + +Path to the config file +``` + +then + +```bash +% perl uberscript + +Usage: + uberscript [options] + + Options: + --config config file +``` + +From now if we call our script without argument, the POD will be printed on STDIN. + +## MooseX::Getopt + +[MooseX::Getopt](http://search.cpan.org/perldoc?MooseX::Getopt) is a Role that add a `new_with_options` to your object. We create a basic Object : + +```perl +package OurShinyObject; + +use Moose; +with qw/MooseX::Getopt/; + +has 'config' => (isa => 'Str', is => 'ro', required => 1); +has 'context' => ( + isa => 'HashRef', + is => 'rw', + lazy => 1, + traits => ['NoGetopt'], + default => sub { LoadFile shift->config } +); + +... +``` + +create a script to call this object + +```perl +#!/usr/bin/perl -w +use strict; +use OurShinyObject; + +my $obj = OurShinyObject->new_from_options(); + +``` + +```sh +% script.pl --config file.yml +``` + +The role will set our attribute **context** using the value from the argument set on the command line. + +The `traits => ['NoGetopt']` indicate that this attributes will be not be read from the command line. An alternate way to do this is to prefix the attributes with **_**. + +## conclusion (?) + +When you write a script, even if you're sure you will never need to have more than one argument, or that you never will have to update the code, *please* consider to use of **Getopt::Long** instead of a `shift @ARGV`, because we all know that you will at a certain point update this script and you will more than one argument :). |
