There are plenty of command line parsers available through npm (yargs, minimist, etc) and they all stem from a very strong POSIX root. But, we wanted something just slightly different. We wanted a parser that was specific for a slack command line structure that we were toying around with. The structure looked like this:
@botname {product-name} {action} <required-param> [optional-param]
With:
- {product-name} being a requirement in the command syntax
- {action} being a requirement in the command syntax
- Parameter order would be respected, if a parameter name was not specified then it’s position should indicate which parameter it is
- Parameter names are optional, but can be indicated by either a prefix of a single (-) or double (--) dash
- The values for named parameters would be indicated by spaces instead of equal signs
- And there’s a few other criteria in there that our brains just naturally figure out but we have to write into the code
Anyways, the point is that the design is just slightly different enough from a standard POSIX command line that we were going to need to build our own.
To do this we tried to figure out a simplified syntax to use in code. This syntax would be used in our hubot commands to parse the given input. Again, there are many parsers already built for this (regex comes to mind), but we built our own. The syntax looks like this:
product-name action {required-param-name} {optional-param:default value}
An example of this syntax would be:
servicepro close {ticketnumber} {memo:Verified and Closing}
If the given input was:
@botname servicepro close 5646831 “Memory usage has been lowered.”
The command line parser would need to return a javascript object that looked like this:
{
“ticketnumber”: “5646831”,
“memo”: “Memory usage has been lowered”,
“success”: true,
“errors”: {}
}
So, here’s the code with some poorly written tests commented out at the bottom:
0 comments:
Post a Comment