From f9c1b23b7773763a0e3016d857180491253ebaf3 Mon Sep 17 00:00:00 2001 From: Rayhan Alcena Date: Wed, 9 Sep 2026 15:33:21 -0400 Subject: [PATCH] Add --help and --version to plerdall and plerdwatcher Closes #47, #48 --- bin/plerdall | 28 ++++++++++++- bin/plerdwatcher | 28 ++++++++++++- t/cli_options.t | 104 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 t/cli_options.t diff --git a/bin/plerdall b/bin/plerdall index d5bac8b..8908b81 100755 --- a/bin/plerdall +++ b/bin/plerdall @@ -15,6 +15,7 @@ use Plerd::Util; use Plerd::Init; use Cwd; use Path::Class::Dir; +use Pod::Usage qw(pod2usage); use Text::Wrap qw(wrap); @@ -26,13 +27,26 @@ my $process_webmentions = 0; my $rebuild_webmentions = 0; my $config_file; my $init_path; +my $help; +my $show_version; GetOptions( 'send-webmentions' => \$webmention_enabled, 'process-webmentions' => \$process_webmentions, 'rebuild-webmentions' => \$rebuild_webmentions, 'config=s' => \$config_file, 'init:s' => \$init_path, -); + 'help' => \$help, + 'version' => \$show_version, +) or pod2usage( -exitval => 2, -verbose => 0 ); + +if ( $help ) { + pod2usage( -exitval => 0, -verbose => 1 ); +} + +if ( $show_version ) { + print "plerdall is part of Plerd $Plerd::VERSION\n"; + exit 0; +} if ($process_webmentions or $rebuild_webmentions) { die "Plerd ended its experimental Webmention receipt support in version " @@ -115,6 +129,18 @@ available online at L. =head1 OPTIONS +=head2 help + + bin/plerdwatcher --help + +Print this documentation and exit. + +=head2 version + + bin/plerdwatcher --version + +Print the running version of Plerd and exit. + =head2 config bin/plerdall --config=/path/to/plerd.conf diff --git a/bin/plerdwatcher b/bin/plerdwatcher index 37a80f6..ea44328 100755 --- a/bin/plerdwatcher +++ b/bin/plerdwatcher @@ -14,6 +14,7 @@ use App::Daemon qw( daemonize ); use Try::Tiny; use Log::Log4perl qw( :easy ); use Getopt::Long; +use Pod::Usage qw(pod2usage); use Plerd; use Plerd::Util; @@ -21,11 +22,24 @@ use Plerd::Util; my $webmention_enabled = 0; my $webmention_port = 0; my $config_file; +my $help; +my $show_version; GetOptions( 'config=s' => \$config_file, 'send-webmentions' => \$webmention_enabled, 'receive-webmentions=i' => \$webmention_port, -); + 'version' => \$show_version, + 'help' => \$help, +) or pod2usage( -exitval => 2, -verbose => 0 ); + +if ( $help ) { + pod2usage( -exitval => 0, -verbose => 1 ); +} + +if ( $show_version ) { + print "plerdwatcher is part of Plerd $Plerd::VERSION\n"; + exit 0; +} if ($webmention_port) { die "Plerd ended its experimental Webmention receipt support in version " @@ -163,6 +177,18 @@ available online at L. =head1 OPTIONS +=head2 help + + bin/plerdwatcher --help + +Print this documentation and exit. + +=head2 version + + bin/plerdwatcher --version + +Print the running version of Plerd and exit. + =head2 config bin/plerdall --config=/path/to/plerd.conf diff --git a/t/cli_options.t b/t/cli_options.t new file mode 100644 index 0000000..356dc9c --- /dev/null +++ b/t/cli_options.t @@ -0,0 +1,104 @@ +use warnings; +use strict; +use Test::More; +use Capture::Tiny ':all'; + +use FindBin; +use lib "$FindBin::Bin/../lib"; + +use Plerd; + +my $plerdall = "$FindBin::Bin/../bin/plerdall"; + +# --version prints the Plerd version and exits zero, without requiring +# a config file to be present. +{ + my ($stdout, $stderr, $exit) = capture { + system( $^X, '-I', "$FindBin::Bin/../lib/", $plerdall, '--version' ); + }; + + is( $exit, 0, '--version exits zero.' ); + like( + $stdout, + qr/\Q$Plerd::VERSION\E/, + '--version output mentions the Plerd version number.', + ); + is( $stderr, '', '--version produces no error output.' ); +} + +# --help prints usage information and exits zero, without requiring +# a config file to be present. +{ + my ($stdout, $stderr, $exit) = capture { + system( $^X, '-I', "$FindBin::Bin/../lib/", $plerdall, '--help' ); + }; + + is( $exit, 0, '--help exits zero.' ); + like( + $stdout, + qr/plerdall/i, + '--help output mentions the program name.', + ); + like( + $stdout, + qr/--config/, + '--help output documents the --config option.', + ); + is( $stderr, '', '--help produces no error output.' ); +} + +# An unrecognized option should still fail informatively (regression +# check for the old "Unknown option: help"-with-a-config-error mess). +{ + my ($stdout, $stderr, $exit) = capture { + system( + $^X, '-I', "$FindBin::Bin/../lib/", + $plerdall, '--this-is-not-a-real-option', + ); + }; + + isnt( $exit, 0, 'An unrecognized option exits non-zero.' ); +} + +# plerdwatcher --version prints the Plerd version and exits zero, +# without requiring a config file or launching the daemon. +{ + my $plerdwatcher = "$FindBin::Bin/../bin/plerdwatcher"; + + my ($stdout, $stderr, $exit) = capture { + system( $^X, '-I', "$FindBin::Bin/../lib/", $plerdwatcher, '--version' ); + }; + + is( $exit, 0, 'plerdwatcher --version exits zero.' ); + like( + $stdout, + qr/\Q$Plerd::VERSION\E/, + 'plerdwatcher --version output mentions the Plerd version number.', + ); + is( $stderr, '', 'plerdwatcher --version produces no error output.' ); +} + +# plerdwatcher --help prints usage information and exits zero, without +# requiring a config file or launching the daemon. +{ + my $plerdwatcher = "$FindBin::Bin/../bin/plerdwatcher"; + + my ($stdout, $stderr, $exit) = capture { + system( $^X, '-I', "$FindBin::Bin/../lib/", $plerdwatcher, '--help' ); + }; + + is( $exit, 0, 'plerdwatcher --help exits zero.' ); + like( + $stdout, + qr/plerdwatcher/i, + 'plerdwatcher --help output mentions the program name.', + ); + like( + $stdout, + qr/--config/, + 'plerdwatcher --help output documents the --config option.', + ); + is( $stderr, '', 'plerdwatcher --help produces no error output.' ); +} + +done_testing; \ No newline at end of file