#!perl

use strict;
use warnings;

use vars '$name', '$gender';
use ScriptX (
    'Getopt::Long' => {
        spec => ['name=s' => \$name, 'gender=s' => \$gender],
    },
    'Run',
);

sub run {
    my ($self, $stash) = @_;

    unless ($name) {
        print "Please specify --name!\n";
        exit 1;
    }
    unless ($gender) {
        print "Please specify --gender!\n";
        exit 1;
    }
    unless ($gender =~ /\A[mf]\z/i) {
        print "Invalid value in --gender, please specify m|f!\n";
        exit 1;
    }

    print "Hello, ", ($gender =~ /m/i ? "Mr." : "Mrs."), " $name!\n";
}

ScriptX->run;

# ABSTRACT: Parse command-line options using Getopt::Long (demos modifying plugin)
# PODNAME: scriptx-eg-getopt-long

__END__

=pod

=encoding UTF-8

=head1 NAME

scriptx-eg-getopt-long - Parse command-line options using Getopt::Long (demos modifying plugin)

=head1 VERSION

This document describes version 0.000002 of scriptx-eg-getopt-long (from Perl distribution ScriptX), released on 2020-10-01.

=head1 SYNOPSIS

Example 1:

 % script-eg-getopt-long
 Please specify --name!

Example 2:

 % script-eg-getopt-long --name Budi
 Please specify --gender!

Example 3:

 % script-eg-getopt-long --name Budi --gender x
 Invalid value in --gender, please specify m|f!

Example 4:

 % script-eg-getopt-long --name Budi --gender m
 Hello, Mr. Budi!

Example 5 (execution is aborted because of unknown option):

 % script-eg-getopt-long --name Budi --gender m --foo
 Unknown option: foo

Example 6 (execution continues despite unknown option):

 % SCRIPTX_IMPORT_JSON='["ModifyPlugin",{"plugin":"Getopt::Long", "add_or_modify_args":{"abort_on_faiure":0}}]' scriptx-eg-getopt-long  --name Budi --gender m --foo
 Unknown option: foo
 Hello, Mr. Budi!

=head1 DESCRIPTION

This script demonstrates the use of L<ScriptX::Getopt::Long> which, admittedly,
does not do much. It's okay if you want to load and use L<Getopt::Long>
directly.

By default, the L</abort_on_failure> is set to 1 which means when Getopt::Long's
GetOptions returns failure the execution is aborted, as shown in example 5. If
you want to set C<abort_on_faiure> to 0, you can modify the script and set:

 use ScriptX (
     'Getopt::Long' => {
         spec => ['name=s' => \$name, 'gender=s' => \$gender],
         abort_on_faiure => 0,
     },
     ...
 );

or use the L<ScriptX::ModifyPlugin> plugin from the command-line:

 % SCRIPTX_IMPORT_JSON='["ModifyPlugin",{"plugin":"Getopt::Long", "add_or_modify_args":{"abort_on_faiure":0}}]' scriptx-eg-getopt-long ...

as shown in example 6.

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/ScriptX>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-ScriptX>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=ScriptX>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 SEE ALSO

L<scriptx-eg-getopt-specless>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2020 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
