A command parser
npm install rich-commands![npm version][npm]
![downloads][npm]
![build][ci]
![coverage][coveralls]
![licence][licence]
RichCommands is a simple, feature-rich and error-free command/argument parser. [[Documentation][docs]]
[npm]: https://www.npmjs.com/package/rich-commands
[docs]: https://4erem6a.github.io/RichCommands
[coveralls]: https://coveralls.io/github/4erem6a/RichCommands
[ci]: https://travis-ci.org/4erem6a/RichCommands
[licence]: https://github.com/4erem6a/RichCommands/blob/master/LICENSE
- Simple API
- Fully configurable syntax
- Regex support
- Quoted arguments
- Rest arguments
- Escape markers
- Empty arguments (Argument skipping)
- Flags with optional values
- Array flag values
``js
const { parseCommand } = require("rich-commands");
const rawCommand = "npm i -D typescript";
const command = parseCommand(rawCommand);
console.log(command);
`
Expected result:
`js`
{
name: "npm",
args: ["i", "typescript"],
flags: { D: true }
}
`js
const { parseArgs } = require("rich-commands");
const rawArgs = '1 "2 3" -f = x';
const argv = parseArgs(rawArgs);
console.log(argv);
`
Expected result:
`js`
{
args: ["1", "2 3"],
flags: { f: "x" }
}
`
command -> string commandPart*
commandPart -> argument | flag
argument -> string | empty
flag ->
string -> rest | quoted | simple
rest ->
quoted ->
simple -> (
empty ->
`
`js`
{
quotes: ['"', ["(", ")"]],
flagMarkers: ["--", "-"],
flagValueMarkers: ["="],
emptyArgMarkers: ["~"],
escapeMarkers: ["\\"],
separators: [" ", "\n", "\r", "\t"],
restMarkers: ["::"]
}
> Since v2.4.0 you can use regular expressions in the parser options.
`js`
{
quotes: ['"', ["(", ")"]],
flagMarkers: [/--?/],
flagValueMarkers: ["="],
emptyArgMarkers: ["~"],
escapeMarkers: ["\\"],
separators: [/\s+/],
restMarkers: ["::"]
}
> ^ and $` regex anchors might not work as you expect due to parsing implementation, you should avoid using them.