App-RegexpCommonUtils

 view release on metacpan or  search on metacpan

script/get-regexp-common-pattern  view on Meta::CPAN

#!perl

our $DATE = '2021-05-25'; # DATE
our $VERSION = '0.003'; # VERSION

use 5.010001;
use strict 'subs', 'vars';
use warnings;
use Perinci::CmdLine::Any;

use Data::Dmp;
use Regexp::Common;

our %SPEC;

$SPEC{app} = {
    v => 1.1,
    summary => 'Get a Regexp::Common::* pattern',
    args => {
        name => {
            summary => "Name of pattern, dot-separated, e.g. num.real",
            schema => ['str*', match=>'\A\w+((\.)\w+)*\z'],
            req => 1,
            pos => 0,
        },
        options => {
            'x.name.is_plural' => 1,
            'x.name.singular' => 'option',
            summary => 'Supply options',
            schema => ['hash*', of=>'str*'],
        },
    },
    examples => [
        {
            argv => ['num.real'],
        },
        {
            argv => ['delimited', '--option', 'delim=/'],
        },
    ],
};
sub app {
    my %args = @_;

    my $opts = $args{options} // {};
    my $code = '$RE' .
        join("", map {"{$_}"} split(/\./, $args{name})) .
            join("", map { "{-$_ => ".dmp($opts->{$_})."}" } sort keys %$opts);

    #say $code;
    my $re = eval $code; die "Can't eval code: $code: $@" if $@;

    if (-t STDOUT && $args{-cmdline} &&
            ($args{-cmdline_r}{format} // 'text') =~ /text/) {
        require Data::Dump::Color;
        return [200, "OK", Data::Dump::Color::dump("$re") . "\n",
                {'cmdline.skip_format'=>1}];
    } else {
        return [200, "OK", "$re"];
    }
}

Perinci::CmdLine::Any->new(
    url => '/main/app',
    pass_cmdline_object => 1,
)->run;

# ABSTRACT: Get a Regexp::Common::* pattern
# PODNAME: get-regexp-common-pattern

__END__

=pod

=encoding UTF-8

=head1 NAME

get-regexp-common-pattern - Get a Regexp::Common::* pattern

=head1 VERSION

This document describes version 0.003 of get-regexp-common-pattern (from Perl distribution App-RegexpCommonUtils), released on 2021-05-25.

=head1 SYNOPSIS

Usage:

 % get-regexp-common-pattern [--config-path=path | -c]
     [--config-profile=profile | -P] [--format=name] [--json]
     [--(no)naked-res] [--no-config | -C] [--no-env] [--option key=s]
     [--options-json=s] [--page-result[=program]] [--view-result[=program]]
     <name>

Examples:

 % get-regexp-common-pattern num.real
 (?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))

 % get-regexp-common-pattern delimited --option 'delim=/'
 (?:(?|(?:\/)(?:[^\\\/]*(?:\\.[^\\\/]*)*)(?:\/)))

=head1 OPTIONS

C<*> marks required options.

=head2 Main options

=over



( run in 1.045 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )