109_help_flag.t 1.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/usr/bin/env perl

# The documentation claims:
#   If Getopt::Long::Descriptive is installed and any of the following command
#   line params are passed (--help, --usage, --?), the program will exit with
#   usage information...

# This is not actually true (as of 0.29), as:
# 1. the consuming class must set up a attributes named 'help', 'usage' and
#     '?' to contain these command line options, which is not clearly
#     documented as a requirement
# 2.  the code is checking whether an option was parsed into an attribute
#     *called* 'help', 'usage' or '?', not whether the option --help, --usage
#     or --? was passed on the command-line (the mapping could be different,
#     if cmd_flag or cmd_aliases is used),

# This inconsistency is the underlying cause of RT#52474, RT#57683, RT#47865.

use strict; use warnings;
use Test::More tests => 6;
use Test::Exception;

{
    package MyClass;
    use strict; use warnings;
gfx's avatar
gfx committed
26 27
    use Mouse;
    with 'MouseX::Getopt';
28 29 30 31
}

# before fix, prints this on stderr:
#Unknown option: ?
Tomas Doran's avatar
Tomas Doran committed
32
#usage: test1.t
33 34 35 36 37 38 39 40 41 42

# after fix, prints this on stderr:
#usage: test1.t [-?] [long options...]
#	-? --usage --help  Prints this usage information.

foreach my $args ( ['--help'], ['--usage'], ['--?'], ['-?'] )
{
    local @ARGV = @$args;

    throws_ok { MyClass->new_with_options() }
43
        qr/^usage: (?:[\d\w]+)\Q.t [-?] [long options...]\E.^\t\Q-? --\E(\[no-\])?usage --(\[no-\])?help\s+\QPrints this usage information.\E$/ms,
44 45 46 47 48 49 50 51
        'Help request detected; usage information properly printed';
}

# now call again, and ensure we got the usage info.
my $obj = MyClass->new_with_options();
ok($obj->meta->has_attribute('usage'), 'class has usage attribute');
isa_ok($obj->usage, 'Getopt::Long::Descriptive::Usage');