Command-line arguments parsing library
  • PHP 94.2%
  • Makefile 5.8%
Find a file
netkas a3ca8ab316
Some checks failed
CI / test (push) Has been skipped
CI / release-artifacts (push) Has been skipped
CI / generate-phpdoc (push) Has been skipped
CI / release (push) Failing after 1s
CI / debug (push) Failing after 1s
CI / check-phpunit (push) Failing after 1s
CI / check-phpdoc (push) Failing after 1s
CI / release-documentation (push) Has been skipped
fix: update installation commands in README for consistency with NCC syntax
2026-01-05 12:40:03 -05:00
.forgejo/workflows feat: add CI workflow for building, testing, and releasing PHP project 2026-01-05 12:30:02 -05:00
.github/workflows fix: ensure reports directory is created with proper permissions before running PHPUnit tests 2026-01-05 12:21:06 -05:00
.idea feat: update build system to use target directory and add CI configuration 2026-01-04 18:49:27 -05:00
assets Added Logo 2023-01-27 00:36:38 -05:00
src/OptsLib 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. 2024-10-29 15:06:21 -04:00
tests fix: update import path for NCC build output in bootstrap 2026-01-04 21:21:35 -05:00
.gitignore feat: update build system to use target directory and add CI configuration 2026-01-04 18:49:27 -05:00
CHANGELOG.md chore: update version to 1.1.4 and add changelog entry for OptsLib compatibility with ncc 3.+ 2026-01-05 12:36:33 -05:00
LICENSE Refactor build process, update project configs, and clean up documentation 2023-10-10 21:29:13 -04:00
Makefile feat: update build system to use target directory and add CI configuration 2026-01-04 18:49:27 -05:00
phpdoc.dist.xml feat: update build system to use target directory and add CI configuration 2026-01-04 18:49:27 -05:00
phpunit.xml feat: update build system to use target directory and add CI configuration 2026-01-04 18:49:27 -05:00
project.yml chore: update version to 1.1.4 and add changelog entry for OptsLib compatibility with ncc 3.+ 2026-01-05 12:36:33 -05:00
README.md fix: update installation commands in README for consistency with NCC syntax 2026-01-05 12:40:03 -05:00

OptsLib

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

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. We do encourage community support and discussions, please be respectful and follow the rules of the community.

Table of Contents

Installation

The library can be installed using ncc:

# n64
ncc install --package="nosial/optslib=latest@n64"

# github
ncc install --package="nosial/optslib=latest@github"

or by adding the following to your project.json file under the build.dependencies section:

dependencies:
    net.nosial.optslib: nosial/optslib=latest@n64

If you don't have the n64 source configured you can add it by running the following command:

ncc repository add --name n64 --type gitlab --host git.n64.cc

Compiling from source

The library can be compiled from source using ncc:

ncc build --config release

or by running the following command:

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.

phpunit

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

    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

    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"

Optionally, if you want arguments after a specific argument/option to be parsed, you can pass the argument name as the first argument.

This is ideal if you are using a command line tool that has a sub-command, for example:

$ mytool subcommand --foo --bar="test" -y --username test

In this case, you can pass the subcommand argument to the getArguments() method to parse the arguments after it.

<?php

    require_once('ncc');
    import('net.nosial.optslib', 'latest');
    
    $arguments = \OptsLib\Parse::getArguments('subcommand');
    
    echo $arguments['foo']; // true
    echo $arguments['bar']; // "test"
    echo $arguments['y']; // true
    echo $arguments['username']; // "test"

Additional functionality

OptsLib also provides some additional functionality that can be used to modify the way arguments are parsed.

getRegex()

This method is used to return the regex pattern used to parse the arguments, it can be used to parse arguments manually.

The default used is

/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/

setRegex()

This method is used to set the regex pattern used to parse the arguments, you can modify the default pattern to suit your needs.

<?php

    require_once('ncc');
    import('net.nosial.optslib', 'latest');
    
    \OptsLib\Parse::setRegex('/(?(?=-)-(?(?=-)-(?'bigflag'[^\\s=]+)|(?'smallflag'\\S))(?:\\s*=\\s*|\\s+)(?(?!-)(?(?=[\\\"\\'])((?<![\\\\])['\"])(?'string'(?:.(?!(?<![\\\\])\\3))*.?)\\3|(?'value'\\S+)))(?:\\s+)?|(?'unmatched'\\S+))/');

Changelog

For a list of changes, see the CHANGELOG.md file.

License

This library is licensed under the MIT license, see the LICENSE file for more information.

The logo was created by Boxicons and is licensed under the MIT license

Contributing

If you want to contribute to this project, you can do so by creating a merge request on the GitLab repository.