MCPcopy
hub / github.com/cacjs/cac / parse

Method parse

src/cac.ts:174–261  ·  view source on GitHub ↗

* Parse argv

(
    argv?: string[],
    {
      run = true,
    }: {
      /** Whether to run the action for matched command */
      run?: boolean | undefined
    } = {},
  )

Source from the content-addressed store, hash-verified

172 * Parse argv
173 */
174 parse(
175 argv?: string[],
176 {
177 run = true,
178 }: {
179 /** Whether to run the action for matched command */
180 run?: boolean | undefined
181 } = {},
182 ): ParsedArgv {
183 if (!argv) {
184 if (!runtimeProcessArgs) {
185 throw new Error(
186 'No argv provided and runtime process argv is not available.',
187 )
188 }
189 argv = runtimeProcessArgs
190 }
191
192 this.rawArgs = argv
193 if (!this.name) {
194 this.name = argv[1] ? getFileName(argv[1]) : 'cli'
195 }
196
197 let shouldParse = true
198
199 // Search sub-commands
200 for (const command of this.commands) {
201 const parsed = this.mri(argv.slice(2), command)
202
203 const commandName = parsed.args[0]
204 if (command.isMatched(commandName)) {
205 shouldParse = false
206 const parsedInfo = {
207 ...parsed,
208 args: parsed.args.slice(1),
209 }
210 this.setParsedInfo(parsedInfo, command, commandName)
211 this.dispatchEvent(
212 new CustomEvent(`command:${commandName}`, { detail: command }),
213 )
214 }
215 }
216
217 if (shouldParse) {
218 // Search the default command
219 for (const command of this.commands) {
220 if (command.isDefaultCommand) {
221 shouldParse = false
222 const parsed = this.mri(argv.slice(2), command)
223 this.setParsedInfo(parsed, command)
224 this.dispatchEvent(new CustomEvent('command:!', { detail: command }))
225 }
226 }
227 }
228
229 if (shouldParse) {
230 const parsed = this.mri(argv.slice(2))
231 this.setParsedInfo(parsed)

Callers 14

index.test.tsFile · 0.80
negated-option.tsFile · 0.80
basic-usage.tsFile · 0.80
help.tsFile · 0.80
sub-command.tsFile · 0.80
deno.tsFile · 0.80
default-command.tsFile · 0.80

Calls 8

mriMethod · 0.95
setParsedInfoMethod · 0.95
outputHelpMethod · 0.95
unsetMatchedCommandMethod · 0.95
outputVersionMethod · 0.95
runMatchedCommandMethod · 0.95
getFileNameFunction · 0.90
isMatchedMethod · 0.80

Tested by

no test coverage detected