Compare commits

..

No commits in common. "master" and "1.1.0" have entirely different histories.

14 changed files with 68 additions and 525 deletions

View file

@ -1,308 +0,0 @@
name: CI
on:
push:
branches:
- '**'
release:
types: [created]
workflow_dispatch:
jobs:
release:
runs-on: ubuntu-latest
container:
image: php:8.3
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
- name: Install phive
run: |
wget -O phive.phar https://phar.io/releases/phive.phar
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
gpg --verify phive.phar.asc phive.phar
chmod +x phive.phar
mv phive.phar /usr/local/bin/phive
- name: Install phab
run: |
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
- name: Install latest version of NCC
run: |
git clone https://git.n64.cc/nosial/ncc.git
cd ncc
make redist
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
if [ -z "$NCC_DIR" ]; then
echo "NCC build directory not found"
exit 1
fi
php "$NCC_DIR/INSTALL" --auto
cd .. && rm -rf ncc
- name: Build project
run: |
ncc build --config release --build-source --log-level debug
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: release
path: build/release/net.nosial.optslib.ncc
release-executable:
runs-on: ubuntu-latest
container:
image: php:8.3
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
- name: Install phive
run: |
wget -O phive.phar https://phar.io/releases/phive.phar
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
gpg --verify phive.phar.asc phive.phar
chmod +x phive.phar
mv phive.phar /usr/local/bin/phive
- name: Install phab
run: |
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
- name: Install latest version of NCC
run: |
git clone https://git.n64.cc/nosial/ncc.git
cd ncc
make redist
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
if [ -z "$NCC_DIR" ]; then
echo "NCC build directory not found"
exit 1
fi
php "$NCC_DIR/INSTALL" --auto
cd .. && rm -rf ncc
- name: Build project
run: |
ncc build --config release-executable --build-source --log-level debug
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: release-executable
path: build/release/release_executable_gz
# Checking for phpunit.xml
check-phpunit:
runs-on: ubuntu-latest
outputs:
phpunit-exists: ${{ steps.check.outputs.phpunit-exists }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for phpunit.xml
id: check
run: |
if [ -f phpunit.xml ]; then
echo "phpunit-exists=true" >> $GITHUB_OUTPUT
else
echo "phpunit-exists=false" >> $GITHUB_OUTPUT
fi
# Checking for phpdoc.dist.xml
check-phpdoc:
runs-on: ubuntu-latest
outputs:
phpdoc-exists: ${{ steps.check.outputs.phpdoc-exists }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Check for phpdoc.dist.xml
id: check
run: |
if [ -f phpdoc.dist.xml ]; then
echo "phpdoc-exists=true" >> $GITHUB_OUTPUT
else
echo "phpdoc-exists=false" >> $GITHUB_OUTPUT
fi
generate-phpdoc:
needs: [release, check-phpdoc]
runs-on: ubuntu-latest
container:
image: php:8.3
if: needs.check-phpdoc.outputs.phpdoc-exists == 'true'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
- name: Download PHPDocumentor
run: |
wget https://phpdoc.org/phpDocumentor.phar
chmod +x phpDocumentor.phar
- name: Generate PHPDoc
run: |
php phpDocumentor.phar -d src -t docs
- name: Archive PHPDoc
run: |
zip -r docs.zip docs
- name: Upload PHPDoc
uses: actions/upload-artifact@v4
with:
name: documentation
path: docs.zip
test:
needs: [release, release-executable, check-phpunit]
runs-on: ubuntu-latest
container:
image: php:8.3
if: needs.check-phpunit.outputs.phpunit-exists == 'true'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: release
path: release
- name: Install dependencies
run: |
apt update -yqq
apt install git libpq-dev libzip-dev zip make wget gnupg -yqq
curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions
chmod +x /usr/local/bin/install-php-extensions
install-php-extensions zip
- name: Install phive
run: |
wget -O phive.phar https://phar.io/releases/phive.phar
wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc
gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79
gpg --verify phive.phar.asc phive.phar
chmod +x phive.phar
mv phive.phar /usr/local/bin/phive
- name: Install phab
run: |
phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C
- name: Install latest version of NCC
run: |
git clone https://git.n64.cc/nosial/ncc.git
cd ncc
make redist
NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1)
if [ -z "$NCC_DIR" ]; then
echo "NCC build directory not found"
exit 1
fi
php "$NCC_DIR/INSTALL" --auto
cd .. && rm -rf ncc
- name: Install NCC packages
run: |
ncc package install --package="release/net.nosial.optslib.ncc" --build-source --reinstall -y --log-level debug
- name: Run PHPUnit tests
run: |
wget https://phar.phpunit.de/phpunit-11.3.phar
php phpunit-11.3.phar --configuration phpunit.xml --log-junit reports/junit.xml --log-teamcity reports/teamcity --testdox-html reports/testdox.html --testdox-text reports/testdox.txt
- name: Upload test reports
uses: actions/upload-artifact@v4
with:
name: reports
path: reports
release-documentation:
needs: generate-phpdoc
permissions: write-all
runs-on: ubuntu-latest
container:
image: php:8.3
if: github.event_name == 'release'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download documentation artifact
uses: actions/download-artifact@v4
with:
name: documentation
path: documentation
- name: Upload documentation artifact
uses: softprops/action-gh-release@v1
with:
files: |
documentation/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
release-artifacts:
needs: [release, release-executable]
permissions: write-all
runs-on: ubuntu-latest
container:
image: php:8.3
if: github.event_name == 'release'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download release artifact
uses: actions/download-artifact@v4
with:
name: release
path: release
- name: Upload release artifact to release
uses: softprops/action-gh-release@v1
with:
files: |
release/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Download release-executable artifact
uses: actions/download-artifact@v4
with:
name: release-executable
path: release-executable
- name: Upload release-executable artifact to release
uses: softprops/action-gh-release@v1
with:
files: |
release-executable/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4
.gitignore vendored
View file

@ -1,5 +1 @@
build/ build/
/.idea/gbrowser_project.xml
/.phpunit.result.cache
/.idea/php-test-framework.xml

7
.idea/php.xml generated
View file

@ -14,17 +14,12 @@
<path value="/usr/share/php" /> <path value="/usr/share/php" />
</include_path> </include_path>
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.3"> <component name="PhpProjectSharedConfiguration" php_language_level="7.1">
<option name="suggestChangeDefaultLanguageLevel" value="false" /> <option name="suggestChangeDefaultLanguageLevel" value="false" />
</component> </component>
<component name="PhpStanOptionsConfiguration"> <component name="PhpStanOptionsConfiguration">
<option name="transferred" value="true" /> <option name="transferred" value="true" />
</component> </component>
<component name="PhpUnit">
<phpunit_settings>
<PhpUnitSettings load_method="PHPUNIT_PHAR" custom_loader_path="" phpunit_phar_path="$USER_HOME$/phpunit.phar" />
</phpunit_settings>
</component>
<component name="PsalmOptionsConfiguration"> <component name="PsalmOptionsConfiguration">
<option name="transferred" value="true" /> <option name="transferred" value="true" />
</component> </component>

View file

@ -5,30 +5,6 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.3] - 2024-10-29
This update introduces a critical bug fix
### Fixed
- Fixed issue where if global $argv is not available, a check to see if `$_SERVER['argv']` is set
before accessing it due to a potential 'undefined' error.
## [1.1.2] - 2024-10-13
Updated Build System
## [1.1.1] - 2024-09-24
Updated OptsLib to work with php 8.3+ & ncc 2.1.0+
### Changed
- Introduced typed properties to OptsLib
### Fixed
- Fixed code-smell from OptsLib
## [1.1.0] - 2023-10-10 ## [1.1.0] - 2023-10-10
@ -36,6 +12,7 @@ Updated OptsLib to work with php 8.3+ & ncc 2.1.0+
Updated optslib to work with ncc 2.+. Updated optslib to work with ncc 2.+.
### Fixed ### Fixed
- Fixed code-smell from optslib. - Fixed code-smell from optslib.

View file

@ -1,25 +1,27 @@
# Variables # Variables
DEFAULT_CONFIGURATION ?= release NCC = ncc
LOG_LEVEL = debug PACKAGE_NAME = net.nosial.optslib.ncc
BUILD_CONFIG = release
# Default Target # Directories
all: release release-executable SRC_DIR = src
BUILD_DIR = build/$(BUILD_CONFIG)
# Build Steps .PHONY: all release install uninstall clean
release:
ncc build --config=release --log-level $(LOG_LEVEL)
release-executable:
ncc build --config=release-executable --log-level $(LOG_LEVEL)
all: release install
install: release release: prepare_build
ncc package install --package=build/release/net.nosial.optslib.ncc --skip-dependencies --build-source --reinstall -y --log-level $(LOG_LEVEL) $(NCC) build --config=$(BUILD_CONFIG)
test: release install: prepare_build
[ -f phpunit.xml ] || { echo "phpunit.xml not found"; exit 1; } $(NCC) package install --package="$(BUILD_DIR)/$(PACKAGE_NAME)" --skip-dependencies -y
phpunit
uninstall:
$(NCC) package uninstall -y --package="$(PACKAGE_NAME)"
clean: clean:
rm -rf build rm -rf $(BUILD_DIR)
.PHONY: all install test clean release release-executable prepare_build:
mkdir -p $(BUILD_DIR)

View file

@ -2,29 +2,13 @@
A basic Options parser and command-line arguments handling a library for PHP. A basic Options parser and command-line arguments handling a library for PHP.
## Community
This project and many others from Nosial are available on multiple publicly available and free git repositories at
- [n64](https://git.n64.cc/nosial/optslib)
- [GitHub](https://github.com/nosial/optslib)
- [Codeberg](https://codeberg.org/nosial/optslib)
Issues & Pull Requests are frequently checked and to be referenced accordingly in commits and changes, Nosial remains
dedicated to keep these repositories up to date when possible.
For questions & discussions see the public Telegram community at [@NosialDiscussions](https://t.me/NosialDiscussions).
We do encourage community support and discussions, please be respectful and follow the rules of the community.
## Table of Contents ## Table of Contents
<!-- TOC --> <!-- TOC -->
* [OptsLib](#optslib) * [OptsLib](#optslib)
* [Community](#community)
* [Table of Contents](#table-of-contents) * [Table of Contents](#table-of-contents)
* [Installation](#installation) * [Installation](#installation)
* [Compiling from source](#compiling-from-source) * [Compiling from source](#compiling-from-source)
* [Testing](#testing)
* [Usage](#usage) * [Usage](#usage)
* [parseArgument()](#parseargument) * [parseArgument()](#parseargument)
* [getArguments()](#getarguments) * [getArguments()](#getarguments)
@ -42,11 +26,7 @@ We do encourage community support and discussions, please be respectful and foll
The library can be installed using ncc: The library can be installed using ncc:
```bash ```bash
# n64 ncc install -p "nosial/libs.opts=latest@n64"
ncc package install -p "nosial/libs.opts=latest@n64"
# github
ncc package install -p "nosial/libs.opts=latest@github"
``` ```
or by adding the following to your project.json file under or by adding the following to your project.json file under
@ -56,7 +36,6 @@ the `build.dependencies` section:
{ {
"name": "net.nosial.optslib", "name": "net.nosial.optslib",
"version": "latest", "version": "latest",
"source_type": "remote",
"source": "nosial/libs.opts=latest@n64" "source": "nosial/libs.opts=latest@n64"
} }
``` ```
@ -82,16 +61,6 @@ or by running the following command:
make release make release
``` ```
## Testing
The library can be tested using PhpUnit with the `phpunit.xml` file that is already included in the repository.
This requires that you have PhpUnit installed & the library has been compiled and installed on the local system.
```bash
phpunit
```
## Usage ## Usage
The usage of this library is very simple, there are The usage of this library is very simple, there are

View file

@ -1,3 +0,0 @@
<?php
require 'ncc';
import('net.nosial.optslib');

View file

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpdocumentor configVersion="3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.phpdoc.org" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/phpDocumentor/phpDocumentor/master/data/xsd/phpdoc.xsd">
<paths>
<output>build/docs</output>
<cache>build/cache</cache>
</paths>
<version number="latest">
<api>
<source dsn=".">
<path>src/OptsLib</path>
</source>
<default-package-name>OptsLib</default-package-name>
</api>
</version>
</phpdocumentor>

View file

@ -1,11 +0,0 @@
<phpunit bootstrap="bootstrap.php">
<testsuites>
<testsuite name="OptsLib Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="error_reporting" value="-1"/>
<server name="KERNEL_DIR" value="app/"/>
</php>
</phpunit>

View file

@ -2,8 +2,8 @@
"project": { "project": {
"compiler": { "compiler": {
"extension": "php", "extension": "php",
"minimum_version": "8.2", "minimum_version": "8.0",
"maximum_version": "8.0" "maximum_version": "8.2"
}, },
"update_source": { "update_source": {
"source": "nosial/libs.opts@n64", "source": "nosial/libs.opts@n64",
@ -18,9 +18,9 @@
"assembly": { "assembly": {
"name": "OptsLib", "name": "OptsLib",
"package": "net.nosial.optslib", "package": "net.nosial.optslib",
"description": "A simple options parser library for PHP",
"copyright": "Copyright (c) 2022-2023 Nosial", "copyright": "Copyright (c) 2022-2023 Nosial",
"version": "1.1.3", "description": "A simple options parser library for PHP",
"version": "1.1.0",
"uuid": "20aefdfa-7b91-11ed-919f-cb63712c8e36" "uuid": "20aefdfa-7b91-11ed-919f-cb63712c8e36"
}, },
"build": { "build": {
@ -31,14 +31,6 @@
"name": "release", "name": "release",
"build_type": "ncc", "build_type": "ncc",
"output": "build/release/%ASSEMBLY.PACKAGE%.ncc" "output": "build/release/%ASSEMBLY.PACKAGE%.ncc"
},
{
"name": "release-executable",
"build_type": "executable",
"output": "build/release/release_executable_gz",
"options": {
"ncc_configuration": "release"
}
} }
] ]
} }

View file

@ -26,79 +26,77 @@
* *
* @var string * @var string
*/ */
private static string $regex = "/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/"; private static $regex = "/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/";
/** /**
* Cache of the parsed arguments. This is used to prevent the arguments from being parsed more than once. * Cache of the parsed arguments. This is used to prevent the arguments from being parsed more than once.
* *
* @var array|null * @var array
*/ */
private static ?array $args_cache = null; private static $args_cache;
/** /**
* Parses the input arguments into an array of flags and values * Parses the input arguments into an array of flags and values
* *
* @param array|string|null $input array The input arguments * @param string|array $input array The input arguments
* @param int $max_arguments The maximum number of arguments to parse * @param int $max_arguments The maximum number of arguments to parse
* @return array The parsed arguments * @return array The parsed arguments
*/ */
public static function parseArgument(array|string|null $input, int $max_arguments = 1000): array public static function parseArgument($input, int $max_arguments=1000): array
{ {
$flags = '';
if (is_string($input)) if (is_string($input))
{ {
$flags = $input; $flags = $input;
} }
elseif (is_array($input)) elseif(is_array($input))
{ {
$flags = implode(' ', $input); $flags = implode(' ', $input);
} }
else else
{ {
global $argv; global $argv;
if (isset($argv) && count($argv) > 1) if(isset($argv) && count($argv) > 1)
{ {
array_shift($argv); array_shift($argv);
}
$flags = implode(' ', $argv); $flags = implode(' ', $argv);
} }
}
$configs = []; $configs = array();
// Assuming self::$regex is always defined and valid
preg_match_all(self::$regex, $flags, $matches, PREG_SET_ORDER); preg_match_all(self::$regex, $flags, $matches, PREG_SET_ORDER);
foreach ($matches as $index => $match) foreach ($matches as $index => $match)
{ {
$value = true; if(isset($match['value']) && $match['value'] !== '')
if (!empty($match['value']))
{ {
$value = $match['value']; $value = $match['value'];
} }
elseif (!empty($match['string'])) elseif(isset($match['string']) && $match['string'] !== '')
{ {
// fix escaped quotes // fix escaped quotes
$value = str_replace(["\\\"", "\\'"], ["\"", "'"], $match['string']); $value = str_replace(["\\\"", "\\'"], ["\"", "'"], $match['string']);
} }
else
{
$value = true;
}
if (!empty($match['bigflag'])) if(isset($match['bigflag']) && $match['bigflag'] !== '')
{ {
$configs[$match['bigflag']] = $value; $configs[$match['bigflag']] = $value;
} }
if (!empty($match['smallflag'])) if(isset($match['smallflag']) && $match['smallflag'] !== '')
{ {
$configs[$match['smallflag']] = $value; $configs[$match['smallflag']] = $value;
} }
if (!empty($match['unmatched'])) if(isset($match['unmatched']) && $match['unmatched'] !== '')
{ {
$configs[$match['unmatched']] = true; $configs[$match['unmatched']] = true;
} }
if ($index >= $max_arguments) if($index >= $max_arguments)
{ {
break; break;
} }
@ -117,18 +115,21 @@
{ {
global $argv; global $argv;
if($argv === null && isset($_SERVER['argv'])) if($argv === null)
{ {
$argv = $_SERVER['argv']; $argv = $_SERVER['argv'];
} }
if(self::$args_cache === null) if(self::$args_cache === null)
{ {
self::$args_cache = [];
if(isset($argv)) if(isset($argv))
{ {
self::$args_cache = self::parseArgument($argv); self::$args_cache = self::parseArgument($argv);
} }
else
{
self::$args_cache = [];
}
} }
if($after === null) if($after === null)
@ -137,6 +138,7 @@
} }
$after_index = array_search($after, array_keys(self::$args_cache), true); $after_index = array_search($after, array_keys(self::$args_cache), true);
if($after_index === false) if($after_index === false)
{ {
return []; return [];

View file

@ -1,70 +0,0 @@
<?php
namespace OptsLib;
use PHPUnit\Framework\TestCase;
class ParseTest extends TestCase
{
/**
* Testing parseArgument method when input is a string type, also testing different flag pattern
*/
public function testParseArgumentWithStringInput()
{
$input = '--flag1=value1 -f value2 unmatched_string';
$expected_result = [
'flag1' => 'value1',
'f' => 'value2',
'unmatched_string' => true,
];
$this->assertEquals($expected_result, Parse::parseArgument($input));
}
/**
* Testing parseArgument method when input is an array type
*/
public function testParseArgumentWithArrayInput()
{
// Array argument will be transformed into '--flag1 value1 "-"f" value2" unmatched_string'
$input = ['--flag1', 'value1', '-', 'f', 'value2', 'unmatched_string'];
$expected_result = [
'flag1' => 'value1',
'unmatched_string' => true,
'f' => true,
'value2' => true
];
$this->assertEquals($expected_result, Parse::parseArgument($input));
}
/**
* Testing parseArgument method when input contains escaped quotes in a string argument
*/
public function testParseArgumentWithEscapedQuotes()
{
$input = "--flag1=\"value1 with some \\\"escaped quotes\\\"\" -f 'value2 with some \\'escaped quotes\\'' unmatched_string";
$expected_result = [
'flag1' => 'value1 with some "escaped quotes"',
'f' => 'value2 with some \'escaped quotes\'',
'unmatched_string' => true,
];
$this->assertEquals($expected_result, Parse::parseArgument($input));
}
/**
* Testing parseArgument method only takes maximum number of arguments specified
*/
public function testParseArgumentWithMaxArguments()
{
$input = '--flag1=value1 -f value2 unmatched_string1 unmatched_string2';
$expected_result = [
'flag1' => 'value1',
'f' => 'value2',
'unmatched_string1' => true
]; // It should only parse maximum 3 arguments
$this->assertEquals($expected_result, Parse::parseArgument($input, 2));
}
}

10
tests/parse_arguments.php Normal file
View file

@ -0,0 +1,10 @@
<?php
require 'ncc';
import('net.nosial.optslib', 'latest');
$parse = \OptsLib\Parse::getArguments();
var_dump($parse);
$parse = \OptsLib\Parse::getArguments('exec');
var_dump($parse);

7
tests/parse_test.php Normal file
View file

@ -0,0 +1,7 @@
<?php
require 'ncc';
import('net.nosial.optslib', 'latest');
$parse = \OptsLib\Parse::parseArgument('test --foo=bar --baz=qux --quux --corge --grault=garply --waldo --fred --plugh --xyzzy --thud');
var_dump($parse);