Permalink
Browse files

command line option improvement

the shell script now attempts to auto locate PHP, followed by running a PHP
version check before starting. getoptx has been added for simplicity in
handling long options, until it has been changed over to getopt native


git-svn-id: https://snaptest.googlecode.com/svn/trunk@49 85759191-e132-0410-920c-21c98535aa7c
  • Loading branch information...
Jakobo committed Jan 16, 2008
1 parent 7db4664 commit 21f361e04e32a7b35f6ed0001437aff78afe00a3
Showing with 313 additions and 46 deletions.
  1. +12 −6 LICENSE
  2. +221 −0 getoptx.sh
  3. +28 −29 snaptest.php
  4. +52 −11 snaptest.sh
View
18 LICENSE
@@ -1,7 +1,7 @@
-"The contents of this file and software package (the "Application") are subject
-to the Mozilla Public License Version 1.1 (the "License"); you may not use
-this file or the Application except in compliance with the License. You may
-obtain a copy of the License at
+"The contents of this file and software package (the "Application") are
+subject to the Mozilla Public License Version 1.1 (the "License"); you may not
+use this file or the Application except in compliance with the License. You
+may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
@@ -11,5 +11,11 @@ under the License.
The Original Code is Snaptest 0.0.1 [no formal release yet].
-The Initial Developer of the Original Code is Jakob Heuser <jakob@felocity.org>.
-Copyright (C) 2007. All Rights Reserved.
+The Initial Developer of the Original Code is Jakob Heuser
+<jakob@felocity.org>.
+Copyright (C) 2007. All Rights Reserved.
+
+---
+
+getoptx.sh is licensed under the Public Domain and may be distributed on its
+own, independent of this software package.
View
@@ -0,0 +1,221 @@
+#! /bin/bash
+
+# getopt.sh:
+# functions like getopts but do long-named options parsing
+# and support optional arguments
+#
+# Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain
+# Date created: December 21, 1997
+# Date modified: December 21, 1997
+#
+# IMPORTANT FEATURES
+#
+# 1) Parses both short and long-named options
+# 2) Supports optional arguments
+# 3) Only uses bash builtins, thus no calls to external
+# utilities such as expr or sed is done. Therefore,
+# parsing speed is high enough
+#
+#
+# DESCRIPTION
+#
+# FUNCTION getopt
+# Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse options with both required and optional arguments,
+# Options with optional arguments must have "." instead of ":" after them.
+# Furthemore, a variable name to place option name cannot be specified
+# and is always placed in OPTOPT variable
+#
+# This function is provided for compatibility with getopts()
+# OPTLIST style, and it actually calls getoptex (see bellow)
+#
+# NOTE that a list of parameters is required and must be either "$@",
+# if processing command line arguments, or some alternative parameters.
+#
+# FUNCTION getoptex
+# Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS}
+#
+# like getopts, but parse long-named options.
+#
+# Both getopt and getoptex return 0 if an option has been parsed,
+# and 1 if all options are already parsed or an error occured
+#
+# Both getopt and getoptex set or test the following variables:
+#
+# OPTERR -- tested for whether error messages must be given for invalid options
+#
+# OPTOPT -- set to the name of an option parsed,
+# or to "?" if no more options or error
+# OPTARG -- set to the option argument, if any;
+# unset if ther is no argument;
+# on error, set to the erroneous option name
+#
+# OPTIND -- Initialized to 1.
+# Then set to the number of the next parameter to be parsed
+# when getopt or getoptex will be called next time.
+# When all options are parsed, contains a number of
+# the first non-option argument.
+#
+#
+# OPTOFS -- If a parameter number $OPTIND containg an option parsed
+# does not contain any more options, OPTOFS is unset;
+# otherwise, OPTOFS is set to such a number of "?" signs
+# which is equal to the number of options parsed
+#
+# You might not set variables OPTIND and OPTOFS yourself
+# unless you want to parse a list of parameters more than once.
+# Otherwise, you whould unset OPTIND (or set it to 1)
+# and unset OPTOFS each time you want to parse a new parameters list
+#
+# Option list format is DIFFERENT from one for getopts or getopt. getopts-style
+# option list can be converted to getoptex-style using a function optlistex
+# (see bellow)
+#
+# DESCRIPTION of option list used with getoptex:
+# Option names are separated by whitespace. Options consiting of
+# more than one character are treated as long-named (--option)
+#
+# Special characters can appear at the and of option names specifying
+# whether an argument is required (default is ";"):
+# ";" (default) -- no argument
+# ":" -- required argument
+# "," -- optional argument
+#
+# For example, an option list "a b c help version f: file: separator."
+# defines the following options:
+# -a, -b, -c, --help, --version -- no argument
+# -f, --file -- argument required
+# --separator -- optional argument
+#
+# FUNCTION optlistex
+# Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST`
+#
+# Converts getopts-style option list in a format suitable for use with getoptex
+# Namely, it inserts spaces after each option name.
+#
+#
+# HOW TO USE
+#
+# In order o use in your bash scripts the functions described,
+# include a command ". getopt.sh" to the file containing the script,
+# which will define functions getopt, getoptex, and optlistex
+#
+# EXAMPLES
+#
+# See files 'getopt1' and 'getopt2' that contain sample scripts that use
+# getopt and getoptex functions respectively
+#
+#
+# Please send your comments to grg@philol.msu.ru
+
+function getoptex()
+{
+ let $# || return 1
+ local optlist="${1#;}"
+ let OPTIND || OPTIND=1
+ [ $OPTIND -lt $# ] || return 1
+ shift $OPTIND
+ if [ "$1" != "-" -a "$1" != "${1#-}" ]
+ then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ]
+ then
+ local o
+ o="-${1#-$OPTOFS}"
+ for opt in ${optlist#;}
+ do
+ OPTOPT="${opt%[;.:]}"
+ unset OPTARG
+ local opttype="${opt##*[^;:.]}"
+ [ -z "$opttype" ] && opttype=";"
+ if [ ${#OPTOPT} -gt 1 ]
+ then # long-named option
+ case $o in
+ "--$OPTOPT")
+ if [ "$opttype" != ":" ]; then return 0; fi
+ OPTARG="$2"
+ if [ -z "$OPTARG" ];
+ then # error: must have an agrument
+ let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT";
+ OPTOPT="?"
+ return 1;
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "--$OPTOPT="*)
+ if [ "$opttype" = ";" ];
+ then # error: must not have arguments
+ let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTARG=${o#"--$OPTOPT="}
+ return 0
+ ;;
+ esac
+ else # short-named option
+ case "$o" in
+ "-$OPTOPT")
+ unset OPTOFS
+ [ "$opttype" != ":" ] && return 0
+ OPTARG="$2"
+ if [ -z "$OPTARG" ]
+ then
+ echo "$0: error: -$OPTOPT must have an argument" >&2
+ OPTARG="$OPTOPT"
+ OPTOPT="?"
+ return 1
+ fi
+ OPTIND=$[OPTIND+1] # skip option's argument
+ return 0
+ ;;
+ "-$OPTOPT"*)
+ if [ $opttype = ";" ]
+ then # an option with no argument is in a chain of options
+ OPTOFS="$OPTOFS?" # move to the next option in the chain
+ OPTIND=$[OPTIND-1] # the chain still has other options
+ return 0
+ else
+ unset OPTOFS
+ OPTARG="${o#-$OPTOPT}"
+ return 0
+ fi
+ ;;
+ esac
+ fi
+ done
+ echo "$0: error: invalid option: $o"
+ fi; fi
+ OPTOPT="?"
+ unset OPTARG
+ return 1
+}
+function optlistex
+{
+ local l="$1"
+ local m # mask
+ local r # to store result
+ while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask
+ while [ -n "$l" ]
+ do
+ r="${r:+"$r "}${l%$m}" # append the first character of $l to $r
+ l="${l#?}" # cut the first charecter from $l
+ m="${m#?}" # cut one "?" sign from m
+ if [ -n "${l%%[^:.;]*}" ]
+ then # a special character (";", ".", or ":") was found
+ r="$r${l%$m}" # append it to $r
+ l="${l#?}" # cut the special character from l
+ m="${m#?}" # cut one more "?" sign
+ fi
+ done
+ echo $r
+}
+function getopt()
+{
+ local optlist=`optlistex "$1"`
+ shift
+ getoptex "$optlist" "$@"
+ return $?
+}
View
@@ -1,5 +1,31 @@
<?php
+function SNAP_usage() {
+ echo "\n";
+ echo "Usage: snaptest.sh [--out=outmode] [--php=phppath] [--help] <path>\n";
+ echo "Usage: php snaptest.php [--out=outmode] [--php=phppath] [--help] <path>\n";
+ echo "\n";
+ echo "<path> :: The path to the test you want to run. Should be a file\n";
+ echo "ending in .test.php or a directory.\n";
+ echo "\n";
+ echo "--out=outmode :: sets the output handler to 'outmode'. The\n";
+ echo "output mode must be located in <snaptest>/outmode.php.\n";
+ echo "\n";
+ echo "--php=phppath :: set the php path for recursion. If not specified,\n";
+ echo "the call 'php' will be used, using whatever is in the current env\n";
+ echo "variable.\n";
+ echo "\n";
+ echo "--match=regex :: Specifies a PCRE regular expression to match. Files\n";
+ echo "that match this regular expression will be included by the test\n";
+ echo "harness.\n";
+ exit;
+}
+
+// requires PHP 5.2+
+if (version_compare(phpversion(), '5.2.0') < 0) {
+ SNAP_usage();
+}
+
require_once 'snap.php';
require_once 'functions.php';
@@ -18,40 +44,13 @@
$php = (isset($options['php']) && $options['php']) ? $options['php'] : 'php';
$ofile = (isset($options['ofile']) && $options['ofile']) ? $options['ofile'] : tempnam('/tmp', 'SNAP');
$xtn = (isset($options['match']) && $options['match']) ? $options['match'] : '^.*\.stest\.php';
-$help = (isset($options['help']) && $options['help']) ? true : false;
+$help = (isset($options['help'])) ? true : false;
$path = (isset($options[0]) && $options[0]) ? $options[0] : '';
// help output if no path is specified
if ($path == '' || $help) {
- echo "\n";
- echo "Usage: snaptest.sh <path>\n";
- echo "Usage: php snaptest.php [--out=outmode] [--php=phppath]\n";
- echo " [--ofile=filepath] [--help] <path>\n";
- echo "\n";
- echo "<path> :: The path to the test you want to run. Should be a file\n";
- echo "ending in .test.php or a directory.\n";
- echo "\n";
- echo "--out=outmode :: sets the output handler to 'outmode'. The\n";
- echo "output mode must be located in <snaptest>/outmode.php.\n";
- echo "This is specific to snaptest.php only\n";
- echo "\n";
- echo "--php=phppath :: set the php path for recursion. If not specified,\n";
- echo "the call 'php' will be used, using whatever is in the current env\n";
- echo "variable.\n";
- echo "This is specific to snaptest.php only\n";
- echo "\n";
- echo "--ofile=filepath :: Declare a file you want to write to. This is\n";
- echo "especially useful if you plan to fork and want all reports to go\n";
- echo "to a single location. If ofile is not specified, a file in /tmp\n";
- echo "from tempnam() will be used.\n";
- echo "This is specific to snaptest.php only\n";
- echo "\n";
- echo "--match=regex :: Specifies a regular expression to match. Files\n";
- echo "that match this regular expression will be included by the test\n";
- echo "harness.\n";
- echo "This is specific to snaptest.php only\n";
- exit;
+ SNAP_usage();
}
View
@@ -1,17 +1,58 @@
#!/bin/bash
-#$> ./snaptest.sh <path>
-# runs snaptest/bootstrap.php --mode=directory --path=<path>
-#
+# load options
+. getoptx.sh
-# Debian / STD Linux
-# phppath=/usr/local/bin/php
+# Auto Locate PHP
+PHP=`which php`
+if [[ ! -x "$PHP" ]] ; then
+ PHP=""
+ if [ -z $PHP ] ; then
+ if [ -x "/usr/local/bin/php" ] ; then
+ PHP="/usr/local/bin/php"
+ fi
+ if [ -x "/usr/bin/php" ] ; then
+ PHP="/usr/bin/php"
+ fi
+ if [ -x "/opt/local/bin/php" ] ; then
+ PHP="/opt/local/bin/php"
+ fi
+ fi
+fi
-# OSX / Darwin
-phppath=/opt/local/bin/php
+# choke and die if we couldn't auto-find PHP
+if [ -z $PHP ] ; then
+ echo "PHP was not found in any common location. You will need to"
+ echo "supply the --php=<path> switch."
+ exit 0
+fi
-# END CONFIG
-filepath=`dirname "$0"`
-cd $filepath;
+# parse the options
+CMD=""
+while getoptex "out. php. match. help;" "$@"
+do
+ if [ "$OPTOPT" = "php" ] ; then
+ if [ -n PHP ] ; then
+ echo "OHSHITNOPHPLAWL"
+ fi
+ PHP=$OPTARG
+ fi
+ if [ "$OPTOPT" = "help" ] ; then
+ CMD="$CMD --help"
+ else
+ CMD="$CMD --$OPTOPT=$OPTARG"
+ fi
+done
+shift $[OPTIND-1]
+for arg in "$@"
+do
+ CMD="$CMD $arg"
+done
-$phppath $filepath/snaptest.php --out=text --php=$phppath $1
+# change to shell script real location
+FPATH=`dirname "$0"`
+cd $FPATH;
+
+# run php on the snaptest.php file with the commands
+CMD="$PHP snaptest.php $CMD"
+$CMD

0 comments on commit 21f361e

Please sign in to comment.