Comparing version 2.0.2 to 2.0.3
@@ -8,3 +8,3 @@ import { ParsedOptions, Program } from './types'; | ||
declare function createArgParser(config: Program): { | ||
run: () => ParsedOptions; | ||
run: () => void; | ||
parse: (args: string[]) => ParsedOptions; | ||
@@ -11,0 +11,0 @@ printError: (error: ParseError) => string; |
@@ -147,2 +147,8 @@ "use strict"; | ||
} | ||
function getCMD(command, path) { | ||
if (path.length === 0) { | ||
return command; | ||
} | ||
return getCMD(command.commandsByKey[path[0]], path.slice(1)); | ||
} | ||
function createArgParser(config) { | ||
@@ -167,3 +173,6 @@ const program = initConfig_1.initConfig(config); | ||
} | ||
return parsedOptions; | ||
const cmd = getCMD(program, parsedOptions.command); | ||
if (cmd.run) { | ||
cmd.run(parsedOptions.options); | ||
} | ||
} | ||
@@ -170,0 +179,0 @@ catch (error) { |
@@ -23,2 +23,3 @@ export interface Argument<V> { | ||
arguments?: Argument<any>[]; | ||
run?: (options: Record<string, Option<any>>) => void; | ||
} | ||
@@ -25,0 +26,0 @@ export interface Program extends Command { |
{ | ||
"name": "cmd-args", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A simple command-line argument parser for NodeJS command-line tools.", | ||
@@ -5,0 +5,0 @@ "main": "dist/cmd-args.js", |
@@ -19,5 +19,5 @@ # cmd-args | ||
```typescript | ||
import { createArgParser } from 'cmd-args' | ||
import { createArgParser } from './cmd-args' | ||
const myParser = createArgParser({ | ||
const myParser = createArgParser({ | ||
cmd: 'my-cmd', | ||
@@ -28,2 +28,5 @@ title: 'My CMD', | ||
version: 'v1.0.0', | ||
run: (options) => { | ||
console.log(options) | ||
}, | ||
options: [ | ||
@@ -34,3 +37,3 @@ { | ||
alias: 'v', | ||
description: 'Enable verbose mode.' | ||
description: 'Enable verbose mode.', | ||
}, | ||
@@ -41,4 +44,5 @@ { | ||
alias: 'o', | ||
description: 'Specifies location to write the output file. If not set the output will go to stdout.' | ||
} | ||
description: | ||
'Specifies location to write the output file. If not set the output will go to stdout.', | ||
}, | ||
], | ||
@@ -48,12 +52,10 @@ arguments: [ | ||
key: 'input-files', | ||
description: 'List of input files to be used.' | ||
description: 'List of input files to be used.', | ||
multi: true, | ||
required: true | ||
} | ||
] | ||
}); | ||
required: true, | ||
}, | ||
], | ||
}) | ||
var program = myParser.run(); | ||
console.log(JSON.stringify(program), null, 2)) | ||
myParser.run() | ||
``` | ||
@@ -60,0 +62,0 @@ |
@@ -221,2 +221,16 @@ // tslint:disable: no-console | ||
function getCMD( | ||
command: CommandInternal, | ||
path: string[] | ||
): CommandInternal { | ||
if (path.length === 0) { | ||
return command | ||
} | ||
return getCMD( | ||
command.commandsByKey[path[0]], | ||
path.slice(1) | ||
) | ||
} | ||
function createArgParser(config: Program) { | ||
@@ -245,3 +259,7 @@ const program = initConfig(config) as ProgramInternal | ||
return parsedOptions | ||
const cmd = getCMD(program, parsedOptions.command) | ||
if (cmd.run) { | ||
cmd.run(parsedOptions.options) | ||
} | ||
} catch (error) { | ||
@@ -248,0 +266,0 @@ if (error instanceof ParseError) { |
@@ -25,2 +25,3 @@ export interface Argument<V> { | ||
arguments?: Argument<any>[] | ||
run?: (options: Record<string, Option<any>>) => void | ||
} | ||
@@ -27,0 +28,0 @@ |
Sorry, the diff of this file is not supported yet
67566
1421
173