Create executable strings using a fluent API.

Overview

command-builder

A PHP class to build executable with using fluent API.

Summary

About

I need to have a fluent way to call my executable. I did not found any other command builder providing such interface that was updated recently or provide a large test coverage.

Features

  • Use a fluent API to construct the string to be executed
  • Class-based
  • Support arguments, long/short options and flags
  • Preserves order of elements
  • Does not handle executing the command

Installation

Install the package using Composer:

composer require khalyomede/command-builder

Examples

1. Create a simple command

In this example, we will just pass a command name, without arguments/options/flags.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

echo $command; // composer

2. Add an argument

In this example, we will add an argument to our command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require");

echo $command; // composer require

3. Add a flag

In this example, we will add a "long" flag to the command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->longFlag("ignore-platform-reqs");

echo $command; // composer require --ignore-platform-reqs

And this is how to add a "short" flag.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->flag("i");

echo $command; // composer require -i

4. Add an option

You can add options to your command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->longOption("prefer-install", "source");

echo $command; // composer require --prefer-install=source

If your option contains spaces, it will automatically be escaped using double quotes.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->longOption("prefer-install", "auto source");

echo $command; // composer require --prefer-install="auto source"

And if your option contains spaces and doubles quotes, they will also be escaped.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->longOption("prefer-install", 'auto "source"');

echo $command; // composer require --prefer-install="auto \"source\""

You can also use "short" option.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("require")
  ->option("p", 'source');

echo $command; // composer require -p=source

5. Configure the standard

You can specify the standard used for the option. By default, it is set to "GNU".

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer", "POSIX");

$command->argument("require")
  ->option("p", 'source');

echo $command; // composer require -p source

$command = new Command("composer")

$command->argument("require")
  ->option("p", 'source');

echo $command; // composer require -p=source

You can also use constants if you prefer

use Khalyomede\CommandBuilder\Command;
use Khalyomede\CommandBuilder\Standard;

$command = new Command("composer", Standard::POSIX);

6. Get the number of arguments

You can know how many arguments were added to the command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->argument("create-project")
  ->argument("laravel/laravel");

echo $command->argumentCount(); // 2

7. Get the number of flags

You can know the number of flags added to your command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->flag("i")
  ->longFlag("prefer-dist");

echo $command->flagCount(); // 2

8. Get the number of options

You can know the number of options added to your command.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->option("a", "source")
  ->longFlag("apcu-autoloader-prefix", "app");

echo $command->optionCount(); // 2

9. Check if a flag has been added

You can know if a flag has been added already.

use Khalyomede\CommandBuilder\Command;

$command = new Command("composer");

$command->longFlag("dev");

var_dump( $command->hasFlag("d", "dev") ); // bool(true)
var_dump( $command->hasFlag("o", "optimize-autoloader") ); // bool(false)

10. Check if an option has been added

You can know if an option has been added or not.

use Khalyomede\CommandBuilder\Command;
use Khalyomede\CommandBuilder\Style;

$command = new Command("composer");

$command->longOption("prefer-install", "source");

var_dump( $command->hasOption("p", "prefer-install") ); // bool(true)
var_dump( $command->hasOption("i", "ignore-platform-req") ); // bool(false)

Compatibility table

This is the compatibility for this version only. To check the compatibility with other version of this package, please browse the version of your choice.

PHP Version Compatibility
8.1.* ✔️
8.0.* ✔️
7.4.* ✔️

Tests

composer run test
composer run mutate
composer run analyse
composer run lint
composer run install-security-checker
composer run check-security
composer run check-updates
You might also like...
Zend\Text is a component to work on text strings from Zend Framework

zend-text Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-text. Zend\Text is a component to work on text strings. It cont

PHP class for parsing user agent strings (HTTP_USER_AGENT).

PHP class for parsing user agent strings (HTTP_USER_AGENT). Includes mobile checks, bots and banned bots checks, browser types/versions and more. Based on browscap (via phpbrowscap), Mobile_Detect and ua-parser. Created for high traffic websites and fast batch processing.

Fluent regular expressions in PHP

FLUX (Fluent Regex) 0.5.2 by Selvin Ortiz Description Fluent Regular Expressions in PHP inspired by and largely based on VerbalExpressions:JS by Jesse

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.

Introduction Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services. It handles almost all of the boilerpl

Enable method chaining or fluent expressions for any value and method.

PHP Pipe Operator A (hopefully) temporary solution to implement the pipe operator in PHP. Table of contents Requirements How to install How to use The

The package provides an expressive "fluent" way to define model attributes.

The package provides an expressive "fluent" way to define model attributes. It automatically builds casts at the runtime and adds a native autocompletion to the models' properties.

A simple, standalone, modern PHP class inspector and mapper library, wrapping PHPs native reflection in a fluent interface

A simple, standalone, modern PHP class inspector and mapper library, wrapping PHPs native reflection in a fluent interface.

A fluent interface for interacting with Netopia's services.
A fluent interface for interacting with Netopia's services.

laravel-netopia A fluent interface for interacting with Netopia's services. Info Database It'll create a table named netopia_payments with the followi

Comments
  • Use built-in PHP escape command function

    Use built-in PHP escape command function

    Versions

    • PHP: 8.0
    • Package: 0.2.1

    Description

    One folks at dev.to suggested to use shellescapeargs built-in function to escape arguments/option values instead of doing it manually.

    Reference: https://dev.to/suckup_de/comment/1le40

    Proposal

    Use the built-in function instead of replacing characters to simplify escaping arguments and option values.

    opened by khalyomede 0
Replace, concat strings or change number fields permanently using Grid Options

It's Pimcore Bundle to replace ,concat strings or change number fields permanently using Grid Options. It will save replaced strings directly in object.

LemonMind.com 5 Aug 31, 2022
This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube features.

Laravel Youtube Client This package provides a simple and intuitive way to work on the Youtube Data API. It provides fluent interface to Youtube featu

Tilson Mateus 6 May 31, 2023
Decimal handling as value object instead of plain strings.

Decimal Object Decimal value object for PHP. Background When working with monetary values, normal data types like int or float are not suitable for ex

Spryker 16 Oct 24, 2022
A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc). Based on the difflib implementation in Python

PHP Diff Class Introduction A comprehensive library for generating differences between two hashable objects (strings or arrays). Generated differences

Chris Boulton 708 Dec 25, 2022
Shortest Path - have a function ShortestPath (strArr) take strArr which will be an array of strings which models a non-looping Graph.

Have the function ShortestPath(strArr) take strArr which will be an array of strings which models a non-looping Graph

null 1 Feb 5, 2022
Laminas\Text is a component to work on text strings

laminas-text This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Steering

Laminas Project 38 Dec 31, 2022
ICSGenerator - The module can generate basic ICS calendar strings and files

ICSGenerator The module can generate basic ICS calendar strings and files. The module simply extends WireData. It has these properties and default val

Timo Hausmann 4 Jun 25, 2022
PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule for validation Japanese string only full-width or only half-width.

Japanese String Helpers PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule f

Deha 54 Mar 22, 2022
PHP Class Encoding featuring popular Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strings.

forceutf8 PHP Class Encoding featuring popular \ForceUTF8\Encoding::toUTF8() function --formerly known as forceUTF8()-- that fixes mixed encoded strin

Sebastián Grignoli 1.6k Dec 22, 2022
Parse DSN strings into value objects to make them easier to use, pass around and manipulate

DSN parser Parse DSN strings into value objects to make them easier to use, pass around and manipulate. Install Via Composer composer require nyholm/d

Tobias Nyholm 77 Dec 13, 2022