Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / FS / FS / Misc / Getopt.pm
1 package FS::Misc::Getopt;
2
3 =head1 NAME
4
5 FS::Misc::Getopt - Getopt::Std for Freeside command line/cron scripts
6
7 =head1 SYNOPSIS
8
9 #!/usr/bin/perl
10
11 use FS::Getopt;
12 use FS::other_stuff;
13 our %opt;
14
15 getopts('AB');
16
17 print "Option A: $opt{A}
18 Option B: $opt{B}
19 Start date: $opt{start}
20 End date: $opt{end}
21 Freeside user: $opt{user}
22 Verbose mode: $DEBUG
23 ";
24
25 =head1 DESCRIPTION
26
27 This module provides a wrapper around Getopt::Std::getopts() that 
28 automatically processes certain common command line options, and sets
29 up a convenient environment for writing a script.
30
31 Options will go into %main::opt, as if you had called getopts(..., \%opt).
32 All options recognized by the wrapper use (and will always use) lowercase 
33 letters as flags, so it's safe for a script to define its options as
34 capital letters.
35
36 Options recognized by the wrapper do not need to be included in the string
37 argument to getopts().
38
39 The following command line options are recognized:
40
41 =over 4
42
43 =item -v: Verbose mode. Sets $main::DEBUG.
44
45 =item -s: Start date. If provided, FS::Getopt will parse it as a date 
46 and set $opt{start} to the resulting Unix timestamp value. If parsing fails, 
47 displays an error and exits.
48
49 =item -e: End date. As for -s; sets $opt{end}.
50
51 =back
52
53 Calling getopts() also performs some additional setup: 
54
55 =over 4
56
57 =item Exports a function named &main::debug, which performs a warn() if 
58 $DEBUG has a true value, and if not, does nothing. This should be used to
59 output informational messages. (warn() is for warnings.)
60
61 =item Captures the first command line argument after any switches and 
62 sets $opt{user} to that value. If a value isn't provided, prints an error
63 and exits.
64
65 =item Loads L<FS::UID> and calls adminsuidsetup() to connect to the database.
66
67 =back
68
69 =cut
70
71 use strict;
72 use base 'Exporter';
73 use Getopt::Std ();
74 use FS::UID qw(adminsuidsetup);
75 use FS::Misc::DateTime qw(parse_datetime day_end);
76
77 our @EXPORT = qw( getopts debug );
78
79 sub getopts {
80   my $optstring = shift;
81   my %opt;
82   $optstring .= 's:e:v';
83
84   Getopt::Std::getopts($optstring, \%opt);
85
86   $opt{user} = shift(@ARGV)
87     or die "Freeside username required.\n";
88   adminsuidsetup($opt{user})
89     or die "Failed to connect as user '$opt{user}'.\n";
90
91   # now we have config access
92   if ( $opt{s} ) {
93     $opt{start} = parse_datetime($opt{s})
94       or die "Unable to parse start date '$opt{s}'.\n";
95   }
96   if ( $opt{e} ) {
97     $opt{end} = parse_datetime($opt{e})
98       or die "Unable to parse start date '$opt{e}'.\n";
99     $opt{end} = day_end($opt{end});
100   }
101   if ( $opt{v} ) {
102     $main::DEBUG ||= $opt{v};
103   }
104
105   %main::opt = %opt;
106 }
107
108 sub debug {
109   warn(@_, "\n") if $main::DEBUG;
110 }
111
112 1;