App-PerinciUtils

 view release on metacpan or  search on metacpan

script/peri-run  view on Meta::CPAN

#!perl

our $DATE = '2019-01-20'; # DATE
our $VERSION = '0.112'; # VERSION
# NO_PERINCI_CMDLINE_SCRIPT

use 5.010;
use strict;
use warnings;
use Log::ger;

use Getopt::Long::Complete qw(GetOptionsWithCompletion);

my %opts = (
    library => [],
    subcommands => [],
    pass_cmdline_object => 0,
);
$Getopt::Long::Complete::opt_permute = 0;
$Getopt::Long::Complete::opt_pass_through = 1;
GetOptionsWithCompletion(
    sub {
        my %args = @_;
        #$log->tracef('args=%s', \%args);
        my $word = $args{word};
        if ($args{type} eq 'arg' && $args{argpos} == 0) {
            my $popts = $args{parsed_opts};

            my @scnames;
            if ($popts && ($popts->{'-s'} || $popts->{'--subcommand'})) {
                # if user has specified -s (or --subcommand), then we complete
                # subcommand name in first argument
                for (
                    @{ $popts->{'-s'} // [] },
                    @{ $popts->{'--subcommand'} // [] },
                ) {
                    /.+\s(.+)/ and push @scnames, $1;
                }
                require Complete::Util;
                return Complete::Util::complete_array_elem(
                    word=>$word, array=>\@scnames);
            } else {
                # else, we complete Riap URL in the first argument
                require Complete::Riap;
                return Complete::Riap::complete_riap_url(
                    word=>$word, type=>'function',
                );
            }
        } elsif ($args{type} eq 'optval') {
            my $opt = $args{opt};
            if ($opt eq '--library' || $opt eq '-I') {
                require Complete::Module;
                return Complete::Module::complete_module(word=>$word);
            }
        }
    },
    'library|I=s' => $opts{library},
    'help|h|?' => \$opts{help},
    'version' => sub {
        no warnings 'once';
        say "peri-run version " . ($main::VERSION // 'dev');
        exit 0;
    },
    'subcommand|s=s' => $opts{subcommands},
    'pass-cmdline-object|o' => \$opts{pass_cmdline_object},
);

my $me = $0; $me =~ s!.+/!!;

my $oscs = $opts{subcommands};
if ($opts{help} || !@ARGV && !@$oscs) {
    print <<USAGE;
$me - Run commands (from any Riap function) on the command-line

Usage:
  $me --help
  $me [common options] <command def> [subcommand] [action]

*Common options* include: `--library` (`-I`) to add directory to Perl search dir
(a la Perl's `-I`), can be specified multiple times. There is also
`--pass-cmdline-object` (`-o`) if you want to pass Perinci::CmdLine object to
the function.

*Command def* is either a single command definition or multiple subcommands
definition. To define a single command, you can specify a Riap function URL
(e.g. '/Foo/Bar/func'). For multiple subcommands, you can specify a Riap package
URL (e.g. '/Foo/Bar/') or a Perl module name (e.g. 'Foo::Bar'), in which case
all functions in the module/package will be listed and added as subcommands.
Alternatively, you can specify each subcommand separately using '--subcommand'
('-s'). For example, the command below specifies two subcommands called 'func1'
and 'altname':

  $me -s /Foo/Bar/func1 -s "/Foo/Bar/func2 altname"

*Subcommand* picks a subcommand name, required only when you specify multiple
subcommands.

*action* is either '--help' to get help message on the command or subcommand,
'--list' to list subcommands, '--version' to get version, or zero or more
command (function) arguments.

Examples:
  Show usage for a subcommand (function):
    $me /Foo/Bar/func1 --help
    $me /Foo/Bar/func1 -h

  Execute a command and display the result as YAML:
    $me http://example.org/api/Foo/Bar/func --format=yaml --arg 12

  Execute a subcommand:
    $me -s "/Foo/Bar/func1 sc1" -s "/Foo/Baz/func1 sc2" sc2 --arg 12

Notes:
* This is just a simple generic front-end for 'Perinci::CmdLine::Any'. For more
  options/customizations, use or subclass the module directly.

TODO:
* HTTP authentication parameters

USAGE
    exit 0;



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