PHP Classes

strargs: Parse commands to get parameters of multiple types

Recommend this page to a friend!
  Info   View files Example   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 52 This week: 1All time: 10,610 This week: 560Up
Version License PHP version Categories
strargs 1.0.0MIT/X Consortium ...5PHP 5, Text processing, Console
Description 

Author

This package can parse commands to get parameters of multiple types.

It can take a command string and parses it to extract parameter values that can be strings, as well objects or arrays encoded using JSON.

The package can also compose a command string from the names and the values of the parameters.

Innovation Award
PHP Programming Innovation award nominee
July 2021
Number 11
Command-line scripts that can run from an operating system console of a computer can take a list of parameters as a string passed in the front of the command name.

Usually, this string is processed to extract the parameter names and the values.

This package can extract the values of parameters passed as a string. It can also extract the values of parameters of more complex types like arrays or objects. It can decode parameter values that may be passed using the JSON format.

Manuel Lemos
Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Age: 21
All time rank: 276644 in Turkey Turkey
Week rank: 51 Up2 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 13x

Example

StrArgs

> Advanced CLI Command Parser

<?php

$strargs = new \IsaEken\Strargs\Strargs('command "my name is" "isa eken" --details="{\"age\": 19, \"gender\": \"Male\"}" --friends[]=\"Nur\" -vvv');

$strargs->getArgument(0); // "my name is"
$strargs->getArgument(1); // "isa eken"
$strargs->getOption('details'); // (object) ["age" => 19, "gender" => "Male"]
$strargs->getOption('friends'); // ['Nur']
$strargs->getVerbose(); // "debug"

Installation

You can install using composer.

composer require isaeken/strargs

Usage

Creating Command Using PHP

$strargs = new \IsaEken\Strargs\Strargs;
$strargs->setCommand('/usr/bin/php');
$strargs->setArgument(0, '/var/www/html');
$strargs->setOption('action', 'serve');
$strargs->setOption('post', $_POST);
$strargs->addFlag('local');
$strargs->setVerbose(\IsaEken\Strargs\Enums\VerboseLevel::VERY_VERBOSE);

$strargs->encode(); // /usr/bin/php "/var/www/html" --action="serve" --local -vv

Parse Command From String

$strargs = new \IsaEken\Strargs\Strargs('command arg1 arg2 --json1="{\"key\":\"value\"}" --json2="[\"asd\"]" --arr1[]="item1" --arr1[]="item2" --arr1[]=10 --arr2[]="item" --opt=1 --opt2="value" -a -b -c --ab --tr --try');

// Call this method first.
$strargs->decode(); 

$strargs->getArgument(0); // arg1
$strargs->getArgument(1); // arg2
$strargs->getOption('json1'); // (object) ['key' => 'value']
$strargs->getOption('arr1'); // ['item1', 'item2', 10]
$strargs->getVerbose(); // normal
$strargs->hasFlag('try'); // true
$strargs->hasFlag('c'); // true
$strargs->hasFlag('d'); // false

License

The MIT License (MIT). Please see License File for more information.


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 4 directories)
Files folder imagetests (2 files)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file phpunit.xml Data Auxiliary data
Plain text file README.md Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageContracts (5 files)
Files folder imageEnums (1 file)
Files folder imageExceptions (1 file)
Files folder imageTraits (5 files)
  Plain text file Helpers.php Class Class source
  Plain text file Strargs.php Class Class source

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file HasArguments.php Class Class source
  Plain text file HasCommand.php Class Class source
  Plain text file HasFlags.php Class Class source
  Plain text file HasOptions.php Class Class source
  Plain text file HasVerbose.php Class Class source

  Files folder image Files  /  src  /  Enums  
File Role Description
  Plain text file VerboseLevel.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file NoCommandException.php Class Class source

  Files folder image Files  /  src  /  Traits  
File Role Description
  Plain text file HasArguments.php Class Class source
  Plain text file HasCommand.php Class Class source
  Plain text file HasFlags.php Class Class source
  Plain text file HasOptions.php Class Class source
  Plain text file HasVerbose.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file HelperTest.php Class Class source
  Plain text file StringTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:52
This week:1
All time:10,610
This week:560Up