File: Command.pm

package info (click to toggle)
debbugs 2.6.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,800 kB
  • sloc: perl: 19,270; makefile: 81; sh: 75
file content (101 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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 <don@donarmstrong.com>.

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);
use Pod::Usage qw(pod2usage);

=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;
}

=back

=cut


1;


__END__
# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End: