1 package MooseX::Getopt::GLD;
2 # ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive
6 use Getopt::Long::Descriptive 0.081;
8 with 'MooseX::Getopt::Basic';
11 is => 'rw', isa => 'Getopt::Long::Descriptive::Usage',
12 traits => ['NoGetopt'],
15 # captures the options: --help --usage --?
17 is => 'ro', isa => 'Bool',
20 cmd_aliases => [ qw(usage ?) ],
21 documentation => 'Prints this usage information.',
24 around _getopt_spec => sub {
29 around _getopt_get_options => sub {
31 my ($class, $params, $opt_spec) = @_;
32 return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec);
36 my ( $class, %params ) = @_;
38 my ( @options, %name_to_init_arg );
40 my $constructor_params = $params{params};
42 foreach my $opt ( @{ $params{options} } ) {
45 $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
47 ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
49 # remove this 'feature' because it didn't work
50 # all the time, and so is better to not bother
51 # since Moose will handle the defaults just
54 #( exists $opt->{default} ? (default => $opt->{default}) : () ),
58 my $identifier = lc($opt->{name});
59 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
61 $name_to_init_arg{$identifier} = $opt->{init_arg};
64 return ( \@options, \%name_to_init_arg );
77 with 'MooseX::Getopt::GLD';
79 has 'out' => (is => 'rw', isa => 'Str', required => 1);
80 has 'in' => (is => 'rw', isa => 'Str', required => 1);
82 # ... rest of the class here
89 my $app = My::App->new_with_options();
90 # ... rest of the script here
92 ## on the command line
93 % perl my_app_script.pl -in file.input -out file.dump