]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/Command.pm
ef9f31cc6b16e25e87e3209f2f202f2015e9e89a
[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 use Pod::Usage qw(pod2usage);
47
48 =head1 Command processing (:commands)
49
50 Functions which parse arguments for commands (exportable with
51 C<:commands>)
52
53 =over
54
55 =item handle_main_arguments(
56
57 =cut 
58
59 sub handle_main_arguments {
60     my ($options,@args) = @_;
61     Getopt::Long::Configure('pass_through');
62     GetOptions($options,@args);
63     Getopt::Long::Configure('default');
64     return $options;
65 }
66
67
68
69 sub handle_subcommand_arguments {
70     my ($argv,$args,$subopt) = @_;
71     $subopt //= {};
72     Getopt::Long::GetOptionsFromArray($argv,
73                                       $subopt,
74                                       keys %{$args},
75                                      );
76     my @usage_errors;
77     for my $arg  (keys %{$args}) {
78         next unless $args->{$arg};
79         my $r_arg = $arg; # real argument name
80         $r_arg =~ s/[=\|].+//g;
81         if (not defined $subopt->{$r_arg}) {
82             push @usage_errors, "You must give a $r_arg option";
83         }
84     }
85     pod2usage(join("\n",@usage_errors)) if @usage_errors;
86     return $subopt;
87 }
88
89
90 1;
91
92
93 __END__
94 # Local Variables:
95 # indent-tabs-mode: nil
96 # cperl-indent-level: 4
97 # End: