5 FS::Getopt - Getopt::Std for Freeside command line/cron scripts
17 print "Option A: $opt{A}
19 Start date: $opt{start}
21 Freeside user: $opt{user}
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.
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
36 Options recognized by the wrapper do not need to be included in the string
37 argument to getopts().
39 The following command line options are recognized:
43 =item -v: Verbose mode. Sets $main::DEBUG.
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.
49 =item -e: End date. As for -s; sets $opt{end}.
53 Calling getopts() also performs some additional setup:
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.)
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
65 =item Loads L<FS::UID> and calls adminsuidsetup() to connect to the database.
74 use FS::UID qw(adminsuidsetup);
75 use FS::Misc::DateTime qw(parse_datetime day_end);
77 our @EXPORT = qw( getopts debug );
80 my $optstring = shift;
82 $optstring .= 's:e:v';
84 Getopt::Std::getopts($optstring, \%opt);
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";
91 # now we have config access
93 $opt{start} = parse_datetime($opt{s})
94 or die "Unable to parse start date '$opt{s}'.\n";
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});
102 $main::DEBUG ||= $opt{v};
109 warn(@_, "\n") if $main::DEBUG;