RichCommands
RichCommands is a simple, feature-rich and error-free command/argument parser. [Documentation]
Features
- Simple API
- Fully configurable syntax
- Regex support
- Quoted arguments
- Rest arguments
- Escape markers
- Empty arguments (Argument skipping)
- Flags with optional values
- Array flag values
Example
Command parsing
const { parseCommand } = require("rich-commands");
const rawCommand = "npm i -D typescript";
const command = parseCommand(rawCommand);
console.log(command);
Expected result:
{
name: "npm",
args: ["i", "typescript"],
flags: { D: true }
}
Argument parsing
const { parseArgs } = require("rich-commands");
const rawArgs = '1 "2 3" -f = x';
const argv = parseArgs(rawArgs);
console.log(argv);
Expected result:
{
args: ["1", "2 3"],
flags: { f: "x" }
}
Grammar
command -> string commandPart*
commandPart -> argument | flag
argument -> string | empty
flag -> <FlagMarker> string [ <FlagValueMarker> argument ]
string -> rest | quoted | simple
rest -> <RestMarker> <any>*
quoted -> <OpeningQuote> (<any> - <ClosingQuote>)* <ClosingQuote>
simple -> (<any> - <Separators> - <OpeningQuotes>)+
empty -> <EmptyArgMarker>
Default syntax
{
quotes: ['"', ["(", ")"]],
flagMarkers: ["--", "-"],
flagValueMarkers: ["="],
emptyArgMarkers: ["~"],
escapeMarkers: ["\\"],
separators: [" ", "\n", "\r", "\t"],
restMarkers: ["::"]
}
Since v2.4.0 you can use regular expressions in the parser options.
{
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.