Updated README.md
Added LICENSE Added tests (Unfinished) Added Codebase Added .gitignore Added .idea files
This commit is contained in:
parent
2ce6ed5418
commit
a24349b0b5
13 changed files with 366 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
build/
|
26
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
26
.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="IncorrectHttpHeaderInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="customHeaders">
|
||||||
|
<set>
|
||||||
|
<option value="Subject" />
|
||||||
|
<option value="Reply-To" />
|
||||||
|
<option value="X-JSON-Schema" />
|
||||||
|
<option value="X-JSON-Type" />
|
||||||
|
<option value="X-JSON-Path" />
|
||||||
|
<option value="X-Java-Type" />
|
||||||
|
<option value="X-Region-Id" />
|
||||||
|
<option value="X-GraphQL-Variables" />
|
||||||
|
<option value="X-SSH-Private-Key" />
|
||||||
|
<option value="X-Args-0" />
|
||||||
|
<option value="X-Args-1" />
|
||||||
|
<option value="X-Args-2" />
|
||||||
|
<option value="X-Args-3" />
|
||||||
|
<option value="X-Args-4" />
|
||||||
|
<option value="X-Args-5" />
|
||||||
|
</set>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/optslib.iml" filepath="$PROJECT_DIR$/.idea/optslib.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
11
.idea/optslib.iml
generated
Normal file
11
.idea/optslib.iml
generated
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
19
.idea/php.xml
generated
Normal file
19
.idea/php.xml
generated
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="MessDetectorOptionsConfiguration">
|
||||||
|
<option name="transferred" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PHPCSFixerOptionsConfiguration">
|
||||||
|
<option name="transferred" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PHPCodeSnifferOptionsConfiguration">
|
||||||
|
<option name="transferred" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PhpProjectSharedConfiguration" php_language_level="7.0" />
|
||||||
|
<component name="PhpStanOptionsConfiguration">
|
||||||
|
<option name="transferred" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PsalmOptionsConfiguration">
|
||||||
|
<option name="transferred" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
18
LICENSE
Normal file
18
LICENSE
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
Copyright 2022 Nosial All Rights Reserved.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation
|
||||||
|
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the Software
|
||||||
|
is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||||
|
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||||
|
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
14
Makefile
Normal file
14
Makefile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
debug:
|
||||||
|
ncc build --config="debug"
|
||||||
|
|
||||||
|
release:
|
||||||
|
ncc build --config="release"
|
||||||
|
|
||||||
|
install:
|
||||||
|
ncc package install --package="build/release/net.nosial.optslib.ncc"
|
||||||
|
|
||||||
|
install-debug:
|
||||||
|
ncc package install --package="build/debug/net.nosial.optslib.ncc"
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
ncc package uninstall -y --package="net.nosial.optslib"
|
109
README.md
Normal file
109
README.md
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
# OptsLib
|
||||||
|
|
||||||
|
A very simple Options parser and command-line arguments
|
||||||
|
handling library for PHP.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
The usage of this library is very simple, there are
|
||||||
|
two functions that you can use to parse options.
|
||||||
|
|
||||||
|
### parseArgument()
|
||||||
|
|
||||||
|
Can be used to parse a single argument string/array, this is useful for
|
||||||
|
parsing command line arguments. The second argument is the maximum number
|
||||||
|
of arguments that can be parsed, this is to prevent infinite loops.
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('ncc');
|
||||||
|
import('net.nosial.optslib', 'latest');
|
||||||
|
|
||||||
|
$input = '--foo --bar="test" -y --username test';
|
||||||
|
$parsed = \OptsLib\Parse::parseArgument($input);
|
||||||
|
|
||||||
|
echo $parsed['foo']; // true
|
||||||
|
echo $parsed['bar']; // "test"
|
||||||
|
echo $parsed['y']; // true
|
||||||
|
echo $parsed['username']; // "test"
|
||||||
|
```
|
||||||
|
|
||||||
|
### getArguments()
|
||||||
|
|
||||||
|
This method is used to access the `$argv` array
|
||||||
|
if it's not set, an empty array is returned.
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('ncc');
|
||||||
|
import('net.nosial.optslib', 'latest');
|
||||||
|
|
||||||
|
$arguments = \OptsLib\Parse::getArguments();
|
||||||
|
|
||||||
|
echo $arguments['output-file']; // "test.txt"
|
||||||
|
echo $arguments['input-file']; // "test.txt"
|
||||||
|
echo $arguments['log']; // "verbose"
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## building
|
||||||
|
|
||||||
|
In order to use this library you need to install it
|
||||||
|
via `ncc`, this can be done using NCC's command-line
|
||||||
|
interface or mentioning this library in your `package.json`
|
||||||
|
file's dependencies, for instance:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"net.nosial.optslib": "nosial/libs.opts=latest@n64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If you don't have N64's repository added to your NCC
|
||||||
|
configuration, you can add it using the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ sudo ncc source add --name n64 --type gitlab --host git.n64.cc
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Building is done using [NCC](https://git.n64.cc/nosial/ncc),
|
||||||
|
you can build it using the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ git clone https://git.n64.cc/nosial/libs/optslib.git
|
||||||
|
$ cd optlibs
|
||||||
|
|
||||||
|
$ ncc build --config release
|
||||||
|
# or
|
||||||
|
$ make release
|
||||||
|
```
|
||||||
|
|
||||||
|
### installing
|
||||||
|
|
||||||
|
Once you have built the library, you can install it using
|
||||||
|
the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ sudo ncc install -p="build/release/net.nosial.optslib.ncc"
|
||||||
|
# or
|
||||||
|
$ sudo make install
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This library is licensed under the MIT license, see the LICENSE file
|
||||||
|
for more information.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
If you want to contribute to this project, you can do so by
|
||||||
|
creating a merge request on the [GitLab repository](https://git.n64.cc/nosial/libs/optslib).
|
||||||
|
|
||||||
|
## Contact
|
||||||
|
|
||||||
|
If you have any questions, you can contact us on [Telegram](https://t.me/NosialDiscussions).
|
42
project.json
Normal file
42
project.json
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
{
|
||||||
|
"project": {
|
||||||
|
"compiler": {
|
||||||
|
"extension": "php",
|
||||||
|
"minimum_version": "8.0",
|
||||||
|
"maximum_version": "8.2"
|
||||||
|
},
|
||||||
|
"options": []
|
||||||
|
},
|
||||||
|
"assembly": {
|
||||||
|
"name": "OptsLib",
|
||||||
|
"package": "net.nosial.optslib",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"uuid": "20aefdfa-7b91-11ed-919f-cb63712c8e36"
|
||||||
|
},
|
||||||
|
"build": {
|
||||||
|
"source_path": "src",
|
||||||
|
"default_configuration": "debug",
|
||||||
|
"define_constants": {
|
||||||
|
"ASSEMBLY_NAME": "%ASSEMBLY.NAME%",
|
||||||
|
"ASSEMBLY_PACKAGE": "%ASSEMBLY.PACKAGE%",
|
||||||
|
"ASSEMBLY_VERSION": "%ASSEMBLY.VERSION%",
|
||||||
|
"ASSEMBLY_UID": "%ASSEMBLY.UID%"
|
||||||
|
},
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "debug",
|
||||||
|
"output_path": "build/debug",
|
||||||
|
"define_constants": {
|
||||||
|
"DEBUG": "1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "release",
|
||||||
|
"output_path": "build/release",
|
||||||
|
"define_constants": {
|
||||||
|
"DEBUG": "0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
104
src/OptsLib/Parse.php
Normal file
104
src/OptsLib/Parse.php
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OptsLib;
|
||||||
|
|
||||||
|
class Parse
|
||||||
|
{
|
||||||
|
private static $Regex = "/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $ArgsCache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the input arguments into an array of flags and values
|
||||||
|
*
|
||||||
|
* @param $input
|
||||||
|
* @param int $max_arguments
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function parseArgument($input, int $max_arguments=1000): array
|
||||||
|
{
|
||||||
|
if (is_string($input))
|
||||||
|
{
|
||||||
|
$flags = $input;
|
||||||
|
}
|
||||||
|
elseif(is_array($input))
|
||||||
|
{
|
||||||
|
$flags = implode(' ', $input);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
global $argv;
|
||||||
|
if(isset($argv) && count($argv) > 1)
|
||||||
|
{
|
||||||
|
array_shift($argv);
|
||||||
|
}
|
||||||
|
$flags = implode(' ', $argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
$configs = array();
|
||||||
|
preg_match_all(self::$Regex, $flags, $matches, PREG_SET_ORDER);
|
||||||
|
|
||||||
|
foreach ($matches as $index => $match)
|
||||||
|
{
|
||||||
|
if (isset($match['value']) && $match['value'] !== '')
|
||||||
|
{
|
||||||
|
$value = $match['value'];
|
||||||
|
}
|
||||||
|
else if (isset($match['string']) && $match['string'] !== '')
|
||||||
|
{
|
||||||
|
// fix escaped quotes
|
||||||
|
$value = str_replace("\\\"", "\"", $match['string']);
|
||||||
|
$value = str_replace("\\'", "'", $value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$value = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($match['bigflag']) && $match['bigflag'] !== '')
|
||||||
|
{
|
||||||
|
$configs[$match['bigflag']] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($match['smallflag']) && $match['smallflag'] !== '')
|
||||||
|
{
|
||||||
|
$configs[$match['smallflag']] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($match['unmatched']) && $match['unmatched'] !== '')
|
||||||
|
{
|
||||||
|
$configs[$match['unmatched']] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($index >= $max_arguments)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $configs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the arguments from the command line
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getArguments(): array
|
||||||
|
{
|
||||||
|
if(self::$ArgsCache == null)
|
||||||
|
{
|
||||||
|
if(isset($argv))
|
||||||
|
{
|
||||||
|
self::$ArgsCache = self::parseArgument($argv);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self::$ArgsCache = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$ArgsCache;
|
||||||
|
}
|
||||||
|
}
|
4
tests/parse_arguments.php
Normal file
4
tests/parse_arguments.php
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$parse = \OptsLib\Parse::getArguments();
|
||||||
|
var_dump($parse);
|
4
tests/parse_test.php
Normal file
4
tests/parse_test.php
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$parse = \OptsLib\Parse::parseArgument('test --foo=bar --baz=qux --quux --corge --grault=garply --waldo --fred --plugh --xyzzy --thud');
|
||||||
|
var_dump($parse);
|
Loading…
Add table
Add a link
Reference in a new issue