2.4.1 • Published 4 years ago

rich-commands v2.4.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

RichCommands

npm version downloads build coverage licence

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" }
}

[More examples]

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.

2.4.1

4 years ago

2.4.0

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago