Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-command-line-args

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-command-line-args - npm Package Compare versions

Comparing version 1.0.5 to 1.1.0

7

dist/contracts.d.ts

@@ -85,2 +85,9 @@ export declare type ArgumentConfig<T extends {

footerContentSections?: Content[];
/**
* Used when generating error messages.
* For example if a param is missing and there is a help option the error message will contain:
*
* 'To view help guide run myBaseCommand -h'
*/
baseCommand?: string;
}

@@ -87,0 +94,0 @@ export interface PartialParseOptions extends ArgsParseOptions<any> {

34

dist/parse.js
"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);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {

@@ -22,3 +33,4 @@ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;

var logger = options.logger || console;
var optionList = helpers_1.createCommandLineConfig(helpers_1.normaliseConfig(config));
var normalisedConfig = helpers_1.normaliseConfig(config);
var optionList = helpers_1.createCommandLineConfig(normalisedConfig);
var parsedArgs = command_line_args_1.default(optionList, options);

@@ -33,3 +45,4 @@ var missingArgs = listMissingArgs(optionList, parsedArgs);

else {
printMissingArgErrors(missingArgs, logger);
printMissingArgErrors(missingArgs, logger, options.baseCommand);
printUsageGuideMessage(__assign(__assign({}, options), { logger: logger }), options.helpArg != null ? optionList.filter(function (option) { return option.name === options.helpArg; })[0] : undefined);
}

@@ -44,8 +57,19 @@ if (missingArgs.length > 0 && exitProcess) {

exports.parse = parse;
function printMissingArgErrors(missingArgs, logger) {
function printMissingArgErrors(missingArgs, logger, baseCommand) {
baseCommand = baseCommand ? baseCommand + " " : "";
missingArgs.forEach(function (config) {
var aliasMessage = config.alias != null ? " or '-" + config.alias + " passedValue'" : "";
logger.error("Required parameter '" + config.name + "' was not passed. Please provide a value by passing '--" + config.name + "=passedValue'" + aliasMessage + " in command line arguments");
var aliasMessage = config.alias != null ? " or '" + baseCommand + "-" + config.alias + " passedValue'" : "";
var runCommand = baseCommand !== ''
? "running '" + baseCommand + "--" + config.name + "=passedValue'" + aliasMessage
: "passing '--" + config.name + "=passedValue'" + aliasMessage + " in command line arguments";
logger.error("Required parameter '" + config.name + "' was not passed. Please provide a value by " + runCommand);
});
}
function printUsageGuideMessage(options, helpParam) {
if (helpParam != null) {
var helpArg = helpParam.alias != null ? "-" + helpParam.alias : "--" + helpParam.name;
var command = options.baseCommand != null ? "run '" + options.baseCommand + " " + helpArg + "'" : "pass '" + helpArg + "'";
options.logger.log("To view the help guide " + command);
}
}
function listMissingArgs(commandLineConfig, parsedArgs) {

@@ -52,0 +76,0 @@ return commandLineConfig

2

package.json
{
"name": "ts-command-line-args",
"version": "1.0.5",
"version": "1.1.0",
"description": "A thin Typescript wrapper around command-line-args",

@@ -5,0 +5,0 @@ "keywords": [

@@ -95,2 +95,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */

footerContentSections?: Content[];
/**
* Used when generating error messages.
* For example if a param is missing and there is a help option the error message will contain:
*
* 'To view help guide run myBaseCommand -h'
*/
baseCommand?: string;
}

@@ -97,0 +105,0 @@

@@ -112,13 +112,51 @@ import { ArgumentConfig } from './contracts';

const expectedRequiredStringMessage = `Required parameter 'requiredString' was not passed. Please provide a value by passing '--requiredString=passedValue' in command line arguments`;
const expectedRequiredArrayMessage = `Required parameter 'requiredArray' was not passed. Please provide a value by passing '--requiredArray=passedValue' or '-o passedValue' in command line arguments`;
it(`should print errors and exit process when required arguments are missing and no baseCommand or help arg are passed`, () => {
const result = parse(getConfig(), {
logger: mockConsole.mock,
argv: [...defaultedString],
});
it(`should print errors and exit process when required arguments are missing`, () => {
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredString' was not passed. Please provide a value by passing '--requiredString=passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredArray' was not passed. Please provide a value by passing '--requiredArray=passedValue' or '-o passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(mockConsole.withFunction('log')).wasNotCalled();
expect(mockProcess.withFunction('exit')).wasCalledOnce();
expect(result).toBeUndefined();
});
it(`should print errors and exit process when required arguments are missing and baseCommand is present`, () => {
const result = parse(getConfig(), {
logger: mockConsole.mock,
argv: [...defaultedString],
baseCommand: 'runMyScript',
});
expect(mockConsole.withFunction('error').withParameters(expectedRequiredStringMessage)).wasCalledOnce();
expect(mockConsole.withFunction('error').withParameters(expectedRequiredArrayMessage)).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredString' was not passed. Please provide a value by running 'runMyScript --requiredString=passedValue'`,
),
).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredArray' was not passed. Please provide a value by running 'runMyScript --requiredArray=passedValue' or 'runMyScript -o passedValue'`,
),
).wasCalledOnce();
expect(mockConsole.withFunction('log')).wasNotCalled();

@@ -130,2 +168,61 @@ expect(mockProcess.withFunction('exit')).wasCalledOnce();

it(`should print errors and exit process when required arguments are missing and help arg is present`, () => {
const result = parse(getHelpConfig(), {
logger: mockConsole.mock,
argv: [...defaultedString],
helpArg: 'optionalHelpArg',
});
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredString' was not passed. Please provide a value by passing '--requiredString=passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredArray' was not passed. Please provide a value by passing '--requiredArray=passedValue' or '-o passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(mockConsole.withFunction('log').withParameters(`To view the help guide pass '-h'`)).wasCalledOnce();
expect(mockProcess.withFunction('exit')).wasCalledOnce();
expect(result).toBeUndefined();
});
it(`should print errors and exit process when required arguments are missing and help arg and baseCommand are present`, () => {
const result = parse(getHelpConfig(), {
logger: mockConsole.mock,
argv: [...defaultedString],
baseCommand: 'runMyScript',
helpArg: 'optionalHelpArg',
});
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredString' was not passed. Please provide a value by running 'runMyScript --requiredString=passedValue'`,
),
).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredArray' was not passed. Please provide a value by running 'runMyScript --requiredArray=passedValue' or 'runMyScript -o passedValue'`,
),
).wasCalledOnce();
expect(
mockConsole.withFunction('log').withParameters(`To view the help guide run 'runMyScript -h'`),
).wasCalledOnce();
expect(mockProcess.withFunction('exit')).wasCalledOnce();
expect(result).toBeUndefined();
});
it(`should print warnings, return an incomplete result when arguments are missing and exitProcess is false`, () => {

@@ -140,6 +237,17 @@ const result = parse(

);
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredString' was not passed. Please provide a value by passing '--requiredString=passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(
mockConsole
.withFunction('error')
.withParameters(
`Required parameter 'requiredArray' was not passed. Please provide a value by passing '--requiredArray=passedValue' or '-o passedValue' in command line arguments`,
),
).wasCalledOnce();
expect(mockConsole.withFunction('error').withParameters(expectedRequiredStringMessage)).wasCalledOnce();
expect(mockConsole.withFunction('error').withParameters(expectedRequiredArrayMessage)).wasCalledOnce();
expect(mockProcess.withFunction('exit')).wasNotCalled();

@@ -146,0 +254,0 @@

@@ -1,2 +0,2 @@

import { ArgumentConfig, ParseOptions, UnkownProperties } from './contracts';
import { ArgumentConfig, ParseOptions, UnkownProperties, PropertyOptions } from './contracts';
import commandLineArgs from 'command-line-args';

@@ -13,3 +13,4 @@ import commandLineUsage from 'command-line-usage';

const logger = options.logger || console;
const optionList = createCommandLineConfig(normaliseConfig(config));
const normalisedConfig = normaliseConfig(config);
const optionList = createCommandLineConfig(normalisedConfig);
const parsedArgs = commandLineArgs(optionList, options);

@@ -28,3 +29,7 @@

} else {
printMissingArgErrors(missingArgs, logger);
printMissingArgErrors(missingArgs, logger, options.baseCommand);
printUsageGuideMessage(
{ ...options, logger },
options.helpArg != null ? optionList.filter((option) => option.name === options.helpArg)[0] : undefined,
);
}

@@ -39,11 +44,23 @@

function printMissingArgErrors(missingArgs: CommandLineOption[], logger: Console) {
function printMissingArgErrors(missingArgs: CommandLineOption[], logger: Console, baseCommand?: string) {
baseCommand = baseCommand ? `${baseCommand} ` : ``;
missingArgs.forEach((config) => {
const aliasMessage = config.alias != null ? ` or '-${config.alias} passedValue'` : ``;
logger.error(
`Required parameter '${config.name}' was not passed. Please provide a value by passing '--${config.name}=passedValue'${aliasMessage} in command line arguments`,
);
const aliasMessage = config.alias != null ? ` or '${baseCommand}-${config.alias} passedValue'` : ``;
const runCommand =
baseCommand !== ''
? `running '${baseCommand}--${config.name}=passedValue'${aliasMessage}`
: `passing '--${config.name}=passedValue'${aliasMessage} in command line arguments`;
logger.error(`Required parameter '${config.name}' was not passed. Please provide a value by ${runCommand}`);
});
}
function printUsageGuideMessage(options: ParseOptions<any> & { logger: Console }, helpParam?: CommandLineOption) {
if (helpParam != null) {
const helpArg = helpParam.alias != null ? `-${helpParam.alias}` : `--${helpParam.name}`;
const command = options.baseCommand != null ? `run '${options.baseCommand} ${helpArg}'` : `pass '${helpArg}'`;
options.logger.log(`To view the help guide ${command}`);
}
}
function listMissingArgs<T>(commandLineConfig: CommandLineOption[], parsedArgs: commandLineArgs.CommandLineOptions) {

@@ -50,0 +67,0 @@ return commandLineConfig

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc