Args::getOptions()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 8
nop 1
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
namespace phpbu\App\Cmd;
3
4
use phpbu\App\Exception;
5
6
/**
7
 * Cli argument parser.
8
 *
9
 * @package    phpbu
10
 * @subpackage Cmd
11
 * @author     Sebastian Feldmann <[email protected]>
12
 * @copyright  Sebastian Feldmann <[email protected]>
13
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
14
 * @link       http://phpbu.de/
15
 * @since      Class available since Release 1.0.0
16
 * @internal
17
 */
18
final class Args
19
{
20
21
    /**
22
     * List of available - options.
23
     *
24
     * @var array
25
     */
26
    private $shortOptions = [
27
        'h' => true,
28
        'v' => true,
29
        'V' => true,
30
    ];
31
32
    /**
33
     * List of available -- options.
34
     *
35
     * @var array
36
     */
37
    private $longOptions = [
38
        'bootstrap='             => true,
39
        'colors'                 => true,
40
        'configuration='         => true,
41
        'debug'                  => true,
42
        'generate-configuration' => true,
43
        'help'                   => true,
44
        'limit='                 => true,
45
        'restore'                => true,
46
        'simulate'               => true,
47
        'verbose'                => true,
48
        'version'                => true
49
    ];
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param bool $isPhar
55
     */
56 17
    public function __construct(bool $isPhar = false)
57
    {
58 17
        if ($isPhar) {
59 1
            $this->longOptions['self-update']   = true;
60 1
            $this->longOptions['version-check'] = true;
61
        }
62 17
    }
63
64
    /**
65
     * Get all cli options.
66
     *
67
     * @param  array $args
68
     * @return array
69
     * @throws \phpbu\App\Exception
70
     */
71 17
    public function getOptions(array $args) : array
72
    {
73
        // remove script name from args
74 17
        if (isset($args[0][0]) && $args[0][0] != '-') {
75 7
            array_shift($args);
76
        }
77
78 17
        $options = [];
79
80 17
        reset($args);
81 17
        array_map('trim', $args);
82
83 17
        foreach ($args as $i => $arg) {
84 17
            $argLength = strlen($arg);
85
            // if empty arg or arg doesn't start with "-" skip it
86 17
            if (empty($arg) || $arg == '--' || $arg[0] != '-') {
87 10
                continue;
88
            }
89 17
            if ($argLength > 1 && $arg[1] == '-') {
90 13
                $this->parseLongOption(substr($arg, 2), $options);
91
            } else {
92 13
                $this->parseShortOption(substr($arg, 1), $options);
93
            }
94
        }
95 12
        return $options;
96
    }
97
98
    /**
99
     * Check short option and put into option list.
100
     *
101
     * @param  string $arg
102
     * @param  array $options
103
     * @throws \phpbu\App\Exception
104
     */
105 4
    public function parseShortOption($arg, array &$options)
106
    {
107 4
        if (!isset($this->shortOptions[$arg])) {
108 1
            throw new Exception('unknown option: -' . $arg);
109
        }
110 3
        $options['-' . $arg] = true;
111 3
    }
112
113
    /**
114
     * Check long option and put into option list.
115
     *
116
     * @param  string $arg
117
     * @param  array $options
118
     * @throws \phpbu\App\Exception
119
     */
120 13
    public function parseLongOption($arg, array &$options)
121
    {
122 13
        $list     = explode('=', $arg);
123 13
        $option   = $list[0];
124 13
        $argument = true;
125
126 13
        if (count($list) > 1) {
127 5
            $argument = $list[1];
128
        }
129 13
        if (count($list) > 2) {
130 1
            throw new Exception('invalid value for option: --' . $arg);
131
        }
132 12
        if (!isset($this->longOptions[$option]) && !isset($this->longOptions[$option . '='])) {
133 1
            throw new Exception('unknown option: --' . $option);
134
        }
135 11
        if ($argument === true && isset($this->longOptions[$option . '='])) {
136 1
            throw new Exception('argument required for option: --' . $option);
137
        }
138 10
        if ($argument !== true && isset($this->longOptions[$option])) {
139 1
            throw new Exception('needless argument for option: --' . $option);
140
        }
141 9
        $options['--' . $option] = $argument;
142 9
    }
143
}
144