JSON-Structure
view release on metacpan or search on metacpan
bin/pjstruct view on Meta::CPAN
#!/usr/bin/env perl
# ABSTRACT: JSON-Structure validator CLI for Perl
# PODNAME: pjstruct
use strict;
use warnings;
use 5.020;
use feature 'signatures';
no warnings 'experimental::signatures';
use Getopt::Long qw(:config gnu_getopt bundling);
use Pod::Usage;
use JSON::MaybeXS;
use File::Basename;
use JSON::Structure;
our $VERSION = '0.01';
# Exit codes
use constant {
EXIT_SUCCESS => 0,
EXIT_INVALID => 1,
EXIT_ERROR => 2,
};
# Main entry point
exit main(@ARGV);
sub main (@args) {
local @ARGV = @args;
# Global options
my %opts = (
format => 'text',
quiet => 0,
verbose => 0,
help => 0,
version => 0,
);
# Parse global options first
Getopt::Long::Configure('pass_through');
GetOptions(
'h|help' => \$opts{help},
'V|version' => \$opts{version},
) or return EXIT_ERROR;
if ($opts{version}) {
say "pjstruct version $VERSION (JSON::Structure $JSON::Structure::VERSION)";
return EXIT_SUCCESS;
}
# Get command
my $command = shift @ARGV // '';
if ($opts{help} || $command eq 'help' || $command eq '') {
return cmd_help($command eq 'help' ? shift @ARGV : undef);
}
# Dispatch to command
my %commands = (
'validate' => \&cmd_validate,
'v' => \&cmd_validate,
'check' => \&cmd_check,
'c' => \&cmd_check,
'version' => sub { say "pjstruct version $VERSION"; return EXIT_SUCCESS; },
);
if (my $handler = $commands{$command}) {
return $handler->(\%opts);
}
else {
warn "pjstruct: unknown command '$command'\n";
warn "Run 'pjstruct help' for usage.\n";
return EXIT_ERROR;
}
}
sub cmd_help ($topic = undef) {
if (!defined $topic) {
print_usage();
return EXIT_SUCCESS;
}
my %topics = (
'validate' => \&help_validate,
'v' => \&help_validate,
'check' => \&help_check,
'c' => \&help_check,
);
( run in 1.883 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )