summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiko Miyagawa <miyagawa@bulknews.net>2013-06-02 09:19:13 +0900
committerTatsuhiko Miyagawa <miyagawa@bulknews.net>2013-06-02 09:19:13 +0900
commit5f2a1525b559b665bc94aed920235470a400bf09 (patch)
tree1dd91ce692397072e3aa8ca6cffa115de3dd1af6
parentcd2120369f37859170ddc0be13648c70e082d24a (diff)
downloadcarton-5f2a1525b559b665bc94aed920235470a400bf09.tar.gz
use Moo in CLI
-rw-r--r--lib/Carton/CLI.pm39
1 files changed, 22 insertions, 17 deletions
diff --git a/lib/Carton/CLI.pm b/lib/Carton/CLI.pm
index fd0cb6f..5bbc096 100644
--- a/lib/Carton/CLI.pm
+++ b/lib/Carton/CLI.pm
@@ -11,46 +11,48 @@ use Carton::Lock;
use Carton::Util;
use Carton::Error;
use Try::Tiny;
+use Moo;
use constant { SUCCESS => 0, INFO => 1, WARN => 2, ERROR => 3 };
our $UseSystem = 0; # 1 for unit testing
-sub new {
- my $class = shift;
- bless {
- verbose => 0,
- }, $class;
-}
+has verbose => (is => 'rw');
+has carton => (is => 'lazy');
+has workdir => (is => 'lazy');
-sub carton {
- my $self = shift;
- $self->{carton} ||= Carton->new;
+sub _build_carton {
+ Carton->new;
}
sub work_file {
my($self, $file) = @_;
- return "$self->{work_dir}/$file";
+ return join "/", $self->workdir, $file;
+}
+
+sub _build_workdir {
+ my $self = shift;
+ $ENV{PERL_CARTON_HOME} || (Cwd::cwd() . "/.carton");
}
sub run {
my($self, @args) = @_;
- $self->{work_dir} = $ENV{PERL_CARTON_HOME} || (Cwd::cwd() . "/.carton");
- mkdir $self->{work_dir}, 0777 unless -e $self->{work_dir};
+ my $dir = $self->workdir;
+ mkdir $dir, 0777 unless -e $dir;
- local @ARGV = @args;
my @commands;
my $p = Getopt::Long::Parser->new(
config => [ "no_ignore_case", "pass_through" ],
);
- $p->getoptions(
+ $p->getoptionsfromarray(
+ \@args,
"h|help" => sub { unshift @commands, 'help' },
"v|version" => sub { unshift @commands, 'version' },
- "verbose!" => \$self->{verbose},
+ "verbose!" => sub { $self->verbose($_[1]) },
);
- push @commands, @ARGV;
+ push @commands, @args;
my $cmd = shift @commands || 'install';
my $call = $self->can("cmd_$cmd");
@@ -89,7 +91,10 @@ HELP
sub parse_options {
my($self, $args, @spec) = @_;
- Getopt::Long::GetOptionsFromArray($args, @spec);
+ my $p = Getopt::Long::Parser->new(
+ config => [ "no_auto_abbrev", "no_ignore_case" ],
+ );
+ $p->getoptionsfromarray($args, @spec);
}
sub parse_options_pass_through {