ts-command-line-args
Advanced tools
Comparing version 2.1.0 to 2.1.2
@@ -21,3 +21,3 @@ module.exports = { | ||
rules: { | ||
indent: ['error', 4, { SwitchCase: 1 }], | ||
indent: 'off', | ||
semi: ['error', 'always'], | ||
@@ -24,0 +24,0 @@ '@typescript-eslint/no-explicit-any': 'off', |
{ | ||
"typescript.tsdk": "node_modules\\typescript\\lib" | ||
"typescript.tsdk": "node_modules\\typescript\\lib", | ||
"cSpell.words": [ | ||
"organise" | ||
] | ||
} |
@@ -56,5 +56,6 @@ export declare type ArgumentConfig<T extends { | ||
} & OptionalPropertyOptions<T> & MultiplePropertyOptions<T>; | ||
export declare type OptionalPropertyOptions<T> = undefined extends T ? { | ||
export declare type OptionalProperty = { | ||
optional: true; | ||
} : unknown; | ||
}; | ||
export declare type OptionalPropertyOptions<T> = undefined extends T ? OptionalProperty : unknown; | ||
export declare type MultiplePropertyOptions<T> = Array<any> extends T ? { | ||
@@ -168,2 +169,14 @@ multiple: true; | ||
helpWhenArgMissingHeader?: ((missingArgs: CommandLineOption[]) => Omit<Content, 'includeIn'>) | Omit<Content, 'includeIn'>; | ||
/** | ||
* adds a (O), (D) or both to typeLabel to indicate if a property is optional or the default option | ||
*/ | ||
displayOptionalAndDefault?: boolean; | ||
/** | ||
* if displayOptionalAndDefault is true and any params are optional or default adds a footer explaining what the (O), (D) means | ||
*/ | ||
addOptionalDefaultExplanatoryFooter?: boolean; | ||
/** | ||
* prepends the supplied description with details about the param. These include default option, optional and the default value. | ||
*/ | ||
prependParamOptionsToDescription?: boolean; | ||
} | ||
@@ -170,0 +183,0 @@ export interface PartialParseOptions extends ArgsParseOptions<any> { |
@@ -7,3 +7,3 @@ import { ArgumentConfig, ArgumentOptions, CommandLineOption } from '../contracts'; | ||
* commandLineArgs throws an error if we pass aa value for a boolean arg as follows: | ||
* myCommand -a=true --booleaanArg=false --otherArg true | ||
* myCommand -a=true --booleanArg=false --otherArg true | ||
* this function removes these booleans so as to avoid errors from commandLineArgs | ||
@@ -15,3 +15,3 @@ * @param args | ||
/** | ||
* Gets the values of anyy boolean arguments that were specified on the commadn line with a value | ||
* Gets the values of any boolean arguments that were specified on the command line with a value | ||
* These arguments were removed by removeBooleanValues | ||
@@ -18,0 +18,0 @@ * @param args |
@@ -22,2 +22,3 @@ "use strict"; | ||
exports.getBooleanValues = exports.removeBooleanValues = exports.mergeConfig = exports.normaliseConfig = exports.createCommandLineConfig = void 0; | ||
var options_helper_1 = require("./options.helper"); | ||
function createCommandLineConfig(config) { | ||
@@ -96,3 +97,3 @@ return Object.keys(config).map(function (key) { | ||
* commandLineArgs throws an error if we pass aa value for a boolean arg as follows: | ||
* myCommand -a=true --booleaanArg=false --otherArg true | ||
* myCommand -a=true --booleanArg=false --otherArg true | ||
* this function removes these booleans so as to avoid errors from commandLineArgs | ||
@@ -105,3 +106,4 @@ * @param args | ||
var _a = getParamConfig(arg, config), argOptions = _a.argOptions, argValue = _a.argValue; | ||
if (isBoolean(argsAndLastValue.lastOption) && booleanValue.some(function (boolValue) { return boolValue === arg; })) { | ||
var lastOption = argsAndLastValue.lastOption; | ||
if (lastOption != null && options_helper_1.isBoolean(lastOption) && booleanValue.some(function (boolValue) { return boolValue === arg; })) { | ||
var args_1 = argsAndLastValue.args.concat(); | ||
@@ -111,3 +113,3 @@ args_1.pop(); | ||
} | ||
else if (isBoolean(argOptions) && argValue != null) { | ||
else if (argOptions != null && options_helper_1.isBoolean(argOptions) && argValue != null) { | ||
return { args: argsAndLastValue.args }; | ||
@@ -123,3 +125,3 @@ } | ||
/** | ||
* Gets the values of anyy boolean arguments that were specified on the commadn line with a value | ||
* Gets the values of any boolean arguments that were specified on the command line with a value | ||
* These arguments were removed by removeBooleanValues | ||
@@ -132,9 +134,11 @@ * @param args | ||
var _a = getParamConfig(arg, config), argOptions = _a.argOptions, argName = _a.argName, argValue = _a.argValue; | ||
if (isBoolean(argOptions) && argValue != null && argName != null) { | ||
var lastOption = argsAndLastOption.lastOption; | ||
if (argOptions != null && options_helper_1.isBoolean(argOptions) && argValue != null && argName != null) { | ||
argsAndLastOption.partial[argName] = convertType(argValue, argOptions); | ||
} | ||
else if (argsAndLastOption.lastName != null && | ||
isBoolean(argsAndLastOption.lastOption) && | ||
lastOption != null && | ||
options_helper_1.isBoolean(lastOption) && | ||
booleanValue.some(function (boolValue) { return boolValue === arg; })) { | ||
argsAndLastOption.partial[argsAndLastOption.lastName] = convertType(arg, argsAndLastOption.lastOption); | ||
argsAndLastOption.partial[argsAndLastOption.lastName] = convertType(arg, lastOption); | ||
} | ||
@@ -146,5 +150,2 @@ return { partial: argsAndLastOption.partial, lastName: argName, lastOption: argOptions }; | ||
exports.getBooleanValues = getBooleanValues; | ||
function isBoolean(option) { | ||
return option != null && option.type.name === 'Boolean'; | ||
} | ||
function getParamConfig(arg, config) { | ||
@@ -151,0 +152,0 @@ var regExpResult = argNameRegExp.exec(arg); |
@@ -7,3 +7,3 @@ import { UsageGuideConfig, IWriteMarkDown, ArgumentConfig, CommandLineOption, ParseOptions, Content, HeaderLevel, OptionContent, SectionHeader } from '../contracts'; | ||
export declare function createOptionsSections<T>(cliArguments: ArgumentConfig<T>, options: ParseOptions<any>): string[]; | ||
export declare function createOptionsSection<T>(optionList: CommandLineOption<any>[], content: OptionContent): string; | ||
export declare function createOptionsSection<T>(optionList: CommandLineOption<any>[], content: OptionContent, options: ParseOptions<any>): string; | ||
export declare function createHeading(section: SectionHeader, defaultLevel: HeaderLevel): string; | ||
@@ -10,0 +10,0 @@ export declare function createOptionRow(option: CommandLineOption, includeAlias?: boolean, includeDescription?: boolean): string; |
@@ -59,10 +59,14 @@ "use strict"; | ||
} | ||
return options_helper_1.getOptionSections(options).map(function (section) { return createOptionsSection(optionList, section); }); | ||
return options_helper_1.getOptionSections(options).map(function (section) { return createOptionsSection(optionList, section, options); }); | ||
} | ||
exports.createOptionsSections = createOptionsSections; | ||
function createOptionsSection(optionList, content) { | ||
function createOptionsSection(optionList, content, options) { | ||
optionList = optionList.filter(function (option) { return filterOptions(option, content.group); }); | ||
var anyAlias = optionList.some(function (option) { return option.alias != null; }); | ||
var anyDescription = optionList.some(function (option) { return option.description != null; }); | ||
return "\n" + createHeading(content, 2) + "\n| Argument |" + (anyAlias ? ' Alias |' : '') + " Type |" + (anyDescription ? ' Description |' : '') + "\n|-|" + (anyAlias ? '-|' : '') + "-|" + (anyDescription ? '-|' : '') + "\n" + optionList.map(function (option) { return createOptionRow(option, anyAlias, anyDescription); }).join('\n') + "\n"; | ||
var footer = options_helper_1.generateTableFooter(optionList, options); | ||
return "\n" + createHeading(content, 2) + "\n| Argument |" + (anyAlias ? ' Alias |' : '') + " Type |" + (anyDescription ? ' Description |' : '') + "\n|-|" + (anyAlias ? '-|' : '') + "-|" + (anyDescription ? '-|' : '') + "\n" + optionList | ||
.map(function (option) { return options_helper_1.mapDefinitionDetails(option, options); }) | ||
.map(function (option) { return createOptionRow(option, anyAlias, anyDescription); }) | ||
.join('\n') + "\n" + (footer != null ? footer + '\n' : ''); | ||
} | ||
@@ -101,2 +105,3 @@ exports.createOptionsSection = createOptionsSection; | ||
} | ||
//TODO: add modifiers | ||
var type = option.type ? option.type.name.toLowerCase() : 'string'; | ||
@@ -103,0 +108,0 @@ var multiple = option.multiple || option.lazyMultiple ? '[]' : ''; |
@@ -1,2 +0,12 @@ | ||
import { ParseOptions, OptionContent } from '../contracts'; | ||
import { Content, OptionList } from 'command-line-usage'; | ||
import { ParseOptions, OptionContent, CommandLineOption, PropertyOptions } from '../contracts'; | ||
export declare function getOptionSections(options: ParseOptions<any>): OptionContent[]; | ||
export declare function getOptionFooterSection<T>(optionList: CommandLineOption<T>[], options: ParseOptions<any>): Content[]; | ||
export declare function generateTableFooter<T>(optionList: CommandLineOption<T>[], options: ParseOptions<any>): string | undefined; | ||
export declare function addOptions<T>(content: OptionContent, optionList: CommandLineOption<T>[], options: ParseOptions<T>): OptionList; | ||
/** | ||
* adds default or optional modifiers to type label or description | ||
* @param option | ||
*/ | ||
export declare function mapDefinitionDetails<T>(definition: CommandLineOption<T>, options: ParseOptions<T>): CommandLineOption<T>; | ||
export declare function isBoolean<T>(option: PropertyOptions<T>): boolean; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getOptionSections = void 0; | ||
exports.isBoolean = exports.mapDefinitionDetails = exports.addOptions = exports.generateTableFooter = exports.getOptionFooterSection = exports.getOptionSections = void 0; | ||
function getOptionSections(options) { | ||
@@ -10,1 +21,82 @@ return (options.optionSections || [ | ||
exports.getOptionSections = getOptionSections; | ||
function getOptionFooterSection(optionList, options) { | ||
var optionsFooter = generateTableFooter(optionList, options); | ||
if (optionsFooter != null) { | ||
console.log("Adding footer: " + optionsFooter); | ||
return [{ content: optionsFooter }]; | ||
} | ||
return []; | ||
} | ||
exports.getOptionFooterSection = getOptionFooterSection; | ||
function generateTableFooter(optionList, options) { | ||
if (options.addOptionalDefaultExplanatoryFooter != true || options.displayOptionalAndDefault != true) { | ||
return undefined; | ||
} | ||
var optionalProps = optionList.some(function (option) { return option.optional === true; }); | ||
var defaultProps = optionList.some(function (option) { return option.defaultOption === true; }); | ||
if (optionalProps || defaultProps) { | ||
var footerValues = [ | ||
optionalProps != null ? '(O) = optional' : undefined, | ||
defaultProps != null ? '(D) = default option' : null, | ||
]; | ||
return footerValues.filter(function (v) { return v != null; }).join(', '); | ||
} | ||
return undefined; | ||
} | ||
exports.generateTableFooter = generateTableFooter; | ||
function addOptions(content, optionList, options) { | ||
optionList = optionList.map(function (option) { return mapDefinitionDetails(option, options); }); | ||
return __assign(__assign({}, content), { optionList: optionList }); | ||
} | ||
exports.addOptions = addOptions; | ||
/** | ||
* adds default or optional modifiers to type label or description | ||
* @param option | ||
*/ | ||
function mapDefinitionDetails(definition, options) { | ||
definition = mapOptionTypeLabel(definition, options); | ||
definition = mapOptionDescription(definition, options); | ||
return definition; | ||
} | ||
exports.mapDefinitionDetails = mapDefinitionDetails; | ||
function mapOptionDescription(definition, options) { | ||
if (options.prependParamOptionsToDescription !== true || isBoolean(definition)) { | ||
return definition; | ||
} | ||
definition.description = definition.description || ''; | ||
if (definition.defaultOption) { | ||
definition.description = "Default Option. " + definition.description; | ||
} | ||
if (definition.optional === true) { | ||
definition.description = "Optional. " + definition.description; | ||
} | ||
if (definition.defaultValue != null) { | ||
definition.description = "Defaults to '" + String(definition.defaultValue) + "'. " + definition.description; | ||
} | ||
return definition; | ||
} | ||
function mapOptionTypeLabel(definition, options) { | ||
if (options.displayOptionalAndDefault !== true || isBoolean(definition)) { | ||
return definition; | ||
} | ||
definition.typeLabel = definition.typeLabel || getTypeLabel(definition); | ||
if (definition.defaultOption) { | ||
definition.typeLabel = definition.typeLabel + " (D)"; | ||
} | ||
if (definition.optional === true) { | ||
definition.typeLabel = definition.typeLabel + " (O)"; | ||
} | ||
return definition; | ||
} | ||
function getTypeLabel(definition) { | ||
var typeLabel = definition.type ? definition.type.name.toLowerCase() : 'string'; | ||
var multiple = definition.multiple || definition.lazyMultiple ? '[]' : ''; | ||
if (typeLabel) { | ||
typeLabel = typeLabel === 'boolean' ? '' : "{underline " + typeLabel + multiple + "}"; | ||
} | ||
return typeLabel; | ||
} | ||
function isBoolean(option) { | ||
return option.type.name === 'Boolean'; | ||
} | ||
exports.isBoolean = isBoolean; |
@@ -118,3 +118,3 @@ "use strict"; | ||
if (additionalHeaderSections === void 0) { additionalHeaderSections = []; } | ||
var sections = __spreadArrays(additionalHeaderSections, (((_a = options.headerContentSections) === null || _a === void 0 ? void 0 : _a.filter(filterCliSections)) || []), options_helper_1.getOptionSections(options).map(function (option) { return (__assign(__assign({}, option), { optionList: optionList })); }), (((_b = options.footerContentSections) === null || _b === void 0 ? void 0 : _b.filter(filterCliSections)) || [])); | ||
var sections = __spreadArrays(additionalHeaderSections, (((_a = options.headerContentSections) === null || _a === void 0 ? void 0 : _a.filter(filterCliSections)) || []), options_helper_1.getOptionSections(options).map(function (option) { return options_helper_1.addOptions(option, optionList, options); }), options_helper_1.getOptionFooterSection(optionList, options), (((_b = options.footerContentSections) === null || _b === void 0 ? void 0 : _b.filter(filterCliSections)) || [])); | ||
helpers_1.visit(sections, function (value) { | ||
@@ -121,0 +121,0 @@ switch (typeof value) { |
{ | ||
"name": "ts-command-line-args", | ||
"version": "2.1.0", | ||
"version": "2.1.2", | ||
"description": "A Typescript wrapper around command-line-args with additional support for markdown usage guide generation", | ||
@@ -32,8 +32,8 @@ "bin": { | ||
"prebuild-release": "npm run clean", | ||
"build-release": "concurrently --kill-others-on-fail npm:test npm:lint npm:build && codecov && node dist/write-markdown -f package.json -p markdownConfig -v", | ||
"prepack": "npm run build-release", | ||
"build-release": "concurrently --kill-others-on-fail npm:test npm:lint npm:build npm:verify-markdown && codecov", | ||
"prewrite-markdown": "npm run build", | ||
"preverify-markdown": "npm run build", | ||
"write-markdown": "node dist/write-markdown -f package.json -p markdownConfig", | ||
"verify-markdown": "node dist/write-markdown -f package.json -p markdownConfig -v" | ||
"verify-markdown": "node dist/write-markdown -f package.json -p markdownConfig -v", | ||
"prepublishOnly": "npm run build-release" | ||
}, | ||
@@ -65,3 +65,3 @@ "repository": { | ||
"@typescript-eslint/parser": "^3.10.1", | ||
"codecov": "^3.8.0", | ||
"codecov": "^3.8.3", | ||
"concurrently": "^5.3.0", | ||
@@ -68,0 +68,0 @@ "eslint": "^7.7.0", |
@@ -68,4 +68,6 @@ export type ArgumentConfig<T extends { [name: string]: any }> = { | ||
export type OptionalPropertyOptions<T> = undefined extends T ? { optional: true } : unknown; | ||
export type OptionalProperty = { optional: true }; | ||
export type OptionalPropertyOptions<T> = undefined extends T ? OptionalProperty : unknown; | ||
export type MultiplePropertyOptions<T> = Array<any> extends T ? { multiple: true } | { lazyMultiple: true } : unknown; | ||
@@ -190,2 +192,17 @@ | ||
| Omit<Content, 'includeIn'>; | ||
/** | ||
* adds a (O), (D) or both to typeLabel to indicate if a property is optional or the default option | ||
*/ | ||
displayOptionalAndDefault?: boolean; | ||
/** | ||
* if displayOptionalAndDefault is true and any params are optional or default adds a footer explaining what the (O), (D) means | ||
*/ | ||
addOptionalDefaultExplanatoryFooter?: boolean; | ||
/** | ||
* prepends the supplied description with details about the param. These include default option, optional and the default value. | ||
*/ | ||
prependParamOptionsToDescription?: boolean; | ||
} | ||
@@ -192,0 +209,0 @@ |
import { PropertyOptions, ArgumentConfig, ArgumentOptions, CommandLineOption } from '../contracts'; | ||
import { isBoolean } from './options.helper'; | ||
@@ -102,3 +103,3 @@ export function createCommandLineConfig<T>(config: ArgumentOptions<T>): CommandLineOption[] { | ||
* commandLineArgs throws an error if we pass aa value for a boolean arg as follows: | ||
* myCommand -a=true --booleaanArg=false --otherArg true | ||
* myCommand -a=true --booleanArg=false --otherArg true | ||
* this function removes these booleans so as to avoid errors from commandLineArgs | ||
@@ -112,7 +113,9 @@ * @param args | ||
if (isBoolean(argsAndLastValue.lastOption) && booleanValue.some((boolValue) => boolValue === arg)) { | ||
const lastOption = argsAndLastValue.lastOption; | ||
if (lastOption != null && isBoolean(lastOption) && booleanValue.some((boolValue) => boolValue === arg)) { | ||
const args = argsAndLastValue.args.concat(); | ||
args.pop(); | ||
return { args }; | ||
} else if (isBoolean(argOptions) && argValue != null) { | ||
} else if (argOptions != null && isBoolean(argOptions) && argValue != null) { | ||
return { args: argsAndLastValue.args }; | ||
@@ -128,3 +131,3 @@ } else { | ||
/** | ||
* Gets the values of anyy boolean arguments that were specified on the commadn line with a value | ||
* Gets the values of any boolean arguments that were specified on the command line with a value | ||
* These arguments were removed by removeBooleanValues | ||
@@ -138,13 +141,13 @@ * @param args | ||
if (isBoolean(argOptions) && argValue != null && argName != null) { | ||
const lastOption = argsAndLastOption.lastOption; | ||
if (argOptions != null && isBoolean(argOptions) && argValue != null && argName != null) { | ||
argsAndLastOption.partial[argName] = convertType(argValue, argOptions) as any; | ||
} else if ( | ||
argsAndLastOption.lastName != null && | ||
isBoolean(argsAndLastOption.lastOption) && | ||
lastOption != null && | ||
isBoolean(lastOption) && | ||
booleanValue.some((boolValue) => boolValue === arg) | ||
) { | ||
argsAndLastOption.partial[argsAndLastOption.lastName] = convertType( | ||
arg, | ||
argsAndLastOption.lastOption, | ||
) as any; | ||
argsAndLastOption.partial[argsAndLastOption.lastName] = convertType(arg, lastOption) as any; | ||
} | ||
@@ -157,6 +160,2 @@ return { partial: argsAndLastOption.partial, lastName: argName, lastOption: argOptions }; | ||
function isBoolean(option?: PropertyOptions<any>): option is PropertyOptions<any> { | ||
return option != null && option.type.name === 'Boolean'; | ||
} | ||
function getParamConfig<T>( | ||
@@ -163,0 +162,0 @@ arg: string, |
@@ -34,2 +34,47 @@ /* eslint-disable no-useless-escape */ | ||
it('should generate a simple usage guide with typeLabel modifiers', () => { | ||
const info: UsageGuideConfig<ICopyFilesArguments> = { | ||
arguments: { ...exampleConfigGuideInfo.arguments, copyFiles: Boolean }, | ||
parseOptions: { displayOptionalAndDefault: true }, | ||
}; | ||
const usageGuide = createUsageGuide(info); | ||
expect(usageGuide).toEqual(` | ||
## Options | ||
| Argument | Type | | ||
|-|-| | ||
| **sourcePath** | string (D) | | ||
| **targetPath** | string | | ||
| **copyFiles** | boolean | | ||
| **resetPermissions** | boolean | | ||
| **filter** | string (O) | | ||
| **excludePaths** | string[] (O) | | ||
`); | ||
}); | ||
it('should generate a simple usage guide with typeLabel modifiers and footer', () => { | ||
const info: UsageGuideConfig<ICopyFilesArguments> = { | ||
arguments: { ...exampleConfigGuideInfo.arguments, copyFiles: Boolean }, | ||
parseOptions: { addOptionalDefaultExplanatoryFooter: true, displayOptionalAndDefault: true }, | ||
}; | ||
const usageGuide = createUsageGuide(info); | ||
expect(usageGuide).toEqual(` | ||
## Options | ||
| Argument | Type | | ||
|-|-| | ||
| **sourcePath** | string (D) | | ||
| **targetPath** | string | | ||
| **copyFiles** | boolean | | ||
| **resetPermissions** | boolean | | ||
| **filter** | string (O) | | ||
| **excludePaths** | string[] (O) | | ||
(O) = optional, (D) = default option | ||
`); | ||
}); | ||
it('should generate a simple usage guide with no additional sections', () => { | ||
@@ -154,4 +199,4 @@ const usageGuide = createUsageGuide(exampleConfigGuideInfo); | ||
| **help** | **h** | boolean | Display this usage guide. | | ||
| **src** | | file ... | The input files to process | | ||
| **timeout** | **t** | ms | Timeout value in ms | | ||
| **src** | | file ... | Default Option. The input files to process | | ||
| **timeout** | **t** | ms | Defaults to '1000'. Timeout value in ms | | ||
@@ -163,3 +208,3 @@ | ||
|-|-|-| | ||
| **plugin** | string | A plugin path | | ||
| **plugin** | string | Optional. A plugin path | | ||
`); | ||
@@ -166,0 +211,0 @@ }); |
@@ -15,3 +15,3 @@ import { | ||
import { normaliseConfig, createCommandLineConfig } from './command-line.helper'; | ||
import { getOptionSections } from './options.helper'; | ||
import { generateTableFooter, getOptionSections, mapDefinitionDetails } from './options.helper'; | ||
import { convertChalkStringToMarkdown } from './string.helper'; | ||
@@ -78,6 +78,10 @@ | ||
return getOptionSections(options).map((section) => createOptionsSection(optionList, section)); | ||
return getOptionSections(options).map((section) => createOptionsSection(optionList, section, options)); | ||
} | ||
export function createOptionsSection<T>(optionList: CommandLineOption<any>[], content: OptionContent): string { | ||
export function createOptionsSection<T>( | ||
optionList: CommandLineOption<any>[], | ||
content: OptionContent, | ||
options: ParseOptions<any>, | ||
): string { | ||
optionList = optionList.filter((option) => filterOptions(option, content.group)); | ||
@@ -87,2 +91,4 @@ const anyAlias = optionList.some((option) => option.alias != null); | ||
const footer = generateTableFooter(optionList, options); | ||
return ` | ||
@@ -92,4 +98,7 @@ ${createHeading(content, 2)} | ||
|-|${anyAlias ? '-|' : ''}-|${anyDescription ? '-|' : ''} | ||
${optionList.map((option) => createOptionRow(option, anyAlias, anyDescription)).join('\n')} | ||
`; | ||
${optionList | ||
.map((option) => mapDefinitionDetails(option, options)) | ||
.map((option) => createOptionRow(option, anyAlias, anyDescription)) | ||
.join('\n')} | ||
${footer != null ? footer + '\n' : ''}`; | ||
} | ||
@@ -133,2 +142,4 @@ | ||
//TODO: add modifiers | ||
const type = option.type ? option.type.name.toLowerCase() : 'string'; | ||
@@ -135,0 +146,0 @@ const multiple = option.multiple || option.lazyMultiple ? '[]' : ''; |
@@ -1,2 +0,3 @@ | ||
import { ParseOptions, OptionContent } from '../contracts'; | ||
import { Content, OptionList } from 'command-line-usage'; | ||
import { ParseOptions, OptionContent, CommandLineOption, OptionalProperty, PropertyOptions } from '../contracts'; | ||
@@ -10,1 +11,112 @@ export function getOptionSections(options: ParseOptions<any>): OptionContent[] { | ||
} | ||
export function getOptionFooterSection<T>(optionList: CommandLineOption<T>[], options: ParseOptions<any>): Content[] { | ||
const optionsFooter = generateTableFooter(optionList, options); | ||
if (optionsFooter != null) { | ||
console.log(`Adding footer: ${optionsFooter}`); | ||
return [{ content: optionsFooter }]; | ||
} | ||
return []; | ||
} | ||
export function generateTableFooter<T>( | ||
optionList: CommandLineOption<T>[], | ||
options: ParseOptions<any>, | ||
): string | undefined { | ||
if (options.addOptionalDefaultExplanatoryFooter != true || options.displayOptionalAndDefault != true) { | ||
return undefined; | ||
} | ||
const optionalProps = optionList.some((option) => ((option as unknown) as OptionalProperty).optional === true); | ||
const defaultProps = optionList.some((option) => option.defaultOption === true); | ||
if (optionalProps || defaultProps) { | ||
const footerValues = [ | ||
optionalProps != null ? '(O) = optional' : undefined, | ||
defaultProps != null ? '(D) = default option' : null, | ||
]; | ||
return footerValues.filter((v) => v != null).join(', '); | ||
} | ||
return undefined; | ||
} | ||
export function addOptions<T>( | ||
content: OptionContent, | ||
optionList: CommandLineOption<T>[], | ||
options: ParseOptions<T>, | ||
): OptionList { | ||
optionList = optionList.map((option) => mapDefinitionDetails(option, options)); | ||
return { ...content, optionList }; | ||
} | ||
/** | ||
* adds default or optional modifiers to type label or description | ||
* @param option | ||
*/ | ||
export function mapDefinitionDetails<T>( | ||
definition: CommandLineOption<T>, | ||
options: ParseOptions<T>, | ||
): CommandLineOption<T> { | ||
definition = mapOptionTypeLabel(definition, options); | ||
definition = mapOptionDescription(definition, options); | ||
return definition; | ||
} | ||
function mapOptionDescription<T>(definition: CommandLineOption<T>, options: ParseOptions<T>): CommandLineOption<T> { | ||
if (options.prependParamOptionsToDescription !== true || isBoolean(definition)) { | ||
return definition; | ||
} | ||
definition.description = definition.description || ''; | ||
if (definition.defaultOption) { | ||
definition.description = `Default Option. ${definition.description}`; | ||
} | ||
if (((definition as unknown) as OptionalProperty).optional === true) { | ||
definition.description = `Optional. ${definition.description}`; | ||
} | ||
if (definition.defaultValue != null) { | ||
definition.description = `Defaults to '${String(definition.defaultValue)}'. ${definition.description}`; | ||
} | ||
return definition; | ||
} | ||
function mapOptionTypeLabel<T>(definition: CommandLineOption<T>, options: ParseOptions<T>): CommandLineOption<T> { | ||
if (options.displayOptionalAndDefault !== true || isBoolean(definition)) { | ||
return definition; | ||
} | ||
definition.typeLabel = definition.typeLabel || getTypeLabel(definition); | ||
if (definition.defaultOption) { | ||
definition.typeLabel = `${definition.typeLabel} (D)`; | ||
} | ||
if (((definition as unknown) as OptionalProperty).optional === true) { | ||
definition.typeLabel = `${definition.typeLabel} (O)`; | ||
} | ||
return definition; | ||
} | ||
function getTypeLabel<T>(definition: CommandLineOption<T>) { | ||
let typeLabel = definition.type ? definition.type.name.toLowerCase() : 'string'; | ||
const multiple = definition.multiple || definition.lazyMultiple ? '[]' : ''; | ||
if (typeLabel) { | ||
typeLabel = typeLabel === 'boolean' ? '' : `{underline ${typeLabel}${multiple}}`; | ||
} | ||
return typeLabel; | ||
} | ||
export function isBoolean<T>(option: PropertyOptions<T>): boolean { | ||
return option.type.name === 'Boolean'; | ||
} |
@@ -20,3 +20,3 @@ import { | ||
} from './helpers'; | ||
import { getOptionSections } from './helpers/options.helper'; | ||
import { addOptions, getOptionFooterSection, getOptionSections } from './helpers/options.helper'; | ||
import { removeAdditionalFormatting } from './helpers/string.helper'; | ||
@@ -128,3 +128,4 @@ import { readFileSync } from 'fs'; | ||
...(options.headerContentSections?.filter(filterCliSections) || []), | ||
...getOptionSections(options).map((option) => ({ ...option, optionList })), | ||
...getOptionSections(options).map((option) => addOptions(option, optionList, options)), | ||
...getOptionFooterSection(optionList, options), | ||
...(options.footerContentSections?.filter(filterCliSections) || []), | ||
@@ -131,0 +132,0 @@ ]; |
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
681785
5379