]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/Command.pm
add Debbugs::Command tool
[debbugs.git] / Debbugs / Command.pm
1 # This module is part of debbugs, and is released under the terms of
2 # the GPL version 3, or any later version (at your option). See the
3 # file README and COPYING for more information.
4 # Copyright 2017 by Don Armstrong <don@donarmstrong.com>.
5
6 package Debbugs::Command;
7
8 =head1 NAME
9
10 Debbugs::Command -- Handle multiple subcommand-style commands
11
12 =head1 SYNOPSIS
13
14  use Debbugs::Command;
15
16 =head1 DESCRIPTION
17
18
19 =head1 BUGS
20
21 None known.
22
23 =cut
24
25 use warnings;
26 use strict;
27 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
28 use base qw(Exporter);
29
30 BEGIN{
31      $VERSION = '0.1';
32      $DEBUG = 0 unless defined $DEBUG;
33
34      @EXPORT = ();
35      %EXPORT_TAGS = (commands    => [qw(handle_main_arguments),
36                                      qw(handle_subcommand_arguments)
37                                     ],
38                     );
39      @EXPORT_OK = ();
40      Exporter::export_ok_tags(keys %EXPORT_TAGS);
41      $EXPORT_TAGS{all} = [@EXPORT_OK];
42
43 }
44
45 use Getopt::Long qw(:config no_ignore_case);
46
47 =head1 Command processing (:commands)
48
49 Functions which parse arguments for commands (exportable with
50 C<:commands>)
51
52 =over
53
54 =item handle_main_arguments(
55
56 =cut 
57
58 sub handle_main_arguments {
59     my ($options,@args) = @_;
60     Getopt::Long::Configure('pass_through');
61     GetOptions($options,@args);
62     Getopt::Long::Configure('default');
63     return $options;
64 }
65
66
67
68 sub handle_subcommand_arguments {
69     my ($argv,$args,$subopt) = @_;
70     $subopt //= {};
71     Getopt::Long::GetOptionsFromArray($argv,
72                                       $subopt,
73                                       keys %{$args},
74                                      );
75     my @usage_errors;
76     for my $arg  (keys %{$args}) {
77         next unless $args->{$arg};
78         my $r_arg = $arg; # real argument name
79         $r_arg =~ s/[=\|].+//g;
80         if (not defined $subopt->{$r_arg}) {
81             push @usage_errors, "You must give a $r_arg option";
82         }
83     }
84     pod2usage(join("\n",@usage_errors)) if @usage_errors;
85     return $subopt;
86 }
87
88
89 1;
90
91
92 __END__
93 # Local Variables:
94 # indent-tabs-mode: nil
95 # cperl-indent-level: 4
96 # End: