Difference between revisions of "Freeside:3:Documentation:Developer/FS/Misc/Getopt"
(Edit via perl MediaWiki framework (1.13)) |
(No difference)
|
Latest revision as of 05:46, 10 February 2015
NAME
FS::Misc::Getopt - Getopt::Std for Freeside command line/cron scripts
SYNOPSIS
- !/usr/bin/perl
use FS::Getopt; use FS::other_stuff; our %opt;
getopts('AB');
print "Option A: $opt{A} Option B: $opt{B} Start date: $opt{start} End date: $opt{end} Freeside user: $opt{user} Verbose mode: $DEBUG ";
DESCRIPTION
This module provides a wrapper around Getopt::Std::getopts() that automatically processes certain common command line options, and sets up a convenient environment for writing a script.
Options will go into %main::opt, as if you had called getopts(..., \%opt). All options recognized by the wrapper use (and will always use) lowercase letters as flags, so it's safe for a script to define its options as capital letters.
Options recognized by the wrapper do not need to be included in the string argument to getopts().
The following command line options are recognized:
- -v: Verbose mode. Sets $main::DEBUG.; -s: Start date. If provided, FS::Getopt will parse it as a date and set $opt{start} to the resulting Unix timestamp value. If parsing fails, displays an error and exits.; -e: End date. As for -s; sets $opt{end}.
Calling getopts() also performs some additional setup:
- Exports a function named &main::debug, which performs a warn() if $DEBUG has a true value, and if not, does nothing. This should be used to output informational messages. (warn() is for warnings.); Captures the first command line argument after any switches and sets $opt{user} to that value. If a value isn't provided, prints an error and exits.; Loads FS::UID and calls adminsuidsetup() to connect to the database.