C<new_with_options> will throw an exception.
If L<Getopt::Long::Descriptive> is installed and any of the following
-command line params are passed, the program will exit with usage
+command line params are passed, the program will exit with usage
information. You can add descriptions for each option by including a
B<documentation> option for each attribute to document.
opt_string => $opt_string,
required => $attr->is_required && !$attr->has_default && !$attr->has_builder && !exists $config_from_file->{$attr->name},
# NOTE:
- # this "feature" was breaking because
- # Getopt::Long::Descriptive would return
- # the default value as if it was a command
+ # this "feature" was breaking because
+ # Getopt::Long::Descriptive would return
+ # the default value as if it was a command
# line flag, which would then override the
# one passed into a constructor.
# See 100_gld_default_bug.t for an example
{
( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
# NOTE:
- # remove this 'feature' because it didn't work
+ # remove this 'feature' because it didn't work
# all the time, and so is better to not bother
- # since Moose will handle the defaults just
+ # since Moose will handle the defaults just
# fine anyway.
# - SL
#( exists $opt->{default} ? (default => $opt->{default}) : () ),
{
package App;
use Moose;
-
+
with 'MooseX::Getopt';
has 'data' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Str',
default => 'file.dat',
);
has 'cow' => (
- metaclass => 'Getopt',
+ metaclass => 'Getopt',
is => 'ro',
isa => 'Str',
default => 'moo',
);
has 'horse' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Str',
default => 'bray',
has 'verbose' => (
is => 'ro',
- isa => 'Bool',
+ isa => 'Bool',
);
-
+
has 'libs' => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [] },
- );
-
+ );
+
has 'details' => (
is => 'ro',
isa => 'HashRef',
);
has '_private_stuff_cmdline' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Int',
default => 832,
cmd_flag => 'p',
);
-
+
}
foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
isa_ok($attr, 'Moose::Meta::Attribute');
isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
can_ok($attr, 'cmd_flag');
- can_ok($attr, 'cmd_aliases');
+ can_ok($attr, 'cmd_aliases');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 50, '... length is 50 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 50, '... length is 50 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'foo.txt', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'foo.txt', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs,
- ['libs/', 'includes/lib'],
- '... libs is [libs/, includes/lib] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs,
+ ['libs/', 'includes/lib'],
+ '... libs is [libs/, includes/lib] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details,
- { os => 'mac', name => 'foo' },
- '... details is { os => mac, name => foo } as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details,
+ { os => 'mac', name => 'foo' },
+ '... details is { os => mac, name => foo } as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... file is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... file is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
# Test cmd_alias without cmd_flag
package App;
use Moose;
use Moose::Util::TypeConstraints;
-
+
use Scalar::Util 'looks_like_number';
-
+
with 'MooseX::Getopt';
subtype 'ArrayOfInts'
=> as 'ArrayRef'
=> where { scalar (grep { looks_like_number($_) } @$_) };
-
+
MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
'ArrayOfInts' => '=i@'
);
-
+
has 'nums' => (
is => 'ro',
isa => 'ArrayOfInts',
default => sub { [0] }
- );
-
+ );
+
}
{
my $app = App->new_with_options;
isa_ok($app, 'App');
-
- is_deeply($app->nums, [0], '... nums is [0] as expected');
+
+ is_deeply($app->nums, [0], '... nums is [0] as expected');
}
{
my $app = App->new_with_options;
isa_ok($app, 'App');
-
- is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
+
+ is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
}
# Make sure it really used our =i@, instead of falling back
package App;
use Moose;
use Moose::Util::TypeConstraints;
-
+
use Scalar::Util 'looks_like_number';
-
+
with 'MooseX::Getopt';
subtype 'ArrayOfInts'
=> as 'ArrayRef'
=> where { scalar (grep { looks_like_number($_) } @$_) };
-
+
has 'nums' => (
is => 'ro',
isa => 'ArrayOfInts',
default => sub { [0] }
- );
-
+ );
+
}
{
my $app = App->new_with_options;
isa_ok($app, 'App');
-
- is_deeply($app->nums, [0], '... nums is [0] as expected');
+
+ is_deeply($app->nums, [0], '... nums is [0] as expected');
}
{
my $app = App->new_with_options;
isa_ok($app, 'App');
-
- is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
+
+ is_deeply($app->nums, [3, 5], '... nums is [3, 5] as expected');
}
{
package App;
use Moose;
-
+
with 'MooseX::Getopt';
has 'data' => (
- traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
+ traits => [ 'MooseX::Getopt::Meta::Attribute::Trait' ],
is => 'ro',
isa => 'Str',
default => 'file.dat',
);
has 'cow' => (
- traits => [ 'Getopt' ],
+ traits => [ 'Getopt' ],
is => 'ro',
isa => 'Str',
default => 'moo',
);
has 'horse' => (
- traits => [ 'Getopt' ],
+ traits => [ 'Getopt' ],
is => 'ro',
isa => 'Str',
default => 'bray',
has 'verbose' => (
is => 'ro',
- isa => 'Bool',
+ isa => 'Bool',
);
-
+
has 'libs' => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [] },
- );
-
+ );
+
has 'details' => (
is => 'ro',
isa => 'HashRef',
);
has '_private_stuff_cmdline' => (
- traits => [ 'Getopt' ],
+ traits => [ 'Getopt' ],
is => 'ro',
isa => 'Int',
default => 832,
cmd_flag => 'p',
);
-
+
}
foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
my $attr = App->meta->get_attribute($attr_name);
isa_ok($attr, 'Moose::Meta::Attribute');
does_ok($attr, 'MooseX::Getopt::Meta::Attribute::Trait');
-
+
can_ok($attr, 'cmd_flag');
- can_ok($attr, 'cmd_aliases');
+ can_ok($attr, 'cmd_aliases');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 50, '... length is 50 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 50, '... length is 50 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'foo.txt', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'foo.txt', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs,
- ['libs/', 'includes/lib'],
- '... libs is [libs/, includes/lib] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs,
+ ['libs/', 'includes/lib'],
+ '... libs is [libs/, includes/lib] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details,
- { os => 'mac', name => 'foo' },
- '... details is { os => mac, name => foo } as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details,
+ { os => 'mac', name => 'foo' },
+ '... details is { os => mac, name => foo } as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... file is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... file is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
# Test cmd_alias without cmd_flag
use Test::More;
use Test::Exception;
-BEGIN {
+BEGIN {
eval 'use Getopt::Long::Descriptive;';
plan skip_all => "Getopt::Long::Descriptive required for this test" if $@;
plan tests => 5;
{
package Testing::Foo;
use Moose;
-
+
with 'MooseX::Getopt';
-
+
has 'bar' => (
is => 'ro',
- isa => 'Int',
+ isa => 'Int',
required => 1,
);
-
+
has 'baz' => (
is => 'ro',
- isa => 'Int',
- required => 1,
- );
+ isa => 'Int',
+ required => 1,
+ );
}
@ARGV = qw(--bar 10);
BEGIN {
eval 'use Getopt::Long::Descriptive;';
plan skip_all => "Getopt::Long::Descriptive required for this test" if $@;
- plan tests => 5;
+ plan tests => 5;
use_ok('MooseX::Getopt');
}
{
package Engine::Foo;
use Moose;
-
+
with 'MooseX::Getopt';
-
+
has 'nproc' => (
metaclass => 'Getopt',
is => 'ro',
{
package App;
use Moose;
-
+
with 'MooseX::Getopt::Basic';
has 'data' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Str',
default => 'file.dat',
);
has 'cow' => (
- metaclass => 'Getopt',
+ metaclass => 'Getopt',
is => 'ro',
isa => 'Str',
default => 'moo',
);
has 'horse' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Str',
default => 'bray',
has 'verbose' => (
is => 'ro',
- isa => 'Bool',
+ isa => 'Bool',
);
-
+
has 'libs' => (
is => 'ro',
isa => 'ArrayRef',
default => sub { [] },
- );
-
+ );
+
has 'details' => (
is => 'ro',
isa => 'HashRef',
);
has '_private_stuff_cmdline' => (
- metaclass => 'MooseX::Getopt::Meta::Attribute',
+ metaclass => 'MooseX::Getopt::Meta::Attribute',
is => 'ro',
isa => 'Int',
default => 832,
cmd_flag => 'p',
);
-
+
}
foreach my $attr_name (qw(data cow horse _private_stuff_cmdline)) {
isa_ok($attr, 'Moose::Meta::Attribute');
isa_ok($attr, 'MooseX::Getopt::Meta::Attribute');
can_ok($attr, 'cmd_flag');
- can_ok($attr, 'cmd_aliases');
+ can_ok($attr, 'cmd_aliases');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 50, '... length is 50 as expected');
- is($app->data, 'file.dat', '... data is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 50, '... length is 50 as expected');
+ is($app->data, 'file.dat', '... data is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'foo.txt', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'foo.txt', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok($app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs,
- ['libs/', 'includes/lib'],
- '... libs is [libs/, includes/lib] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs,
+ ['libs/', 'includes/lib'],
+ '... libs is [libs/, includes/lib] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned on as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... data is foo.txt as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details,
- { os => 'mac', name => 'foo' },
- '... details is { os => mac, name => foo } as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... data is foo.txt as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details,
+ { os => 'mac', name => 'foo' },
+ '... details is { os => mac, name => foo } as expected');
}
{
isa_ok($app, 'App');
ok(!$app->verbose, '... verbosity is turned off as expected');
- is($app->length, 24, '... length is 24 as expected');
- is($app->data, 'file.dat', '... file is file.dat as expected');
- is_deeply($app->libs, [], '... libs is [] as expected');
- is_deeply($app->details, {}, '... details is {} as expected');
+ is($app->length, 24, '... length is 24 as expected');
+ is($app->data, 'file.dat', '... file is file.dat as expected');
+ is_deeply($app->libs, [], '... libs is [] as expected');
+ is_deeply($app->details, {}, '... details is {} as expected');
}
# Test cmd_alias without cmd_flag
$meta->add_attribute('BigD', traits => ['Getopt'], isa => 'Bool',
cmd_aliases => ['D'], is => 'ro');
$meta->add_attribute('SmallD', traits => ['Getopt'], isa => 'Bool',
- cmd_aliases => ['d'], is => 'ro');
+ cmd_aliases => ['d'], is => 'ro');
$role->meta->apply($meta);
{