-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simone Pietro Manzi
committed
Oct 25, 2018
1 parent
966877c
commit 5473940
Showing
2 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,19 @@ | ||
# argumentor | ||
simple and easy PHP library for passing arguments and options to a PHP script from command line. | ||
|
||
simple and easy PHP library for passing arguments and options to a PHP command script from command line. | ||
|
||
## Usage | ||
|
||
<?php | ||
|
||
include "./argumentor.php" | ||
|
||
$command = new Argumentor\Command(); | ||
|
||
$command->RegisterArgument("argumentA"); | ||
$command->RegisterOption("optionA", "o"); | ||
|
||
$command->Exec(function(\Argumentor\Arguments $arguments, \Argumentor\Options $options) { | ||
var_dump($arguments->Get("argumentA")); | ||
var_dump($options->Get("optionA")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
namespace Argumentor; | ||
|
||
class Arguments { | ||
|
||
private $arguments = []; | ||
|
||
public function __construct(array $arguments) | ||
{ | ||
|
||
$this->arguments = $arguments; | ||
|
||
return TRUE; | ||
|
||
} | ||
|
||
public function Get(string $name) | ||
{ | ||
|
||
$name = ltrim($name, "-"); | ||
|
||
return isset($this->arguments[$name]) ? $this->arguments[$name] : NULL; | ||
|
||
} | ||
|
||
} | ||
|
||
class Options { | ||
|
||
private $options = []; | ||
|
||
public function __construct(array $options) | ||
{ | ||
|
||
$this->options = $options; | ||
|
||
return TRUE; | ||
|
||
} | ||
|
||
public function Get(string $name) | ||
{ | ||
|
||
$name = ltrim($name, "-"); | ||
|
||
return isset($this->options[$name]) ? $this->options[$name] : NULL; | ||
|
||
} | ||
|
||
} | ||
|
||
class Command { | ||
|
||
protected $arguments = []; | ||
protected $options = []; | ||
protected $shortsmap = []; | ||
|
||
public function RegisterArgument(string $name) | ||
{ | ||
|
||
$name = ltrim($name, "-"); | ||
|
||
$this->arguments[$name] = NULL; | ||
|
||
return TRUE; | ||
|
||
} | ||
|
||
public function RegisterOption(string $name, string $short = "") | ||
{ | ||
|
||
$name = ltrim($name, "-"); | ||
|
||
$short = ltrim($short, "-"); | ||
|
||
$this->options[$name] = NULL; | ||
|
||
if (!empty($short)) $this->shortsmap[$short] = $name; | ||
|
||
return TRUE; | ||
|
||
} | ||
|
||
public function Exec(callable $callback, $funcargs = NULL) | ||
{ | ||
|
||
global $argv; | ||
|
||
for ($i = 1; $i < count($argv); $i++) { | ||
|
||
$arg = $argv[$i]; | ||
|
||
if (is_string($arg)) { | ||
|
||
$argtrim = ltrim($argv[$i], "-"); | ||
|
||
if (strlen($argtrim) > 0) { | ||
|
||
if ($arg[0] == "-") { | ||
|
||
if ($arg[1] !== "-" && strlen($arg) > 2 && $arg[2] !== "=") { | ||
|
||
$flags = str_split($argtrim); | ||
|
||
foreach ($flags as $flag) { | ||
|
||
$name = array_key_exists($flag, $this->shortsmap) ? $this->shortsmap[$flag] : FALSE; | ||
|
||
if ($name && array_key_exists($name, $this->options)) { | ||
|
||
$this->options[$name] = TRUE; | ||
|
||
} | ||
|
||
} | ||
|
||
} else { | ||
|
||
$names = explode("=", $argtrim); | ||
|
||
$name = array_shift($names); | ||
|
||
$name = array_key_exists($name, $this->shortsmap) ? $this->shortsmap[$name] : $name; | ||
|
||
if (array_key_exists($name, $this->options)) { | ||
|
||
if (count($names) > 0) { | ||
|
||
$this->options[$name] = array_pop($names); | ||
|
||
} else if (isset($argv[$i + 1]) && $argv[$i + 1][0] !== "-") { | ||
|
||
$this->options[$name] = $argv[$i + 1]; | ||
|
||
$i++; | ||
|
||
} else { | ||
|
||
$this->options[$name] = TRUE; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} else { | ||
|
||
foreach ($this->arguments as $name => $argument) { | ||
|
||
if ($this->arguments[$name] === NULL) { | ||
|
||
$this->arguments[$name] = $arg; | ||
|
||
break; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
$funcargs = func_get_args(); | ||
|
||
array_shift($funcargs); | ||
|
||
return $callback(new Arguments($this->arguments), new Options($this->options), $funcargs); | ||
|
||
} | ||
|
||
} |