File: SpotCommandline.php

package info (click to toggle)
spotweb 20130826%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,132 kB
  • ctags: 11,281
  • sloc: php: 31,367; xml: 1,009; sh: 148; makefile: 83
file content (91 lines) | stat: -rwxr-xr-x 2,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php

class SpotCommandline {
	private static $_parsed = null;
	private static $_defaults = null;

	/*
	 * $noopt is the list of parameters without value
	 * $defaults is the list of default values for parameters
	 *
	 */
	static public function initialize($noopt, $defaults) {
		self::$_parsed = self::parseParameters($noopt);
		self::$_defaults = $defaults;
	} # initialize
	
	/*
	 * Are we running from the commandline?
	 */
	static public function isCommandline() {
		return (!isset($_SERVER['SERVER_PROTOCOL']));
	} # isCommandline
	
	/*
	 * Returns either the value of the function or '1' 
	 * when set
	 *
	 * When the parameter is not given, we return the
	 * default value
	 */
	static public function get($val) {
		if (isset(self::$_parsed[$val])) {
			return self::$_parsed[$val];
		} else {
			return self::$_defaults[$val];
		} # else
	} # get
	
   /**
     * 
	 * This code is copied from:
	 *		http://nl.php.net/manual/en/function.getopt.php#83414
	 *
     * Parses $GLOBALS['argv'] for parameters and assigns them to an array.
     *
     * Supports:
     * -e
     * -e <value>
     * --long-param
     * --long-param=<value>
     * --long-param <value>
     * <value>
     *
     * @param array $noopt List of parameters without values
     */
    static private function parseParameters($noopt = array()) {
        $result = array();

        if (!isset($GLOBALS['argv'])) {
            return $result;
        } # if
        
        $params = $GLOBALS['argv'];
		
        // could use getopt() here (since PHP 5.3.0), but it doesn't work reliably
        reset($params);
        while (list($tmp, $p) = each($params)) {
            if ($p{0} == '-') {
                $pname = substr($p, 1);
                $value = true;
                if ($pname{0} == '-') {
                    // long-opt (--<param>)
                    $pname = substr($pname, 1);
                    if (strpos($p, '=') !== false) {
                        // value specified inline (--<param>=<value>)
                        list($pname, $value) = explode('=', substr($p, 2), 2);
                    }
                }
                // check if next parameter is a descriptor or a value
                $nextparm = current($params);
                if (!in_array($pname, $noopt) && $value === true && $nextparm !== false && $nextparm{0} != '-') list($tmp, $value) = each($params);
                $result[$pname] = $value;
            } else {
                // param doesn't belong to any option
                $result[] = $p;
            }
        }
        return $result;
    } # parseParameters
	
} # SpotCommandline