SideCarCLI-send arguments to interceptors
Summary links SideCarCLI
( Description : SideCar for CLI applications. Interceptors for Line, Finish, Timer . See Code )For the SideCarCLI I want the interceptors ( line, finish , timer) to receive parts of the command line of the original application. But how to parse the command line and how to pass to the interceptors ? RegEx to the rescue!
I will let the user define the command line of the starting application and parse the command line with his own regex.
For example , if he put as arguments
x=y z=t
a possible regex will be
(?<FirstArg>\w+)=((\w+)) (?<LastArg>\w+)=((\w+))
Also, if the starting application is ping.exe and the command line is
-n 20 \”www.yahoo.com\”
then a possible regex to have the site is
(?<FirstArg>.+) (?<site>.+)
Then , in the interceptors , I will put
“LineInterceptors”: [
{
“Name”: “WindowsStandardWindowsOutputInterceptor”,
“Arguments”: “/c echo \”{site} {line}\””,
“FullPath”: “cmd.exe”,
“FolderToExecute”: null,
“InterceptOutput”: true
}
and will replace the
“Arguments”: “/c echo \”{site} {line}\””,
with the
site
parsed from regex.
The code is pretty simple . As inputs, we have this.regEx for the regex and this.Arguments for the command line
var regex = new Regex(this.regEx);
var names = regex.
GetGroupNames().
Where(it => !int.TryParse(it, out var _)).
ToArray();var matches = regex.Matches(this.Arguments);
if (matches.Count == 0)
throw new ArgumentException($” the regex {regEx} has no matches for {Arguments}”);var m = matches.FirstOrDefault();
foreach(var g in names)
{
if (m.Groups[g].Success)
{
argsRegex[g]=m.Groups[g].Value;
}
else
{
throw new ArgumentException($”cannot find {g} in regex {regEx} when parsing {Arguments}”);
}
}
That’s all! You can find code at https://github.com/ignatandrei/SideCarCLI
.
Leave a Reply