diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ea8af9c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ae1a1f8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/optslib.iml b/.idea/optslib.iml new file mode 100644 index 0000000..4348106 --- /dev/null +++ b/.idea/optslib.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..de0a636 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..10ffa64 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..838591f --- /dev/null +++ b/Makefile @@ -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" \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..883d468 --- /dev/null +++ b/README.md @@ -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 + 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; + } + } \ No newline at end of file diff --git a/tests/parse_arguments.php b/tests/parse_arguments.php new file mode 100644 index 0000000..2f3a8b7 --- /dev/null +++ b/tests/parse_arguments.php @@ -0,0 +1,4 @@ +