Subject: | Feature suggestion: pass the Usage object back to the class. |
Inside _parse_argv, there is a Getopt::Long::Descriptive::Usage that
gets generated which is used as an argument for _getopt_full_usage()
when printing the usage information (when --help or --usage is supplied,
or there is an error parsing the arguments). It would be really handy
if this object were saved and sent back to the class so it can use it in
the case of future errors, e.g. if argument validation fails.
I would suggest adding another attribute in the composing class:
# in Basic.pm:
has _usage => (
is => 'rw', isa => 'Getopt::Long::Descriptive::Usage',
metaclass => 'NoGetopt',
);
sub new_with_options
{
...
$class->new(
_usage => $processed{usage},
# ... other args (ARGV, extra_argv etc)
);
}
So in the composing class, one could do:
sub do_work
{
my $this = shift;
if (not validate($this->foo))
{
# exit with usage info
print "Your foo argument does not validate\n";
print $this->usage->text, "\n";
exit;
}
}
"usage" might be a better attribute name than _usage (as ARGV and
extra_argv don't use underscores), but it conflicts with the usage attr
I add in the solution I provided to
https://rt.cpan.org/Public/Bug/Display.html?id=57683:
has [qw(help usage)] => (
is => 'ro', isa => 'Bool',
documentation => 'Prints this usage documentation.',
traits => ['Getopt'],
);
Although I suppose that could be rewritten thusly:
has "_$_" => (
is => 'ro', isa => 'Bool',
documentation => 'Prints this usage documentation.',
traits => ['Getopt'],
cmd_flag => $_,
) for (qw(help usage));
If I receive general noises of approval for this suggestion, I might put
submit a patch. Please make appropriate noise.