Created
September 7, 2018 12:57
-
-
Save lastguest/b8efabfd8cda72b7c1c0d49b0113c22f to your computer and use it in GitHub Desktop.
[PHP] Core: CLI
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
<?php | |
class CLI { | |
const | |
BLACK = "\033[0;30m", | |
DARKGRAY = "\033[1;30m", | |
RED = "\033[0;31m", | |
LIGHTRED = "\033[1;31m", | |
GREEN = "\033[0;32m", | |
LIGHTGREEN = "\033[1;32m", | |
BROWN = "\033[0;33m", | |
YELLOW = "\033[1;33m", | |
BLUE = "\033[0;34m", | |
LIGHTBLUE = "\033[1;34m", | |
PURPLE = "\033[0;35m", | |
LIGHTPURPLE = "\033[1;35m", | |
CYAN = "\033[0;36m", | |
LIGHTCYAN = "\033[1;36m", | |
GRAY = "\033[0;37m", | |
WHITE = "\033[1;37m", | |
BOLD = "\033[1m"; | |
protected static $args, | |
$commands; | |
public static function args(){ | |
if (!static::$args){ | |
static::$args = (object)['opts'=>(object)[],'args'=>[],'extra'=>'']; | |
$args = array_slice($GLOBALS['argv'], 1); | |
while (list($argi, $arg_x) = each($args)){ | |
if ($arg_x == '--'){ | |
// Skip parsing args | |
static::$args->extra = implode(" ", array_slice($args, $argi+1)); | |
break; | |
} else if ($arg_x{0} == '-') { | |
// Single or Multiple options | |
$arg_name = substr($arg_x, 1); | |
if ($arg_x{1} == '-') { | |
// Named option | |
$arg_name = substr($arg_name, 1); | |
if (strpos($arg_name, '=') !== false){ | |
if (substr($arg_name, -1,1)=='='){ | |
// Direct assignment, quoted value : --option="foo bar" | |
$arg_name = rtrim($arg_name,'='); | |
$value = current($args); | |
next($args); | |
} else { | |
// Direct assignment : --option=123 | |
list($arg_name, $value) = explode('=', substr($arg_x,2), 2); | |
static::$args->opts->$arg_name = $value; | |
} | |
} else { | |
if (substr($arg_name,0,3)=='no-'){ | |
// Inverted option (FALSE): --no-option | |
$value = false; | |
$arg_name = substr($arg_name, 3); | |
} else { | |
// Simple option (TRUE): --option | |
$value = true; | |
} | |
} | |
static::$args->opts->$arg_name = $value; | |
} else { | |
// Multiple single options : -abCD | |
foreach(str_split($arg_name, 1) as $flag){ | |
static::$args->opts->$flag = true; | |
} | |
} | |
} else { | |
// Simple argument | |
static::$args->args[] = $arg_x; | |
} | |
} | |
} | |
return static::$args; | |
} | |
public static function define($command_name){ | |
return static::$commands[$command_name] = new CLI_Command($command_name); | |
} | |
} | |
class CLI_Command { | |
public $name, | |
$options, | |
$args, | |
$title, | |
$description, | |
$help, | |
$callback; | |
public function __construct($name){ | |
$this->name = $name; | |
} | |
public function option($name, $description=''){ | |
$this->options[$name] = [ | |
'name' => ($optional=substr($name,-1,1)=='?')?substr($name,0,-1):$name, | |
'description' => $description, | |
'required' => !$optional, | |
]; | |
return $this; | |
} | |
public function argument($name, $description=''){ | |
$this->args[$name] = [ | |
'name' => ($optional=substr($name,-1,1)=='?')?substr($name,0,-1):$name, | |
'description' => $description, | |
'required' => !$optional, | |
]; | |
return $this; | |
} | |
public function title($value){ | |
$this->title = $value; | |
return $this; | |
} | |
public function description($value){ | |
$this->description = $value; | |
return $this; | |
} | |
public function help($value){ | |
$this->help = $value; | |
return $this; | |
} | |
public function run($callback){ | |
$this->callback = $callback; | |
return $this; | |
} | |
public function switch($arg, $map){ | |
$caller = $map[CLI::args()->$arg]; | |
return $this; | |
} | |
} | |
/* | |
$x = CLI::define("user") | |
->title("User commands") | |
->description("Tools for users administration.") | |
->argument("name", "The user name") | |
->argument("nickname?", "An optional nickname") | |
->option("promote","Promote user to next level") | |
->option("force","Create user if it exists") | |
->run(function($cli){ | |
$cli->write("Hello"); | |
}); | |
*/ | |
$x = CLI::define("user") | |
->title("User commands") | |
->description("Tools for users administration.") | |
->option("promote","Promote user to next level") | |
->option("force","Create user if it exists") | |
->switch('action',[ | |
'show' => function($args){ | |
echo "Show " . $args['arg'][0]; | |
}, | |
'list' => function($args){ | |
echo "List"; | |
}, | |
'remove' => function($args){ | |
echo "Remove " . $args['arg'][0]; | |
}, | |
]); | |
var_dump($x); | |
/* | |
echo CLI::CYAN; | |
var_dump( | |
CLI::args() | |
); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment