Skip to content

Instantly share code, notes, and snippets.

@loilo
Last active September 27, 2019 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loilo/6a2a86bf3a5fb55c0521e649559028a8 to your computer and use it in GitHub Desktop.
Save loilo/6a2a86bf3a5fb55c0521e649559028a8 to your computer and use it in GitHub Desktop.
Build a command line arguments string from a PHP associative array

PHP arg string generator

Generate a command line arg string from an associative array.

This is purely built for functionality, not for beauty: no grouping of shorthand flags, all args will be escaped and wrapped in single quotes, no matter if needed or not.

Usage

Associative key-value pairs will be treated as command line options while array entries with no/integer keys will be used as regular arguments.

echo 'node ' . generateArgStr([
    'app.js',
    'arg' => 'foo'
]);

// node 'app.js' '--arg' 'foo'

Command line options will be prefixed with two dashes -- for regular options and one dash - for short options (option names with just one character).

echo 'node ' . generateArgStr([
    'e' => 'console.log("foo")'
]);

// node '-e' 'console.log("foo")'

You can change this behaviour by passing the prefix option.

It defaults to this array: [ 'short' => '-', 'long' => '--' ] but you can also just pass a string:

echo 'node ' . generateArgStr([
    'e' => 'console.log("foo")'
], [
    'prefix' => '--'
]);

// node '--e' 'console.log("foo")'

Associative pairs with boolean values will be present as simple flag options (if value is true) or completely be omitted (if value is false). This allows for easy conditional toggling of options.

echo 'node ' . generateArgStr([
    'inspect' => false,
    'app.js',
    'verbose' => true
]);

// node 'app.js' '--verbose'

By default, options and their values will be separated by a space and thus be two technically sepearate arguments. However, some CLI apps need a different kind of option formatting, like mysql — it requires options and their according values as one argument, joined by an "equals" sign =.

For that, there's the glue option:

echo 'mysql ' . generateArgStr([
    'user' => 'root'
], [
    'glue' => '='
]);

// mysql '--user=root'
<?php
function prefixOptionArg (string $option, $prefix) {
if (is_string($prefix)) {
return $prefix . $option;
} elseif (is_array($prefix)) {
if (strlen($option) === 1 && is_string($prefix['short'])) {
return $prefix['short'] . $option;
} elseif (strlen($option) > 1 && is_string($prefix['long'])) {
return $prefix['long'] . $option;
}
}
return $option;
}
function generateArgStr ($args, $config = []) {
$config = array_merge([
'prefix' => [
'short' => '-',
'long' => '--'
],
'glue' => null
], $config);
if (is_string($args)) $args = [ $args ];
$opts = [];
foreach ($args as $option => $value) {
if (is_int($option)) {
$opts[] = $value;
} else {
if (is_bool($value)) {
if ($value === true) {
$opts[] = prefixOptionArg($option, $config['prefix']);
}
} elseif (is_null($config['glue'])) {
$opts[] = prefixOptionArg($option, $config['prefix']);
$opts[] = $value;
} else {
$opts[] = prefixOptionArg($option, $config['prefix']) . $config['glue'] . $value;
}
}
}
return join(' ', array_map('escapeshellarg', $opts));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment