MCPcopy Create free account
hub / github.com/yargs/yargs / parseCommand

Function parseCommand

lib/parse-command.ts:3–33  ·  view source on GitHub ↗
(cmd: string)

Source from the content-addressed store, hash-verified

1import {NotEmptyArray} from './typings/common-types.js';
2
3export function parseCommand(cmd: string) {
4 const extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ');
5 const splitCommand = extraSpacesStrippedCommand.split(/\s+(?![^[]*]|[^<]*>)/);
6 const bregex = /\.*[\][<>]/g;
7
8 const firstCommand = splitCommand.shift();
9 if (!firstCommand) throw new Error(`No command found in: ${cmd}`);
10
11 const parsedCommand: ParsedCommand = {
12 cmd: firstCommand.replace(bregex, ''),
13 demanded: [],
14 optional: [],
15 };
16 splitCommand.forEach((cmd, i) => {
17 let variadic = false;
18 cmd = cmd.replace(/\s/g, '');
19 if (/\.+[\]>]/.test(cmd) && i === splitCommand.length - 1) variadic = true;
20 if (/^\[/.test(cmd)) {
21 parsedCommand.optional.push({
22 cmd: cmd.replace(bregex, '').split('|') as NotEmptyArray<string>,
23 variadic,
24 });
25 } else {
26 parsedCommand.demanded.push({
27 cmd: cmd.replace(bregex, '').split('|') as NotEmptyArray<string>,
28 variadic,
29 });
30 }
31 });
32 return parsedCommand;
33}
34
35export interface ParsedCommand {
36 cmd: string;

Callers 5

parseArgsFunction · 0.85
addHandlerMethod · 0.85
cmdToParseOptionsMethod · 0.85
commandCompletionsMethod · 0.85
parse-command.mjsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…