cvsuser 03/11/16 13:21:22
Added: App-Options CHANGES MANIFEST MANIFEST.SKIP Makefile.PL
README TODO
App-Options/bin prefix
App-Options/t app.conf main.t test1 test1.conf test2 test3
test4 test5
Log:
initial version. replaces App-BEGIN.
Revision Changes Path
1.1 p5ee/App-Options/CHANGES
Index: CHANGES
===================================================================
#########################################
# CHANGE LOG
#########################################
VERSION 0.60
o Initial release
1.1 p5ee/App-Options/MANIFEST
Index: MANIFEST
===================================================================
CHANGES
MANIFEST
Makefile.PL
README
TODO
bin/prefix
lib/App/Options.pm
t/main.t
t/app.conf
1.1 p5ee/App-Options/MANIFEST.SKIP
Index: MANIFEST.SKIP
===================================================================
~$
^MANIFEST\.
^Makefile$
^blib/
^pm_to_blib$
\.old$
\.bak$
\.cvsignore$
\.tar\.gz$
htdocs/.exists
CVS/
1.1 p5ee/App-Options/Makefile.PL
Index: Makefile.PL
===================================================================
######################################################################
## File: $Id: Makefile.PL,v 1.1 2003/11/16 21:21:22 spadkins Exp $
######################################################################
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
my @programs = (
"bin/prefix",
);
%opts = (
'NAME' => 'App-Options',
'DISTNAME' => 'App-Options',
'VERSION' => '0.60',
'EXE_FILES' => [ @programs ],
'dist' => {'COMPRESS'=>'gzip -9f', 'SUFFIX' => 'gz',
'ZIP'=>'/usr/bin/zip','ZIPFLAGS'=>'-rl'},
);
WriteMakefile(%opts);
1.1 p5ee/App-Options/README
Index: README
===================================================================
######################################################################
## File: $Id: README,v 1.1 2003/11/16 21:21:22 spadkins Exp $
######################################################################
This is the App-Options distribution.
It parses command line options, incorporates values from environment
variables and configuration files, validates option values, and
returns them in a simple hash structure. It would be used instead
of modules like Getopt::Long, but of course it does a lot more.
In particular, it may be run within a BEGIN block in order to
modify the @INC variable with additional directories so that
subsequent "use" and "require" statements are affected.
FEATURES
o Easy to use.
o Parse long (--whatever) and short (-w) command line options
o Combine options with Environment Variables
o Combine options with global and local config files
o Multiple config files can be used via "import" option
o Modify @INC variable with the "perlinc" option
o Validate "required" options provided
o Validate option values against types (integer, int, float,
number, date, datetime, string)
o Provide automatic "-?" and "--help" messages
It is useful on its own for any perl programs which require flexible
and easy option processing, and it has no dependencies other than
on "Cwd", a module included with Perl itself. However, it was
specifically designed in order to support the P5EE/App-Context
variant of the Perl 5 Enterprise Environment.
For more information on the P5EE project, see the web pages at
http://www.officevision.com/pub/p5ee
and/or join the mailing list at
[EMAIL PROTECTED]
The mailing list is described at
http://p5ee.perl.org/mailinglist/
http://lists.perl.org/showlist.cgi?name=p5ee
HOW DO I INSTALL IT?
To install this module, cd to the directory that contains this README
file and type the following:
perl Makefile.PL
make
make test
make install
1.1 p5ee/App-Options/TODO
Index: TODO
===================================================================
######################################################################
## File: $Id: TODO,v 1.1 2003/11/16 21:21:22 spadkins Exp $
######################################################################
o quoting, var = " hello world "
o here documents, var = <<EOF
o check list of configurable environment vars instead of "APP_${uc_var}"
o try use lib "dir"; instead of unshift(@INC,"dir")
o reconsider "perlinc", perhaps use "perl5lib" instead
o consider checking the PERL5LIB variable under -T
1.1 p5ee/App-Options/bin/prefix
Index: prefix
===================================================================
#!/bin/bash
#*********************************************************************
#** prefix
#*********************************************************************
#** @(#)$Id: prefix,v 1.1 2003/11/16 21:21:22 spadkins Exp $
#*********************************************************************
export OLDPREFIX=$PREFIX
export PREFIX=${PREFIX:-/usr/local}
if [[ -f "$HOME/.prefixes" ]] # check if multiple environments exist
then
export PREFIXES_FILE=$HOME/.prefixes
else
export PREFIXES_FILE=/etc/prefixes
fi
print_usage ()
{
echo "The prefix script allows you to manage multiple installations of"
echo "a set of software installed on the same machine. It helps in this"
echo "by taking care of the reconfiguration of environment variables"
echo "appropriate for each installation. It is assumed that the"
echo "software for each installation all under a single directory"
echo "called PREFIX. This is especially important on development"
echo "machines where multiple versions of software are installed side-"
echo "by-side (i.e. development, test, production)."
echo
echo "There are three usages of the prefix script:"
echo
echo " (1) The interactive usage should be placed as the LAST LINE"
echo " of a user's ".profile". The user must be running the"
echo " Korn shell (ksh) or the Bourne Again shell (bash)."
echo " The user is prompted to enter one of the known echo PREFIX locations."
echo " During configuration, the \$PREFIX/.prefixrc file is sourced"
echo " in order to accomplish environment-specific configurations."
echo " (2) The non-interactive user configuration does not consult"
echo " $HOME/.prefixes or /etc/prefixes or prompt the user, but merely"
echo " configures the environment in accordance with the cmd line argument."
echo " (3) The batch command usage is mainly for running commands from"
echo " cron or running commands in another environment without changing"
echo " to that environment."
echo
echo "Usage (1): . prefix (sets up environment)"
echo " (2): . prefix <prefix> (non-interactive setup)"
echo " (3): prefix <prefix> <cmd> <args> (Runs cmd configured for PREFIX)"
echo
}
if [[ "$1" = "-?" ]]
then
print_usage
else
export PREFIX_CMD=""
if [[ "$1" != "" ]]
then
export NEW_PREFIX="$1"
export PREFIX_ASK="no"
shift
if [[ $# -gt 0 ]]
then
export PREFIX_CMD="$*"
set -- # clear cmd line args
fi
fi
if [[ "$PREFIX_ASK" != "no" ]]
then
if [[ -f "$PREFIXES_FILE" ]] # check if multiple environments exist
then
export NUM_PREFIXES=$(wc -l < "$PREFIXES_FILE")
if [[ "$NEW_PREFIX" = "" ]]
then
export NEW_PREFIX=$(head -1 "$PREFIXES_FILE")
fi
if [[ "$PREFIX_ASK" != "no" && "$NUM_PREFIXES" -gt 1 ]]
then
echo "========================================================"
echo "SET UP SOFTWARE PREFIX (ROOT DIRECTORY FOR THE SOFTWARE)"
echo "========================================================"
export PS3="Select a directory: "
select NEW_PREFIX in $(sed 's/#.*//' "$PREFIXES_FILE")
do
break
done
if [[ "$NEW_PREFIX" = "" ]]
then
export NEW_PREFIX="$REPLY"
fi
fi
fi
fi
if [[ "$NEW_PREFIX" = "" ]]
then
echo
echo "ERROR: Please specify a PREFIX or create a ~/.prefixes file"
echo " to enable interactive usage."
echo
print_usage
elif [[ ! -d $NEW_PREFIX/. ]]
then
echo
echo "ERROR: Directory [$NEW_PREFIX] does not exist."
echo
print_usage
else
export PREFIX=$NEW_PREFIX
if [[ -t 1 && "$PREFIX_CMD" = "" ]]
then
echo "Configuring Software Prefix [$NEW_PREFIX]."
fi
######################################################
# Remove all PATH references before adding them back
######################################################
export SED_CLEAN_PATH=""
export SED_CLEAN_LIBPATH=""
export SED_CLEAN_MANPATH=""
for AUXDIR in $(echo "$OLDPREFIX:$PRE_PREFIX:$POST_PREFIX" | sed -e 's/^/:/'
-e 's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
do
SED_CLEAN_PATH="$SED_CLEAN_PATH -e s!:$AUXDIR/bin!!g"
SED_CLEAN_LIBPATH="$SED_CLEAN_LIBPATH -e s!:$AUXDIR/lib!!g"
SED_CLEAN_MANPATH="$SED_CLEAN_MANPATH -e s!:$AUXDIR/man!!g"
done
# Remove old references from the PATH
export PATH=`echo $PATH | \
sed -e "s/^:*/:/" \
-e "s/:*$/:/" \
-e "s/:::*/:/g" \
$SED_CLEAN_PATH \
-e "s/::*$//" \
-e "s/^::*//"`
# Remove old references from the LD_LIBRARY_PATH
OLD_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH=""
export LD_LIBRARY_PATH=`echo $OLD_LD_LIBRARY_PATH | \
sed -e "s/^:*/:/" \
-e "s/:*$/:/" \
-e "s/:::*/:/g" \
$SED_CLEAN_LIBPATH \
-e "s/::*$//" \
-e "s/^::*//"`
unset OLD_LD_LIBRARY_PATH
# Remove old references from the LIBPATH
OLD_LIBPATH="$LIBPATH"
export LIBPATH=""
export LIBPATH=`echo $OLD_LIBPATH | \
sed -e "s/^:*/:/" \
-e "s/:*$/:/" \
-e "s/:::*/:/g" \
$SED_CLEAN_LIBPATH \
-e "s/::*$//" \
-e "s/^::*//"`
unset OLD_LIBPATH
# Remove old references from the MANPATH
export MANPATH=`echo $MANPATH | \
sed -e "s/^:*/:/" \
-e "s/:*$/:/" \
-e "s/:::*/:/g" \
$SED_CLEAN_MANPATH \
-e "s/::*$//" \
-e "s/^::*//"`
######################################################
# source the environment's .rmsrc file if it exists
######################################################
for AUXDIR in $(echo $POST_PREFIX | sed -e 's/^/:/' -e
's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
do
if [[ -f $AUXDIR/.prefixrc ]]
then
. $AUXDIR/.prefixrc
fi
done
if [[ -f $PREFIX/.prefixrc ]]
then
. $PREFIX/.prefixrc
fi
for AUXDIR in $(echo $PRE_PREFIX | sed -e 's/^/:/' -e
's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
do
if [[ -f $AUXDIR/.prefixrc ]]
then
. $AUXDIR/.prefixrc
fi
done
######################################################
# source the user's .prefixrc file if it exists
######################################################
if [[ -f ~/.prefixrc ]]
then
. ~/.prefixrc
fi
##########################################################
# Set appropriate defaults for common variables
##########################################################
export AUXPATH=""
export AUXLIBPATH=""
export AUXMANPATH=""
export SP_INCLUDEPATH=""
for AUXDIR in $(echo $PRE_PREFIX | sed -e 's/^/:/' -e
's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
do
AUXPATH="$AUXPATH$AUXDIR/bin:"
AUXLIBPATH="$AUXLIBPATH$AUXDIR/lib:"
AUXMANPATH="$AUXMANPATH$AUXDIR/man:"
SP_INCLUDEPATH="$SP_INCLUDEPATH -I$AUXDIR/include"
done
SP_INCLUDEPATH="$SP_INCLUDEPATH -I$PREFIX/include"
export AUXPATHPOST=""
export AUXLIBPATHPOST=""
export AUXMANPATHPOST=""
for AUXDIR in $(echo $POST_PREFIX | sed -e 's/^/:/' -e
's!:\([^/~]\)!:'$PREFIX'/\1!' -e 's/:/ /g')
do
AUXPATHPOST="$AUXPATHPOST:$AUXDIR/bin"
AUXLIBPATHPOST="$AUXLIBPATHPOST:$AUXDIR/lib"
AUXMANPATHPOST="$AUXMANPATHPOST:$AUXDIR/man"
SP_INCLUDEPATH="$SP_INCLUDEPATH -I$AUXDIR/include"
done
# Add new references into the PATH
export PATH=$AUXPATH$PREFIX/bin$AUXPATHPOST:$PATH
# Add new references into the LD_LIBRARY_PATH
if [[ "$LD_LIBRARY_PATH" = "" ]]
then
export LD_LIBRARY_PATH=$AUXLIBPATH$PREFIX/lib$AUXLIBPATHPOST
else
export
LD_LIBRARY_PATH=$AUXLIBPATH$PREFIX/lib$AUXLIBPATHPOST:$LD_LIBRARY_PATH
fi
# Add new references into the LIBPATH
if [[ "$LIBPATH" = "" ]]
then
export LIBPATH=$AUXLIBPATH$PREFIX/lib$AUXLIBPATHPOST
else
export LIBPATH=$AUXLIBPATH$PREFIX/lib$AUXLIBPATHPOST:$LIBPATH
fi
# Add new references into the MANPATH
if [[ "$MANPATH" = "" ]]
then
export MANPATH=$AUXMANPATH$PREFIX/man$AUXMANPATHPOST
else
export MANPATH=$AUXMANPATH$PREFIX/man$AUXMANPATHPOST:$MANPATH
fi
fi
fi
set -- # clear cmd line args
unset PREFIXES_FILE
unset PREFIX_CMD
unset PREFIX_ASK
unset NUM_PREFIXES
unset NEW_PREFIX
unset SED_CLEAN_PATH
unset SED_CLEAN_LIBPATH
unset SED_CLEAN_MANPATH
unset AUXPATH
unset AUXLIBPATH
unset AUXMANPATH
unset AUXDIR
unset AUXPATHPOST
unset AUXLIBPATHPOST
unset AUXMANPATHPOST
1.1 p5ee/App-Options/t/app.conf
Index: app.conf
===================================================================
var = value
prefix = /usr/local
[junk]
var1 = some other junk
var5 = it better not be this
[ALL]
[main] var1 = pattern match
[main] var2 = old pattern match
[main] var3 = value3
dir = /usr/local
htdocs_dir = ${dir}/htdocs
[:dir=] dir = /usr/bad
template_dir = ${dir}/template
[:dir2=] dir2 = /usr/local
cgibin_dir = ${dir2}/cgi-bin
[:greeting=] greeting = Hello
[bozo]
var4 = value4
[]
var5 = value5
[bozo]
var6 = value6
[ALL]
var8 = value8
[ma.*]
var7 = value7
1.1 p5ee/App-Options/t/main.t
Index: main.t
===================================================================
#!/usr/local/bin/perl -w
use Test::More qw(no_plan);
use lib "lib";
use lib "../lib";
use_ok("App::Options");
my ($dir);
$dir = ".";
$dir = "t" if (! -f "app.conf");
delete $ENV{PREFIX};
delete $ENV{DOCUMENT_ROOT};
App::Options->init();
#print "CONF:\n ", join("\n ",%App::conf), "\n";
ok(%App::conf, "put something in %App::conf");
is($App::conf{prefix}, "/usr/local", "prefix = /usr/local");
is($App::conf{app}, "main", "app = main");
is($App::conf{var}, "value", "var = value");
is($App::conf{var1}, "pattern match", "pattern match");
is($App::conf{var2}, "old pattern match", "old pattern match");
is($App::conf{htdocs_dir}, "/usr/local/htdocs", "variable substitution");
is($App::conf{cgibin_dir}, "/usr/local/cgi-bin", "variable substitution (default
used)");
is($App::conf{template_dir}, "/usr/local/template", "variable substitution (default
supplied but not used)");
is($App::conf{greeting}, "Hello", "variable substitution (var name used since var
not defined)");
is($App::conf{var3}, "value3", "inline pattern match");
is($App::conf{var4}, undef, "section excluded");
is($App::conf{var5}, "value5", "section exclusion ended");
is($App::conf{var6}, undef, "section excluded again");
is($App::conf{var7}, "value7", "section included");
is($App::conf{var8}, "value8", "ALL works");
%App::conf = (
config_file => "$dir/app.conf",
prefix => "/usr/local",
perlinc => "/usr/mycompany/2.1.7/lib/perl5"
);
App::Options->init();
#print "CONF:\n ", join("\n ",%App::conf), "\n";
ok(%App::conf, "put something in %App::conf");
is($App::conf{prefix}, "/usr/local", "prefix = /usr/local");
is($App::conf{app}, "main", "app = main");
is($App::conf{var}, "value", "var = value");
is($App::conf{var1}, "pattern match", "pattern match");
is($App::conf{var2}, "old pattern match", "old pattern match");
is($INC[0], "/usr/mycompany/2.1.7/lib/perl5", "[EMAIL PROTECTED] affected by
perlinc");
App::Options->init(\%App::otherconf);
#print "CONF:\n ", join("\n ",%App::otherconf), "\n";
ok(%App::otherconf, "put something in %App::otherconf");
is($App::otherconf{prefix}, "/usr/local", "prefix = /usr/local");
is($App::otherconf{app}, "main", "app = main");
is($App::otherconf{var}, "value", "var = value");
is($App::otherconf{var1}, "pattern match", "pattern match");
is($App::otherconf{var2}, "old pattern match", "old pattern match");
App::Options->init(conf => \%App::conf3);
#print "CONF:\n ", join("\n ",%App::conf3), "\n";
ok(%App::conf3, "put something in %App::conf3");
is($App::conf3{prefix}, "/usr/local", "prefix = /usr/local");
is($App::conf3{app}, "main", "app = main");
is($App::conf3{var}, "value", "var = value");
is($App::conf3{var1}, "pattern match", "pattern match");
is($App::conf3{var2}, "old pattern match", "old pattern match");
exit 0;
1.1 p5ee/App-Options/t/test1
Index: test1
===================================================================
#!/usr/local/bin/perl
BEGIN {
use App::BEGIN;
App::BEGIN->init(
params => [ "invoice_id", "cust_nm" ],
);
}
1.1 p5ee/App-Options/t/test1.conf
Index: test1.conf
===================================================================
perlinc = /usr/rubicon/devel/src/p5ee/App-BEGIN/lib
flush_imports = 1
1.1 p5ee/App-Options/t/test2
Index: test2
===================================================================
#!/usr/local/bin/perl
BEGIN {
use App::BEGIN;
App::BEGIN->init(
param => {
invoice_id => "4",
cust_nm => "Joe Smith",
},
);
}
1.1 p5ee/App-Options/t/test3
Index: test3
===================================================================
#!/usr/local/bin/perl
BEGIN {
use App::BEGIN;
App::BEGIN->init(
param => {
invoice_id => "4;type=int",
cust_nm => "Joe Smith",
purchase_dttm => "2003-10-31 23:59:59; required; type=datetime",
delivery_dt => "required=1; type=date",
price => "type=float",
},
);
}
1.1 p5ee/App-Options/t/test4
Index: test4
===================================================================
#!/usr/local/bin/perl
BEGIN {
use App::BEGIN;
App::BEGIN->init(
param => {
invoice_id => {default => 4, type => "int"},
cust_nm => {default => "Joe Smith"},
purchase_dttm => {default => "2003-10-31 23:59:59", required => 1, type
=> "datetime"},
delivery_dt => {required => 1, type => "date"},
price => {type => "float"},
},
);
}
1.1 p5ee/App-Options/t/test5
Index: test5
===================================================================
#!/usr/local/bin/perl
BEGIN {
use App::BEGIN;
App::BEGIN->init(
params => [ "invoice_id", "cust_nm", "price" ],
param => {
invoice_id => {default => 4, type => "int"},
cust_nm => {default => "Joe Smith"},
purchase_dttm => {default => "2003-10-31 23:59:59", type => "datetime"},
delivery_dt => {type => "date"},
price => {type => "float"},
},
);
}