[SCM] libmoox-options-perl Debian packaging branch, master, updated. debian/3.78-1-17-g6aa50a6

celogeek me at celogeek.com
Mon May 6 12:22:38 UTC 2013


The following commit has been merged in the master branch:
commit 5c851974a86fbc89257437742bb709d616be136b
Author: celogeek <me at celogeek.com>
Date:   Thu Apr 25 00:08:56 2013 +0200

    add json support

diff --git a/lib/MooX/Options.pm b/lib/MooX/Options.pm
index f3a583e..fdd68b2 100755
--- a/lib/MooX/Options.pm
+++ b/lib/MooX/Options.pm
@@ -17,7 +17,7 @@ use Carp;
 
 # VERSION
 my @OPTIONS_ATTRIBUTES
-    = qw/format short repeatable negativable autosplit doc order/;
+    = qw/format short repeatable negativable autosplit doc order json/;
 
 sub import {
     my ( undef, @import ) = @_;
@@ -126,9 +126,17 @@ sub _validate_and_filter_options {
     $options{doc} = $options{documentation} if !defined $options{doc};
     $options{order} = 0 if !defined $options{order};
 
+    if ($options{json}) {
+        delete $options{repeatable};
+        delete $options{autosplit};
+        delete $options{negativable};
+        $options{format} = 's';
+    }
+
     my %cmdline_options = map { ( $_ => $options{$_} ) }
         grep { exists $options{$_} } @OPTIONS_ATTRIBUTES, 'required';
 
+
     $cmdline_options{repeatable} = 1 if $cmdline_options{autosplit};
     $cmdline_options{format} .= "@"
         if $cmdline_options{repeatable}
@@ -447,6 +455,27 @@ Ex :
 
 Specified the order of the attribute.
 
+The order value is an integer.
+
+=item json
+
+The parameter will be treat like a json string.
+
+Ex :
+
+    {
+        package t;
+        use Moo;
+        use MooX::Options;
+
+        option 'hash' => (is => 'ro', json => 1);
+
+        1;
+    }
+    local @ARGV=('--hash', '{"a":1,"b":2}');
+    my $t = t->new_with_options;
+    t->hash # { a => 1, b => 2 }
+
 =head1 namespace::clean
 
 To use namespace::clean you need to add 2 methods as an exception. It is use by MooX::Options when you run the new_with_options methods.
diff --git a/lib/MooX/Options/Role.pm b/lib/MooX/Options/Role.pm
index 458a507..6f9386a 100644
--- a/lib/MooX/Options/Role.pm
+++ b/lib/MooX/Options/Role.pm
@@ -18,6 +18,7 @@ use Getopt::Long 2.38;
 use Getopt::Long::Descriptive 0.091;
 use Regexp::Common;
 use Data::Record;
+use JSON;
 
 
 requires qw/_options_data _options_config/;
@@ -74,11 +75,10 @@ sub parse_options {
     };
 
     my %has_to_split;
-    my @sorted_keys = sort {
-        $options_data{$a}{order} <=> $options_data{$b}{order} # sort by order
-            or $a cmp $b                                      # sort by attr name
-    } keys %options_data;
-    for my $name (@sorted_keys) {
+    for my $name (sort {
+                      $options_data{$a}{order} <=> $options_data{$b}{order} # sort by order
+                          or $a cmp $b                                      # sort by attr name
+                  } keys %options_data) {
         my %data = %{ $options_data{$name} };
         my $doc  = $data{doc};
         $doc = "no doc for $name" if !defined $doc;
@@ -137,7 +137,11 @@ sub parse_options {
         if ( !defined $cmdline_params{$name} || $options_config{prefer_commandline}) {
             my $val = $opt->$name();
             if ( defined $val ) {
-                $cmdline_params{$name} = $val;
+                if ($data{json}) {
+                    $cmdline_params{$name} = decode_json($val);
+                } else {
+                    $cmdline_params{$name} = $val;
+                }
             }
         }
         push @missing_required, $name

-- 
libmoox-options-perl Debian packaging



More information about the Pkg-perl-cvs-commits mailing list