Not sure how it handles multiple spaces between arguments.
Code: Select all
| ParseArgs.inc
|-------------------------------------------------------------------------------
| Name:
| Arguments Parser include.
|-----
|
| Current Version:
| 1.0.0
|-----
|
| Author:
| brianMan <EvenLessSpam@Yahoo.com>
|-----
|
| Requirements:
| None; this is a stand-alone include.
|-----
|
| Description:
| This include is meant for those that have/create macros that take run-time
| arguments/parameters to handle settings. An example could be simple
| debugging, or enabling/disabling statistics at run-time.
|
| The arguments that is passed to the macro MUST be declared in the macro
| before calling the ParseArgs sub-routine, otherwise they simply just won't
| be used.
|-----
|
| Example:
| /macro HelloWorld "stats=0 debug=1"
|
| #include ParseArgs.inc
| Sub Main
| /if (!${Defined[Param0]}) /return
| /declare stats bool outer true
| /declare debug bool outer false
|
| /call ParseArgs ${Param0}
| /echo stats set to ${stats}
| /echo debug set to ${debug}
| /return
|-----
|
| Changelist:
| 1.0.0
| Initial release.
|-----
|
| Todo:
| Pass strings that contain spaces as arguments. Currently not possible
| because spaces are being used as the delimiter.
|-------------------------------------------------------------------------------
Sub ParseArgs
/if (!${Defined[Param0]}) {
/echo Syntax: /call ParseArgs "space seperated string of arguments"
/return
}
/declare strArgs string local
/declare intArgs int local
/declare intArg int local
/declare strKey string local
/declare strValue string local
/varset strArgs ${Param0}
/varcalc intArgs ${strArgs.Count[ ]}+1
/for intArg 1 to ${intArgs}
/if ${intArgs}>1 /varset strArgs ${strArgs.Arg[${intArg}, ]}
/varset strKey ${strArgs.Left[$strArgs.Find[=]]}
/varset strValue ${strArgs.Right[${Math.Calc[${strArgs.Length}-${strArgs.Find[=]}-1]}]}
/if (${Defined[${strKey}]}) {
/varset ${strKey} ${strValue}
} else {
/if (${Defined[Param1]}) {
/echo Args::Debug : ${strKey} is not defined
}
}
/next intArg
/return