ts-command-line-args
Advanced tools
Comparing version 2.1.4 to 2.2.0
@@ -180,3 +180,10 @@ export declare type ArgumentConfig<T extends { | ||
prependParamOptionsToDescription?: boolean; | ||
/** | ||
* sets the exit code of the process when exiting early due to missing args or showing usage guide | ||
* 0 will be used for an exit code if this is not specified. | ||
*/ | ||
processExitCode?: number | ProcessExitCodeFunction<T>; | ||
} | ||
export declare type ProcessExitCodeFunction<T> = (reason: ExitReason, passedArgs: Partial<T>, missingArgs: CommandLineOption<any>[]) => number; | ||
export declare type ExitReason = 'missingArgs' | 'usageGuide'; | ||
export interface PartialParseOptions extends ArgsParseOptions<any> { | ||
@@ -183,0 +190,0 @@ /** |
@@ -84,3 +84,3 @@ "use strict"; | ||
if (exitProcess) { | ||
return process.exit(); | ||
return process.exit(resolveExitCode(options, 'usageGuide', parsedArgs, missingArgs)); | ||
} | ||
@@ -106,3 +106,3 @@ } | ||
if (missingArgs.length > 0 && exitProcess) { | ||
process.exit(); | ||
process.exit(resolveExitCode(options, 'missingArgs', parsedArgs, missingArgs)); | ||
} | ||
@@ -117,2 +117,12 @@ else { | ||
exports.parse = parse; | ||
function resolveExitCode(options, reason, passedArgs, missingArgs) { | ||
switch (typeof options.processExitCode) { | ||
case 'number': | ||
return options.processExitCode; | ||
case 'function': | ||
return options.processExitCode(reason, passedArgs, missingArgs); | ||
default: | ||
return 0; | ||
} | ||
} | ||
function printHelpGuide(options, optionList, logger, additionalHeaderSections) { | ||
@@ -119,0 +129,0 @@ var _a, _b; |
{ | ||
"name": "ts-command-line-args", | ||
"version": "2.1.4", | ||
"version": "2.2.0", | ||
"description": "A Typescript wrapper around command-line-args with additional support for markdown usage guide generation", | ||
@@ -64,4 +64,5 @@ "bin": { | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"ansi-regex": "^5.0.1", | ||
"codecov": "^3.8.3", | ||
"concurrently": "^5.3.0", | ||
"concurrently": "^6.3.0", | ||
"eslint": "^7.7.0", | ||
@@ -75,3 +76,5 @@ "eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"hosted-git-info": "^2.8.9", | ||
"jest": "^26.4.2", | ||
"lodash": "^4.17.21", | ||
"prettier": "^2.1.1", | ||
@@ -78,0 +81,0 @@ "rimraf": "^3.0.2", |
@@ -206,4 +206,17 @@ export type ArgumentConfig<T extends { [name: string]: any }> = { | ||
prependParamOptionsToDescription?: boolean; | ||
/** | ||
* sets the exit code of the process when exiting early due to missing args or showing usage guide | ||
* 0 will be used for an exit code if this is not specified. | ||
*/ | ||
processExitCode?: number | ProcessExitCodeFunction<T>; | ||
} | ||
export type ProcessExitCodeFunction<T> = ( | ||
reason: ExitReason, | ||
passedArgs: Partial<T>, | ||
missingArgs: CommandLineOption<any>[], | ||
) => number; | ||
export type ExitReason = 'missingArgs' | 'usageGuide'; | ||
export interface PartialParseOptions extends ArgsParseOptions<any> { | ||
@@ -210,0 +223,0 @@ /** |
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { ArgumentConfig } from './contracts'; | ||
import { ArgumentConfig, ProcessExitCodeFunction } from './contracts'; | ||
import { | ||
@@ -17,2 +17,3 @@ IMocked, | ||
import * as helpersImport from './helpers'; | ||
import { ParseOptions } from '.'; | ||
@@ -396,2 +397,5 @@ jest.mock('fs', () => require('@morgan-stanley/ts-mocking-bird').proxyJestModule(require.resolve('fs'))); | ||
it(`should print errors and exit process when required arguments are missing and help arg is present`, () => { | ||
const mockExitFunction = Mock.create<{ processExitCode: ProcessExitCodeFunction<any> }>().setup( | ||
setupFunction('processExitCode', () => 5), | ||
); | ||
const result = parse(getHelpConfig(), { | ||
@@ -401,2 +405,3 @@ logger: mockConsole.mock, | ||
helpArg: 'optionalHelpArg', | ||
processExitCode: mockExitFunction.mock.processExitCode, | ||
}); | ||
@@ -419,5 +424,16 @@ | ||
expect(mockConsole.withFunction('log').withParameters(`To view the help guide pass '-h'`)).wasCalledOnce(); | ||
expect( | ||
mockExitFunction | ||
.withFunction('processExitCode') | ||
.withParametersEqualTo( | ||
'missingArgs', | ||
{ defaultedString: 'defaultedStringValue', requiredBoolean: false }, | ||
[ | ||
{ name: 'requiredString', type: String }, | ||
{ name: 'requiredArray', type: String, alias: 'o', multiple: true }, | ||
] as any, | ||
), | ||
).wasCalledOnce(); | ||
expect(mockProcess.withFunction('exit').withParameters(5)).wasCalledOnce(); | ||
expect(mockProcess.withFunction('exit')).wasCalledOnce(); | ||
expect(result).toBeUndefined(); | ||
@@ -452,3 +468,3 @@ }); | ||
expect(mockProcess.withFunction('exit')).wasCalledOnce(); | ||
expect(mockProcess.withFunction('exit').withParameters()).wasCalledOnce(); | ||
@@ -492,2 +508,5 @@ expect(result).toBeUndefined(); | ||
it(`and exit when help arg is passed`, () => { | ||
const mockExitFunction = Mock.create<{ processExitCode: ProcessExitCodeFunction<any> }>().setup( | ||
setupFunction('processExitCode', () => 5), | ||
); | ||
const result = parse(getAllOptionalHelpConfig(), { | ||
@@ -509,2 +528,3 @@ logger: mockConsole.mock, | ||
], | ||
processExitCode: mockExitFunction.mock.processExitCode, | ||
}); | ||
@@ -560,3 +580,8 @@ | ||
expect(result).toBeUndefined(); | ||
expect(mockProcess.withFunction('exit')).wasCalledOnce(); | ||
expect( | ||
mockExitFunction | ||
.withFunction('processExitCode') | ||
.withParametersEqualTo('usageGuide', { optionalHelpArg: true }, []), | ||
).wasCalledOnce(); | ||
expect(mockProcess.withFunction('exit').withParameters(5)).wasCalledOnce(); | ||
expect(mockConsole.withFunction('error')).wasNotCalled(); | ||
@@ -563,0 +588,0 @@ expect(mockConsole.withFunction('log').withParameters(verifyHelpContent)).wasCalledOnce(); |
@@ -9,2 +9,3 @@ import { | ||
CommandLineResults, | ||
ExitReason, | ||
} from './contracts'; | ||
@@ -84,3 +85,3 @@ import commandLineArgs from 'command-line-args'; | ||
if (exitProcess) { | ||
return process.exit(); | ||
return process.exit(resolveExitCode(options, 'usageGuide', parsedArgs, missingArgs)); | ||
} | ||
@@ -110,3 +111,3 @@ } else if (missingArgs.length > 0) { | ||
if (missingArgs.length > 0 && exitProcess) { | ||
process.exit(); | ||
process.exit(resolveExitCode(options, 'missingArgs', parsedArgs, missingArgs)); | ||
} else { | ||
@@ -121,2 +122,18 @@ if (addCommandLineResults) { | ||
function resolveExitCode<T>( | ||
options: ParseOptions<T>, | ||
reason: ExitReason, | ||
passedArgs: Partial<T>, | ||
missingArgs: CommandLineOption<T>[], | ||
): number { | ||
switch (typeof options.processExitCode) { | ||
case 'number': | ||
return options.processExitCode; | ||
case 'function': | ||
return options.processExitCode(reason, passedArgs, missingArgs as any); | ||
default: | ||
return 0; | ||
} | ||
} | ||
function printHelpGuide<T>( | ||
@@ -123,0 +140,0 @@ options: ParseOptions<T>, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
690975
80
5462
26