cvsuser 04/12/02 08:00:15
Modified: App-Options CHANGES
App-Options/lib/App Options.pm
App-Options/t app.conf main.t
Log:
add host/hostname options
Revision Changes Path
1.7 +8 -1 p5ee/App-Options/CHANGES
Index: CHANGES
===================================================================
RCS file: /cvs/public/p5ee/App-Options/CHANGES,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- CHANGES 2 Sep 2004 21:09:00 -0000 1.6
+++ CHANGES 2 Dec 2004 16:00:12 -0000 1.7
@@ -2,6 +2,13 @@
# CHANGE LOG
#############################################################################
+VERSION 0.92
+ x cleaned up some warnings which appeared under -w (concatenation of undef)
+ x undefined values appear as "[undef]" rather than "[]" in print_usage
+ x options appear as "--var=<value>" rather than "--var=<var>" in print_usage
+ x option attribute value_description is used instead of "<value>" if given
+ x add system-supplied hostname and host options
+
VERSION 0.91
x show_all is off by default whenever "option" or "options" used
x remove app_path_info
@@ -65,5 +72,5 @@
"path" variable to be set from the "PATH" environment variable.)
VERSION 0.61
- o Initial release
+ x Initial release
1.9 +50 -8 p5ee/App-Options/lib/App/Options.pm
Index: Options.pm
===================================================================
RCS file: /cvs/public/p5ee/App-Options/lib/App/Options.pm,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Options.pm 2 Sep 2004 21:06:57 -0000 1.8
+++ Options.pm 2 Dec 2004 16:00:13 -0000 1.9
@@ -1,6 +1,6 @@
#############################################################################
-## $Id: Options.pm,v 1.8 2004/09/02 21:06:57 spadkins Exp $
+## $Id: Options.pm,v 1.9 2004/12/02 16:00:13 spadkins Exp $
#############################################################################
package App::Options;
@@ -9,6 +9,8 @@
use strict;
use Carp;
+use Sys::Hostname;
+use Cwd 'abs_path';
$VERSION = "0.91";
@@ -290,6 +292,13 @@
An imported file goes on the head of the queue of files to be
processed.
+ hostname - the hostname as returned by the hostname() function
+ provided by Sys::Hostname (may or may not include domain
+ qualifiers as a fully qualified domain name).
+
+ host - same as hostname, but with any trailing domain name removed.
+ (everything after the first ".")
+
flush_imports - flush all pending imported option files.
=cut
@@ -354,6 +363,14 @@
#################################################################
#################################################################
+ # 0. Set system-supplied values (i.e. hostname/host)
+ #################################################################
+ my $host = hostname;
+ $values->{hostname} = $host;
+ $host =~ s/\..*//; # get rid of extra domain name qualifiers
+ $values->{host} = $host;
+
+ #################################################################
# 1. Read the command-line options
# (anything starting with one or two dashes is an option var
# i.e. --debugmode=record -debugmode=replay
@@ -402,7 +419,6 @@
# Using "abs_path" gets rid of all symbolic links and gives the real path
# to the directory in which the script runs.
if (!$prefix) {
- use Cwd 'abs_path';
my $abs_prog_dir = abs_path($prog_dir);
if ($abs_prog_dir =~ s!/bin$!!) {
$prefix = $abs_prog_dir;
@@ -658,7 +674,12 @@
if ($values->{debug_options}) {
print STDERR "%App::options (or other) =\n";
foreach $var (sort keys %$values) {
- print STDERR " $var = [$values->{$var}]\n";
+ if (defined $values->{$var}) {
+ print STDERR " $var = [$values->{$var}]\n";
+ }
+ else {
+ print STDERR " $var = [undef]\n";
+ }
}
print STDERR "[EMAIL PROTECTED] =\n";
foreach $var (@INC) {
@@ -788,7 +809,7 @@
push(@vars, (sort keys %$values));
}
my ($var, $value, $type, $desc, $option);
- my ($var_str, $value_str, $type_str, $desc_str);
+ my ($var_str, $value_str, $type_str, $desc_str, $val_desc);
$option = $init_args->{option} || {};
foreach $var (@vars) {
next if ($option_seen{$var});
@@ -797,7 +818,8 @@
$value = $values->{$var};
$type = $option->{$var}{type} || "";
$desc = $option->{$var}{description} || "";
- $var_str = ($type eq "boolean") ? $var : ($type ? "$var=<$type>" :
"$var=<$var>");
+ $val_desc = $option->{$var}{value_description} || "";
+ $var_str = ($type eq "boolean") ? $var : ($val_desc ?
"$var=<$val_desc>" : "$var=<value>");
$value_str = (defined $value) ? $value : "undef";
$type_str = ($type) ? " ($type)" : "";
$desc_str = ($desc) ? " $desc" : "";
@@ -1321,9 +1343,10 @@
If, however, your projects were not in the habit of using the
PREFIX environment variable and the program is not installed in
-$PREFIX/bin, he would have to put the above lines in the same
-directory as "listcust" in either the "app.conf" file or the
-"listcust.conf" file.
+$PREFIX/bin, he would have to put the above lines
+in either the "app.conf" file or the "listcust.conf" file
+in either the global /etc/app directory or in the same
+directory as "listcust".
A user (without privileges to the "$PREFIX/etc/app" directory
or the directory in which "listcust" lives) would have to put
@@ -1510,6 +1533,25 @@
no_env_vars => 1,
);
+=head2 A Deployment Scenario
+
+Sometimes a software system gets deployed across many machines.
+You may wish to have a single option file set different values
+when it is deployed to different machines.
+
+For this purpose, the automatic "host" and "hostname" values
+are useful. Suppose you have four servers named "foo1", "foo2",
+"foo3", and "foo4". You may wish the software to use different
+databases on each server. So app.conf might look like this.
+
+ [host=foo1] dbname = devel
+ [host=foo2]
+ dbname = test
+ [host=foo3]
+ dbname = prod
+ [ALL]
+ dbname = prod
+
Hopefully, that's enough to get you going.
I welcome all feedback, bug reports, and feature requests.
1.4 +1 -0 p5ee/App-Options/t/app.conf
Index: app.conf
===================================================================
RCS file: /cvs/public/p5ee/App-Options/t/app.conf,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- app.conf 19 Feb 2004 16:17:15 -0000 1.3
+++ app.conf 2 Dec 2004 16:00:14 -0000 1.4
@@ -33,3 +33,4 @@
var9 = value9
[/ol/]
var7 = value7
+[host=xyzzy3] hosttest = you really named a host xyzzy3?
1.5 +6 -0 p5ee/App-Options/t/main.t
Index: main.t
===================================================================
RCS file: /cvs/public/p5ee/App-Options/t/main.t,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- main.t 19 Feb 2004 16:17:15 -0000 1.4
+++ main.t 2 Dec 2004 16:00:14 -0000 1.5
@@ -81,5 +81,11 @@
is($App::options3{var1}, "pattern match", "pattern match");
is($App::options3{var2}, "old pattern match", "old pattern match");
+ok($App::options{hostname}, "hostname option set");
+ok($App::options{host}, "host option set");
+ok(length($App::options{host}) <= length($App::options{hostname}) &&
$App::options{host} !~ /\./,
+ "host option shorter than hostname option");
+ok(! defined $App::options{hosttest}, "host not named xyzzy3");
+
exit 0;