Added simple cmd for php
This commit is contained in:
parent
a02ce657f5
commit
f78f438b16
1 changed files with 83 additions and 0 deletions
83
vendor/phangoapp/cuchuluphputils/src/Cmd.php
vendored
Normal file
83
vendor/phangoapp/cuchuluphputils/src/Cmd.php
vendored
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PhangoApp\CuchuluPHPUtils;
|
||||||
|
|
||||||
|
|
||||||
|
class Cmd {
|
||||||
|
|
||||||
|
static public function getopts($args) {
|
||||||
|
|
||||||
|
//Generate getopt
|
||||||
|
|
||||||
|
// Option ['option' => ['help' => 'help text', 'required' => false, 'only_option' => true ]]]
|
||||||
|
|
||||||
|
//Horrible optimization.
|
||||||
|
$h_options=[];
|
||||||
|
|
||||||
|
$h_options_text=[];
|
||||||
|
|
||||||
|
foreach($args as $k => $arg) {
|
||||||
|
|
||||||
|
$required_value='';
|
||||||
|
|
||||||
|
$only_option=$arg['only_option'] ?? false;
|
||||||
|
|
||||||
|
$required=$arg['required'] ?? false;
|
||||||
|
|
||||||
|
if($only_option) {
|
||||||
|
|
||||||
|
$options[$k]=$k.'::';
|
||||||
|
|
||||||
|
$h_options[]='--'.$k;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if($required) {
|
||||||
|
|
||||||
|
$options[$k]=$k.':';
|
||||||
|
$required_value='[required]';
|
||||||
|
|
||||||
|
$h_options[]='--'.$k.'='.strtoupper($k).$required_value;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$h_options_text[$k]='--'.$k.': '.$arg['help'] ?? ucfirst($k);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$final_args=getopt('', array_values($options));
|
||||||
|
|
||||||
|
$error=false;
|
||||||
|
|
||||||
|
foreach($args as $k => $arg) {
|
||||||
|
|
||||||
|
$only_option=$arg['only_option'] ?? false;
|
||||||
|
|
||||||
|
$required=$arg['required'] ?? false;
|
||||||
|
|
||||||
|
if($required && !isset($final_args[$k])) {
|
||||||
|
|
||||||
|
$error=true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if($error) {
|
||||||
|
/*
|
||||||
|
* usage: delete_wordpress.py [-h] --domain DOMAIN --webapp WEBAPP
|
||||||
|
delete_wordpress.py: error: the following arguments are required: --domain, --webapp
|
||||||
|
*/
|
||||||
|
|
||||||
|
echo "usage: ".basename(__FILE__)." ".implode(' ', $h_options)." \n";
|
||||||
|
|
||||||
|
echo implode("\n", $h_options_text)."\n";
|
||||||
|
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $final_args;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue