From: Karen Etheridge Date: Fri, 2 Jul 2010 02:12:42 +0000 (-0700) Subject: use insertion order to sort attributes when formulating arguments for Getopt::Long X-Git-Tag: 0.30~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=73038480bcf3ed44f61511cba4961c5c3613ddad;p=gitmo%2FMooseX-Getopt.git use insertion order to sort attributes when formulating arguments for Getopt::Long --- diff --git a/ChangeLog b/ChangeLog index 1bf3778..fec1139 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ Revision history for Perl extension MooseX-Getopt * MooseX::Getopt::Basic + - uses attribute insertion order when determining the order in which + options are documented in the usage (e.g. in $obj->usage->text). (Karen + Etheridge) + + * MooseX::Getopt::Basic - store the usage object to the usage attr (RT#58715) - properly checks whether the *option* --help, --usage, or --? were used, rather than the attribute 'help', 'usage' or '?' were set diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index 9a6c79a..44d683c 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -158,6 +158,7 @@ sub _traditional_spec { sub _compute_getopt_attrs { my $class = shift; + sort { $a->insertion_order <=> $b->insertion_order } grep { $_->does("MooseX::Getopt::Meta::Attribute::Trait") or diff --git a/t/110_sort_usage_by_attr_order.t b/t/110_sort_usage_by_attr_order.t new file mode 100644 index 0000000..1e41e79 --- /dev/null +++ b/t/110_sort_usage_by_attr_order.t @@ -0,0 +1,39 @@ + +# The usage information prints the 'documentation' value for all Getopt +# attributes, except the order is not deterministic (rather, it uses the order +# in which the attributes are stored in the metaclass 'attributes' hash). +# Let's sort them by insertion order, which should lead to nicer output: +# If MooseX::Getopt is applied early, the help options will be on top +# the help options will always be on top (assuming this role is applied +# early), followed by options added by parent classes and roles, and then +# options added by this class. + +use strict; use warnings; +use Test::More tests => 1; +use Test::Exception; + +{ + package MyClass; + use strict; use warnings; + use Moose; + with 'MooseX::Getopt'; + + has $_ => ( + is => 'ro', isa => 'Str', + traits => ['Getopt'], + documentation => 'Documentation for "' . $_ . '"', + ) foreach qw(foo bar baz); +} + +my $obj = MyClass->new_with_options(); + +my $expected = <usage->text, $expected, 'Usage text has nicely sorted options'); +