From 1b514b3dfeba13f531edfe17fd05388bd0a25ac8 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Thu, 14 Dec 2017 14:27:14 -0800 Subject: [PATCH] add Debbugs::Command tool --- Debbugs/Command.pm | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Debbugs/Command.pm diff --git a/Debbugs/Command.pm b/Debbugs/Command.pm new file mode 100644 index 0000000..a180316 --- /dev/null +++ b/Debbugs/Command.pm @@ -0,0 +1,96 @@ +# This module is part of debbugs, and is released under the terms of +# the GPL version 3, or any later version (at your option). See the +# file README and COPYING for more information. +# Copyright 2017 by Don Armstrong . + +package Debbugs::Command; + +=head1 NAME + +Debbugs::Command -- Handle multiple subcommand-style commands + +=head1 SYNOPSIS + + use Debbugs::Command; + +=head1 DESCRIPTION + + +=head1 BUGS + +None known. + +=cut + +use warnings; +use strict; +use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT); +use base qw(Exporter); + +BEGIN{ + $VERSION = '0.1'; + $DEBUG = 0 unless defined $DEBUG; + + @EXPORT = (); + %EXPORT_TAGS = (commands => [qw(handle_main_arguments), + qw(handle_subcommand_arguments) + ], + ); + @EXPORT_OK = (); + Exporter::export_ok_tags(keys %EXPORT_TAGS); + $EXPORT_TAGS{all} = [@EXPORT_OK]; + +} + +use Getopt::Long qw(:config no_ignore_case); + +=head1 Command processing (:commands) + +Functions which parse arguments for commands (exportable with +C<:commands>) + +=over + +=item handle_main_arguments( + +=cut + +sub handle_main_arguments { + my ($options,@args) = @_; + Getopt::Long::Configure('pass_through'); + GetOptions($options,@args); + Getopt::Long::Configure('default'); + return $options; +} + + + +sub handle_subcommand_arguments { + my ($argv,$args,$subopt) = @_; + $subopt //= {}; + Getopt::Long::GetOptionsFromArray($argv, + $subopt, + keys %{$args}, + ); + my @usage_errors; + for my $arg (keys %{$args}) { + next unless $args->{$arg}; + my $r_arg = $arg; # real argument name + $r_arg =~ s/[=\|].+//g; + if (not defined $subopt->{$r_arg}) { + push @usage_errors, "You must give a $r_arg option"; + } + } + pod2usage(join("\n",@usage_errors)) if @usage_errors; + return $subopt; +} + + +1; + + +__END__ +# Local Variables: +# indent-tabs-mode: nil +# cperl-indent-level: 4 +# End: -- 2.39.2