Option   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A is() 0 3 2
A short() 0 3 1
A parse() 0 16 4
A long() 0 3 1
A bool() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the PHP-CLI package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Cli\Input;
13
14
/**
15
 * Cli Option.
16
 *
17
 * @author  Jitendra Adhikari <[email protected]>
18
 * @license MIT
19
 *
20
 * @link    https://github.com/adhocore/cli
21
 */
22
class Option extends Parameter
23
{
24
    /** @var string Short name */
25
    protected $short = '';
26
27
    /** @var string Long name */
28
    protected $long = '';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function parse(string $raw)
34
    {
35
        if (\strpos($raw, '-with-') !== false) {
36
            $this->default = false;
37
        } elseif (\strpos($raw, '-no-') !== false) {
38
            $this->default = true;
39
        }
40
41
        $parts = \preg_split('/[\s,\|]+/', $raw);
42
43
        $this->short = $this->long = $parts[0];
44
        if (isset($parts[1])) {
45
            $this->long = $parts[1];
46
        }
47
48
        $this->name = \str_replace(['--', 'no-', 'with-'], '', $this->long);
49
    }
50
51
    /**
52
     * Get long name.
53
     *
54
     * @return string
55
     */
56
    public function long(): string
57
    {
58
        return $this->long;
59
    }
60
61
    /**
62
     * Get short name.
63
     *
64
     * @return string
65
     */
66
    public function short(): string
67
    {
68
        return $this->short;
69
    }
70
71
    /**
72
     * Test if this option matches given arg.
73
     *
74
     * @param string $arg
75
     *
76
     * @return bool
77
     */
78
    public function is(string $arg): bool
79
    {
80
        return $this->short === $arg || $this->long === $arg;
81
    }
82
83
    /**
84
     * Check if the option is boolean type.
85
     *
86
     * @return bool
87
     */
88
    public function bool(): bool
89
    {
90
        return \preg_match('/\-no-|\-with-/', $this->long) > 0;
91
    }
92
}
93