r13740 - in /branches/upstream/libmoosex-getopt-perl/current: ChangeLog META.yml Makefile.PL lib/MooseX/Getopt.pm
gregoa-guest at users.alioth.debian.org
gregoa-guest at users.alioth.debian.org
Sun Jan 27 22:16:10 UTC 2008
Author: gregoa-guest
Date: Sun Jan 27 22:16:09 2008
New Revision: 13740
URL: http://svn.debian.org/wsvn/?sc=1&rev=13740
Log:
[svn-upgrade] Integrating new upstream version, libmoosex-getopt-perl (0.10)
Modified:
branches/upstream/libmoosex-getopt-perl/current/ChangeLog
branches/upstream/libmoosex-getopt-perl/current/META.yml
branches/upstream/libmoosex-getopt-perl/current/Makefile.PL
branches/upstream/libmoosex-getopt-perl/current/lib/MooseX/Getopt.pm
Modified: branches/upstream/libmoosex-getopt-perl/current/ChangeLog
URL: http://svn.debian.org/wsvn/branches/upstream/libmoosex-getopt-perl/current/ChangeLog?rev=13740&op=diff
==============================================================================
--- branches/upstream/libmoosex-getopt-perl/current/ChangeLog (original)
+++ branches/upstream/libmoosex-getopt-perl/current/ChangeLog Sun Jan 27 22:16:09 2008
@@ -1,4 +1,13 @@
Revision history for Perl extension MooseX-Getopt
+
+0.10 Wed. Jan. 23, 2008
+ * MooseX::Getopt
+ - Support default configfile attribute when
+ used with MooseX::ConfigFromFile-based roles
+
+0.09 Tues. Jan. 22, 2008
+ * MooseX::Getopt
+ - Use Getopt::Long::Descriptive if it's available
0.08 Tues. Dec. 8, 2007
* MooseX::Getopt
Modified: branches/upstream/libmoosex-getopt-perl/current/META.yml
URL: http://svn.debian.org/wsvn/branches/upstream/libmoosex-getopt-perl/current/META.yml?rev=13740&op=diff
==============================================================================
--- branches/upstream/libmoosex-getopt-perl/current/META.yml (original)
+++ branches/upstream/libmoosex-getopt-perl/current/META.yml Sun Jan 27 22:16:09 2008
@@ -1,6 +1,6 @@
---
name: MooseX-Getopt
-version: 0.08
+version: 0.10
author:
- 'Stevan Little E<lt>stevan at iinteractive.comE<gt>'
- 'Brandon L. Black, E<lt>blblack at gmail.comE<gt>'
@@ -17,7 +17,7 @@
provides:
MooseX::Getopt:
file: lib/MooseX/Getopt.pm
- version: 0.08
+ version: 0.10
MooseX::Getopt::Meta::Attribute:
file: lib/MooseX/Getopt/Meta/Attribute.pm
version: 0.04
@@ -29,7 +29,7 @@
version: 0.03
MooseX::Getopt::Strict:
file: lib/MooseX/Getopt/Strict.pm
-generated_by: Module::Build version 0.2808
+generated_by: Module::Build version 0.280801
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.2.html
version: 1.2
Modified: branches/upstream/libmoosex-getopt-perl/current/Makefile.PL
URL: http://svn.debian.org/wsvn/branches/upstream/libmoosex-getopt-perl/current/Makefile.PL?rev=13740&op=diff
==============================================================================
--- branches/upstream/libmoosex-getopt-perl/current/Makefile.PL (original)
+++ branches/upstream/libmoosex-getopt-perl/current/Makefile.PL Sun Jan 27 22:16:09 2008
@@ -1,4 +1,4 @@
-# Note: this file was auto-generated by Module::Build::Compat version 0.03
+# Note: this file was auto-generated by Module::Build::Compat version 0.2808_01
use ExtUtils::MakeMaker;
WriteMakefile
(
Modified: branches/upstream/libmoosex-getopt-perl/current/lib/MooseX/Getopt.pm
URL: http://svn.debian.org/wsvn/branches/upstream/libmoosex-getopt-perl/current/lib/MooseX/Getopt.pm?rev=13740&op=diff
==============================================================================
--- branches/upstream/libmoosex-getopt-perl/current/lib/MooseX/Getopt.pm (original)
+++ branches/upstream/libmoosex-getopt-perl/current/lib/MooseX/Getopt.pm Sun Jan 27 22:16:09 2008
@@ -1,18 +1,19 @@
package MooseX::Getopt;
use Moose::Role;
-
-use Getopt::Long ();
use MooseX::Getopt::OptionTypeMap;
use MooseX::Getopt::Meta::Attribute;
use MooseX::Getopt::Meta::Attribute::NoGetopt;
-our $VERSION = '0.08';
+use Getopt::Long (); # GLD uses it anyway, doesn't hurt
+use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive };
+
+our $VERSION = '0.10';
our $AUTHORITY = 'cpan:STEVAN';
-has ARGV => (is => 'rw', isa => 'ArrayRef');
-has extra_argv => (is => 'rw', isa => 'ArrayRef');
+has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
+has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
sub new_with_options {
my ($class, @params) = @_;
@@ -25,12 +26,23 @@
my $params = $processed{params};
- if($class->meta->does_role('MooseX::ConfigFromFile')
- && defined $params->{configfile}) {
- %$params = (
- %{$class->get_config_from_file($params->{configfile})},
- %$params,
- );
+ if($class->meta->does_role('MooseX::ConfigFromFile')) {
+ my $configfile;
+
+ if(defined $params->{configfile}) {
+ $configfile = $params->{configfile}
+ }
+ else {
+ my $cfmeta = $class->meta->get_attribute('configfile');
+ $configfile = $cfmeta->default if $cfmeta->has_default;
+ }
+
+ if(defined $configfile) {
+ %$params = (
+ %{$class->get_config_from_file($configfile)},
+ %$params,
+ );
+ }
}
$class->new(
@@ -46,6 +58,51 @@
local @ARGV = @{ $params{argv} || \@ARGV };
+ my ( $opt_spec, $name_to_init_arg ) = ( HAVE_GLD ? $class->_gld_spec(%params) : $class->_traditional_spec(%params) );
+
+ # Get a clean copy of the original @ARGV
+ my $argv_copy = [ @ARGV ];
+
+ my @err;
+
+ my ( $parsed_options, $usage ) = eval {
+ local $SIG{__WARN__} = sub { push @err, @_ };
+
+ if ( HAVE_GLD ) {
+ return Getopt::Long::Descriptive::describe_options($class->_usage_format(%params), @$opt_spec);
+ } else {
+ my %options;
+ Getopt::Long::GetOptions(\%options, @$opt_spec);
+ return ( \%options, undef );
+ }
+ };
+
+ die join "", grep { defined } @err, $@ if @err or $@;
+
+ # Get a copy of the Getopt::Long-mangled @ARGV
+ my $argv_mangled = [ @ARGV ];
+
+ my %constructor_args = (
+ map {
+ $name_to_init_arg->{$_} => $parsed_options->{$_}
+ } keys %$parsed_options,
+ );
+
+ return (
+ params => \%constructor_args,
+ argv_copy => $argv_copy,
+ argv => $argv_mangled,
+ ( defined($usage) ? ( usage => $usage ) : () ),
+ );
+}
+
+sub _usage_format {
+ return "usage: %c %o";
+}
+
+sub _traditional_spec {
+ my ( $class, %params ) = @_;
+
my ( @options, %name_to_init_arg, %options );
foreach my $opt ( @{ $params{options} } ) {
@@ -53,26 +110,28 @@
$name_to_init_arg{ $opt->{name} } = $opt->{init_arg};
}
- # Get a clean copy of the original @ARGV
- my $argv_copy = [ @ARGV ];
-
- {
- local $SIG{__WARN__} = sub { die $_[0] };
- Getopt::Long::GetOptions(\%options, @options);
+ return ( \@options, \%name_to_init_arg );
+}
+
+sub _gld_spec {
+ my ( $class, %params ) = @_;
+
+ my ( @options, %name_to_init_arg );
+
+ foreach my $opt ( @{ $params{options} } ) {
+ push @options, [
+ $opt->{opt_string},
+ $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
+ {
+ ( $opt->{required} ? (required => $opt->{required}) : () ),
+ ( exists $opt->{default} ? (default => $opt->{default}) : () ),
+ },
+ ];
+
+ $name_to_init_arg{ $opt->{name} } = $opt->{init_arg};
}
- # Get a copy of the Getopt::Long-mangled @ARGV
- my $argv_mangled = [ @ARGV ];
-
- return (
- argv_copy => $argv_copy,
- argv => $argv_mangled,
- params => {
- map {
- $name_to_init_arg{$_} => $options{$_}
- } keys %options,
- }
- );
+ return ( \@options, \%name_to_init_arg );
}
sub _compute_getopt_attrs {
@@ -107,8 +166,8 @@
if ($attr->has_type_constraint) {
my $type_name = $attr->type_constraint->name;
- if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {
- $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name);
+ if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {
+ $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name)
}
}
@@ -117,6 +176,7 @@
init_arg => $attr->init_arg,
opt_string => $opt_string,
required => $attr->is_required,
+ ( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
}
}
@@ -192,7 +252,12 @@
If your class also uses a configfile-loading role based on
L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
L<MooseX::Getopt>'s C<new_with_options> will load the configfile
-specified by the C<--configfile> option for you.
+specified by the C<--configfile> option (or the default you've
+given for the configfile attribute) for you.
+
+Options specified in multiple places follow the following
+precendence order: commandline overrides configfile, which
+overrides explicit new_with_options parameters.
=head2 Supported Type Constraints
@@ -313,6 +378,9 @@
If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
C<new_with_options> will throw an exception.
+If you have L<Getopt::Long::Descriptive> a the C<usage> param is also passed to
+C<new>.
+
=item B<ARGV>
This accessor contains a reference to a copy of the C<@ARGV> array
More information about the Pkg-perl-cvs-commits
mailing list