From c11ab0abb790e519d63c47c37962c31dc032014a Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 13 Jul 2018 13:32:45 +0100 Subject: I'm losing the plot, here --- lib/Getopt/Dakkar/Option.pm | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/Getopt/Dakkar/Option.pm (limited to 'lib/Getopt/Dakkar/Option.pm') diff --git a/lib/Getopt/Dakkar/Option.pm b/lib/Getopt/Dakkar/Option.pm new file mode 100644 index 0000000..0dfd882 --- /dev/null +++ b/lib/Getopt/Dakkar/Option.pm @@ -0,0 +1,38 @@ +package Getopt::Dakkar::Option; +use Getopt::Dakkar::Style qw(class); +with 'Getopt::Dakkar::Role::Piece'; +# VERSION +# ABSTRACT: an option + +has can_bundle => ( is => 'ro', isa => Bool, default => 0 ); +has parameters => ( is => 'ro', isa => ArrayRef, default => sub { [] } ); + +sub match($self,$arg) { + for my $candidate ($self->matching_strings->@*) { + if (length($candidate)==1) { + return 1 if $arg eq "-$candidate"; + return 1 if $self->can_bundle && $arg =~ /^-\Q$candidate/; + } + else { + return 1 if $arg eq "--$candidate"; + } + } + return 0; +} + +sub parse($self,$args,$stash) { + my $my_option = shift $args->@*; + + # first, get arguments if needed + for my $parameter ($self->parameters->@*) { + my $argument = $parameter->parse($args,$stash); + # aaarg, + } + + # then, remove the option and put the bundle back, if needed + if ($my_option =~ /^-[^-]+/) { # we're in a bundle + $my_option =~ s{^-.}{-}; + unshift $args->@*, $my_option; + } + return $the_option_argument; +} -- cgit v1.2.3