From: Karen Etheridge Date: Sat, 14 Apr 2012 19:04:01 +0000 (-0700) Subject: print --usage, --help to stdout not stderr X-Git-Tag: v0.42~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Getopt.git;a=commitdiff_plain;h=c885acae4797dc3ce50e91445ceb96c971e87671;hp=3aaa34a1cf97648e9704fc9c73abc4ebd8f90b39 print --usage, --help to stdout not stderr --- diff --git a/ChangeLog b/ChangeLog index bb824bf..0135fbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ Revision history for Perl extension MooseX-Getopt +{{$NEXT}} + * Now bails with exit status 0, rather than dying, when printing + --usage/--help information. (Karen Etheridge) + 0.40 Fri 13 Apr 2012 * Fix tests when MooseX::ConfigFromFile is installed, with Getopt::Long::Descriptive >= 0.091. RT#76287 diff --git a/dist.ini b/dist.ini index 2929945..905919b 100644 --- a/dist.ini +++ b/dist.ini @@ -32,3 +32,4 @@ Test::Fatal = 0.003 Test::Warn = 0.21 Test::More = 0.88 Test::Requires = 0.05 +Test::Trap = 0 diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index 87b54db..6b4d40e 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -146,7 +146,8 @@ sub _getopt_spec_exception { sub _getopt_full_usage { my ($self, $usage) = @_; - $usage->die; + print $usage->text; + exit 0; } sub _usage_format { diff --git a/t/104_override_usage.t b/t/104_override_usage.t index 887844b..8e7a1ff 100644 --- a/t/104_override_usage.t +++ b/t/104_override_usage.t @@ -1,7 +1,7 @@ use strict; use warnings; use Test::More 0.88; -use Test::Fatal; +use Test::Trap; { package MyScript; @@ -29,13 +29,15 @@ use Test::Fatal; { local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception; local @ARGV = ('--help'); - like exception { MyScript->new_with_options }, qr/A foo/; + trap { MyScript->new_with_options }; + like($trap->stdout, qr/A foo/); is $MyScript::usage, 1; } { local $MyScript::usage; local @MyScript::warnings; local @MyScript::exception; local @ARGV = ('-q'); # Does not exist - like exception { MyScript->new_with_options }, qr/A foo/; + trap { MyScript->new_with_options }; + like($trap->die, qr/A foo/); is_deeply \@MyScript::warnings, [ 'Unknown option: q ' diff --git a/t/109_help_flag.t b/t/109_help_flag.t index 580884e..eb75961 100644 --- a/t/109_help_flag.t +++ b/t/109_help_flag.t @@ -16,9 +16,11 @@ # This inconsistency is the underlying cause of RT#52474, RT#57683, RT#47865. +# Update: since 0.41, usage info is printed to stdout, not stderr. + use strict; use warnings; -use Test::More tests => 6; -use Test::Fatal; +use Test::More tests => 18; +use Test::Trap; { package MyClass; @@ -31,7 +33,7 @@ use Test::Fatal; #Unknown option: ? #usage: test1.t -# after fix, prints this on stderr: +# after fix, prints this on stdout (formerly stderr): #usage: test1.t [-?] [long options...] # -? --usage --help Prints this usage information. @@ -43,10 +45,17 @@ my $usage_text = $obj->usage->text; foreach my $args ( ['--help'], ['--usage'], ['--?'], ['-?'] ) { local @ARGV = @$args; + note "Setting \@ARGV to @$args"; + + trap { MyClass->new_with_options() }; - is exception { MyClass->new_with_options() }, + is($trap->leaveby, 'exit', 'bailed with an exit code'); + is($trap->exit, 0, '...of 0'); + is( + $trap->stdout, $usage_text, - 'Help request detected; usage information properly printed'; + 'Usage information printed to STDOUT', + ); + is($trap->stderr, '', 'there was no STDERR output'); } -