symfttpd /lib/Argument.php

Language PHP Lines 49
MD5 Hash 8f9968d1b50c79c26c94b35ed83c8700
Repository https://github.com/fabriceb/symfttpd.git View Raw File View Project SPDX
 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
<?php
if (version_compare(PHP_VERSION, '5.3.0') < 0)
{
  require dirname(__FILE__).'/getopt.php';
}

class Argument
{
  /**
   * Get command line options in a sane way.
   *
   * Examples:
   *    getopt("p", "port", 42)
   *    getopt("n", "nofork", false)
   *    getopt("P", "path", null, true)
   * @param string $short Short option (one letter)
   * @param string $long Long option (many letters)
   * @param mixed $default "42". If strictly equal to false, the option \
   *   is considered a boolean (option present returning true, false otherwise)
   * @param boolean $required Required option (ignored if $default is false)
   * @return string|boolean
   */
  static public function get($short, $long, $default, $required = false)
  {
    $addon = $default === false ? '' : ':';

    $options = (version_compare(PHP_VERSION, '5.3.0') < 0)
      ? _getopt($short.$addon, array($long.$addon))
      : getopt($short.$addon, array($long.$addon));

    $value = $default;
    if (isset($options[$short]))
    {
      $value = $default === false ? true : $options[$short];
    }
    if (isset($options[$long]))
    {
      $value = $default === false ? true : $options[$long];
    }

    if ($required && is_null($value))
    {
      throw new Exception('Missing required "'.$long.'" parameter');
    }

    return $value;
  }
}
Back to Top