Passed
Push — develop ( 6a6d60...d23fdb )
by nguereza
02:35
created

Option::isBool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Console
5
 *
6
 * Platine Console is a powerful library with support of custom
7
 * style to build command line interface applications
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Console
12
 * Copyright (c) 2017-2020 Jitendra Adhikari
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Option.php
35
 *
36
 *  The Input Option class
37
 *
38
 *  @package    Platine\Console\Input
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   http://www.iacademy.cf
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Console\Input;
50
51
/**
52
 * Class Option
53
 * @package Platine\Console\Input
54
 */
55
class Option extends Parameter
56
{
57
58
    /**
59
     * The short option name
60
     * @var string
61
     */
62
    protected string $short = '';
63
64
     /**
65
     * The long option name
66
     * @var string
67
     */
68
    protected string $long = '';
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function parse(string $raw): void
74
    {
75
        if (strpos($this->raw, '-with-') !== false) {
76
            $this->default = false;
77
        } elseif (strpos($this->raw, '-no-') !== false) {
78
            $this->default = true;
79
        }
80
81
        $parts = preg_split('/[\s,\|]+/', $raw);
82
83
        if ($parts !== false) {
84
            $this->short = $parts[0];
85
            $this->long = $parts[0];
86
87
            if (isset($parts[1])) {
88
                $this->long = $parts[1];
89
            }
90
        }
91
92
        $this->name = str_replace(['--', 'no-', 'with-'], '', $this->long);
93
    }
94
95
    /**
96
     * Return the short option name
97
     * @return string
98
     */
99
    public function getShort(): string
100
    {
101
        return $this->short;
102
    }
103
104
    /**
105
     * Return the long option name
106
     * @return string
107
     */
108
    public function getLong(): string
109
    {
110
        return $this->long;
111
    }
112
113
    /**
114
     * Whether the given argument match option name
115
     * @param string $arg
116
     * @return bool
117
     */
118
    public function is(string $arg): bool
119
    {
120
        return $this->short === $arg
121
              || $this->long === $arg;
122
    }
123
124
    /**
125
     * Check if the option is bool type.
126
     * @return bool
127
     */
128
    public function isBool(): bool
129
    {
130
        return preg_match('/\-no-|\-with-/', $this->long) > 0;
131
    }
132
}
133