Last active
August 28, 2024 21:14
-
-
Save jesobreira/4e8b5e4a1fa2b32676de to your computer and use it in GitHub Desktop.
CmdLine arguments parser (get values/existence/flags)
This file contains hidden or 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 | |
/* | |
Based on CmdLine UDF for AutoIt (also mine): https://www.autoitscript.com/forum/topic/169610-cmdline-udf-get-valueexistenceflag/ | |
*/ | |
class cmdline { | |
function get($sKey, $mDefault = Null) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if($argv[$i]=="/".$sKey OR $argv[$i]=="-".$sKey OR $argv[$i]=="--".$sKey) { | |
if($argc>=$i+1) { | |
return $argv[$i+1]; | |
} | |
} | |
} | |
return $mDefault; | |
} | |
function keyexists($sKey) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if($argv[$i]=="/".$sKey OR $argv[$i]=="-".$sKey OR $argv[$i]=="--".$sKey) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function valueexists($sValue) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if($argv[$i]==$sValue) return true; | |
} | |
return false; | |
} | |
function flagenabled($sKey) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if(preg_match("/\+([a-zA-Z]*)".$sKey."([a-zA-Z]*)/", $argv[$i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function flagdisabled($sKey) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if(preg_match("/\-([a-zA-Z]*)".$sKey."([a-zA-Z]*)/", $argv[$i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function flagexists($sKey) { | |
global $argv,$argc; | |
for($i = 1; $i <= ($argc-1); $i++) { | |
if(preg_match("/(\+|\-)([a-zA-Z]*)".$sKey."([a-zA-Z]*)/", $argv[$i])) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function getvalbyindex($iIndex, $mDefault = null) { | |
global $argv,$argc; | |
if(($argc-1)>=$iIndex) { | |
return $argv[$iIndex]; | |
} else { | |
return $mDefault; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment