PHP Classes

strargs: Parse commands to get parameters of multiple types

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 53 All time: 10,641 This week: 68Up
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: 22
All time rank: 275244 in Turkey Turkey
Week rank: 215 Up5 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 (20)  
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 (20)  /  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 (20)  /  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 (20)  /  src  /  Enums  
File Role Description
  Plain text file VerboseLevel.php Class Class source

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

  Files folder image Files (20)  /  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 (20)  /  tests  
File Role Description
  Plain text file HelperTest.php Class Class source
  Plain text file StringTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:53
This week:0
All time:10,641
This week:68Up