#!/usr/bin/perl
# Copyright (C) 2015-2016 Sergey Poznyakoff <gray@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

use List::Regexp;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use File::Basename;
use Pod::Usage;
use Pod::Man;

use constant EX_OK => 0;
use constant EX_USAGE => 1;

my $progname = basename($0);
my $progdescr = "creates a regexp matching all words in the command line";
my %opts = ( type => 'posix' );

GetOptions("h" => sub {
               pod2usage(-message => "$progname: $progdescr",
                         -exitstatus => EX_OK);
           },
           "help" => sub {
               pod2usage(-exitstatus => EX_OK, -verbose => 2);
           },
           "usage" => sub {
               pod2usage(-exitstatus => EX_OK, -verbose => 0);
           },
	   "pcre" => sub { $opts{type} = $_[0] },
	   "posix" => sub { $opts{type} = $_[0] },
	   "emacs" => sub { $opts{type} = $_[0] },
	   "exact" => sub { $opts{match} = $_[0] },
	   "word" => sub { $opts{match} = $_[0] },
	   "group" => \$opts{group},
	   "debug" => \$opts{debug}) or exit(EX_USAGE);

pod2usage(-exitstatus => EX_USAGE, -verbose => 0, -output  => \*STDERR)
    if $#ARGV == -1;

print regexp_opt(\%opts, @ARGV);
print "\n";
__END__
=head1 NAME

regexp-opt - create a regexp matching all words in the command line

=head1 SYNOPSIS

B<regexp-opt>
B<--debug>
B<--emacs>
B<--exact>
B<--pcre>
B<--posix>
B<--word>
I<STRING> [I<STRING>...]

=head1 DESCRIPTION

Creates a regular expression that will match any of the words listed in the
command line.  By default, POSIX extended regexp is created.
    
=head1 OPTIONS

=over 4

=item B<--debug>

Enable debugging output.
    
=item B<--emacs>

Create B<Emacs> regular expression.    
    
=item B<--exact>

Create a regexp that will match only words appearing at a line alone.    
    
=item B<--pcre>

Create Perl-compatible regular expression.    
    
=item B<--posix>

Create POSIX extended regular expression (default).    
    
=item B<--word>

Create regexp for matching whole words. 
    
=back    

=head1 AUTHOR

Sergey Poznyakoff <gray@gnu.org>
