summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-06-24 15:15:56 -0400
committerJesse Luehrs <doy@tozt.net>2013-06-24 15:15:56 -0400
commit6f052e89933cc543f3b54bf183c0f0a55bf01f6f (patch)
tree2b0e876c77f40760e991e9ae2c68a6cd1ef57461
parentaed9192b71c5c4c63bd4da670a3bfebe58b66fcb (diff)
downloadreply-6f052e89933cc543f3b54bf183c0f0a55bf01f6f.tar.gz
reply-6f052e89933cc543f3b54bf183c0f0a55bf01f6f.zip
move script running stuff into Reply::App
-rw-r--r--bin/reply60
-rw-r--r--lib/Reply/App.pm118
2 files changed, 120 insertions, 58 deletions
diff --git a/bin/reply b/bin/reply
index 5572193..ae50f13 100644
--- a/bin/reply
+++ b/bin/reply
@@ -4,10 +4,7 @@ use warnings;
# PODNAME: reply
# ABSTRACT: read, eval, print, loop, yay!
-use Getopt::Long;
-
-use Reply;
-use Reply::Config;
+use Reply::App;
=head1 SYNOPSIS
@@ -24,57 +21,4 @@ this program.
=cut
-my $cfgfile = '.replyrc';
-GetOptions(
- 'cfg:s' => \$cfgfile,
- 'version' => sub { version() },
- 'help' => sub { usage(0) },
-) or usage(1);
-
-my $cfg = Reply::Config->new(file => $cfgfile);
-
-my %args = (config => $cfg);
-my $file = $cfg->file;
-if (!-e $file) {
- print("$file not found. Generating a default...\n");
- if (open my $fh, '>', $file) {
- my $contents = do {
- local $/;
- <DATA>
- };
- $contents =~ s/use 5.XXX/use $]/;
- print $fh $contents;
- close $fh;
- }
- else {
- warn "Couldn't write to $file";
- %args = ();
- }
-}
-
-Reply->new(%args)->run;
-
-sub usage {
- print " reply [--version] [--help] [--cfg file]\n";
- exit($_[0]);
-}
-
-sub version {
- print "Reply version $Reply::VERSION\n";
- exit(0);
-}
-
-__DATA__
-script_line1 = use strict
-script_line2 = use warnings
-script_line3 = use 5.XXX
-
-[Interrupt]
-[FancyPrompt]
-[DataDumper]
-[Colors]
-[ReadLine]
-[Hints]
-[Packages]
-[LexicalPersistence]
-[ResultCache]
+exit(Reply::App->new->run(@ARGV));
diff --git a/lib/Reply/App.pm b/lib/Reply/App.pm
new file mode 100644
index 0000000..beddf84
--- /dev/null
+++ b/lib/Reply/App.pm
@@ -0,0 +1,118 @@
+package Reply::App;
+use strict;
+use warnings;
+# ABSTRACT: command line app runner for Reply
+
+use Getopt::Long 'GetOptionsFromArray';
+
+use Reply;
+use Reply::Config;
+
+=head1 SYNOPSIS
+
+ use Reply::App;
+ exit(Reply::App->new->run(@ARGV));
+
+=head1 DESCRIPTION
+
+This module encapsulates the various bits of functionality related to running
+L<Reply> as a command line application.
+
+=cut
+
+=method new
+
+Returns a new Reply::App instance. Takes no arguments.
+
+=cut
+
+sub new { bless {}, shift }
+
+=method run(@argv)
+
+Parses the argument list given (typically from @ARGV), along with the user's configuration file, and attempts to start a Reply shell. A default configuration file will be generated for the user if none exists.
+
+=cut
+
+sub run {
+ my $self = shift;
+ my @argv = @_;
+
+ my $cfgfile = '.replyrc';
+ my $exitcode;
+ my $parsed = GetOptionsFromArray(
+ \@argv,
+ 'cfg:s' => \$cfgfile,
+ 'version' => sub { $exitcode = 0; version() },
+ 'help' => sub { $exitcode = 0; usage() },
+ );
+
+ if (!$parsed) {
+ usage(1);
+ $exitcode = 1;
+ }
+
+ return $exitcode if defined $exitcode;
+
+ my $cfg = Reply::Config->new(file => $cfgfile);
+
+ my %args = (config => $cfg);
+ my $file = $cfg->file;
+ if (!-e $file) {
+ print("$file not found. Generating a default...\n");
+ if (open my $fh, '>', $file) {
+ my $contents = do {
+ local $/;
+ <DATA>
+ };
+ $contents =~ s/use 5.XXX/use $]/;
+ print $fh $contents;
+ close $fh;
+ }
+ else {
+ warn "Couldn't write to $file";
+ %args = ();
+ }
+ }
+
+ Reply->new(%args)->run;
+
+ return 0;
+}
+
+=method usage
+
+Prints usage information to the screen.
+
+=cut
+
+sub usage {
+ print " reply [--version] [--help] [--cfg file]\n";
+}
+
+=method version
+
+Prints version information to the screen.
+
+=cut
+
+sub version {
+ print "Reply version $Reply::VERSION\n";
+}
+
+1;
+
+__DATA__
+script_line1 = use strict
+script_line2 = use warnings
+script_line3 = use 5.XXX
+
+[Interrupt]
+[FancyPrompt]
+[DataDumper]
+[Colors]
+[ReadLine]
+[Hints]
+[Packages]
+[LexicalPersistence]
+[ResultCache]