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

argumentate

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argumentate - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0

.github/workflows/test_release.yml

98

argumentate.js

@@ -1,14 +0,4 @@

/**
* Check if argument is an option (begins with -- or -)
* @param {string} arg - Argument to check
* @returns {bool} Is it then?
*/
const isOption = (arg) => /^--/.test(arg) || /^-/.test(arg);
/**
* Gets the name of a given option (ex: --plep ==> plep)
* @param {string} arg => Argument (option) from which to extract the name
* @returns {string} option name
*/
const getOptionName = (arg, mappings={}) => {
const getOptionName = (arg, mapping={}) => {
// Delete first dashes (ex: --plep-plop ==> plep-plop)

@@ -19,4 +9,7 @@ let opt = arg.replace(/^-(-)?/g, '');

if(mappings[opt]) {
return mappings[opt];
if(mapping[opt]) {
if(mapping[opt] instanceof Object) {
return mapping[opt].key;
}
return mapping[opt];
}

@@ -27,33 +20,37 @@

/**
* @function
* @name argumentate
* Extracts options and variables from arguments
* @param {string[]} args - List of arguments, usually process.argv (.slice(2) to remove paths)
* @returns {Object} {options:{}, variables: []} Object containing options {opt: true}, and array of variables
*
* @example
* $ argumentate(['start', '-p=8080', '-c', './myconfig.json'])
*
* > {
* > options: {
* > p: '8080',
* > c: './myconfig.json'
* > },
* > variables: ['start']
* > }
*
*
* @example
* argumentate(['start', '-p=8080', '-c', './myconfig.json'], { p: 'port', c: 'config' })
*
* > {
* > options: {
* > port: '8080',
* > config: './myconfig.json'
* > },
* > variables: ['start']
* > }
*/
module.exports = function(args, mapping={}) {
const default_help = ({ options, variables, mapping }, { name, command }) => {
const options_help = Object.entries(mapping).map(([k, v]) => {
const word_option = typeof v === 'string' ? v : v.key;
const word_description = v instanceof Object ? v.help : '';
return `\t-${k}, --${word_option}\t\t${word_description}`;
}).join('\n');
return console.log(`
${name}
Usage:
${command} [options]
Options:
${options_help}`);
};
// mapping is a list of options & default values, or description and keys
// { c: 'config', p: { key: 'port', help: 'The port to use when launching this command' } }
function argumentate({ args=process.argv.slice(2), mapping={}, config={} }={}) {
const {
useHelp=true,
helpKey='h',
helpWord='help',
autoExit=true,
help=default_help,
name,
command
} = config;
if(useHelp && !mapping[helpKey]) {
mapping[helpKey] = helpWord;
}
let options = {};

@@ -101,2 +98,13 @@ let variables = [];

if(useHelp && (options[helpWord] || options[helpKey])) {
if(!(help instanceof Function)) {
throw new Error(`The help argument must be a function, got ${typeof help}`);
}
help({ options, variables, mapping }, { name, command });
if(autoExit) {
return process.exit(0);
}
}
return {

@@ -107,1 +115,3 @@ options,

};
module.exports = argumentate;
{
"name": "argumentate",
"version": "2.0.0",
"version": "3.0.0",
"description": "A simple argv parser (like so many others)",

@@ -11,3 +11,3 @@ "main": "argumentate.js",

"scripts": {
"test": "nyc mocha --exit test"
"test": "mocha --exit test"
},

@@ -26,4 +26,4 @@ "keywords": [

"mocha": "^6.1.4",
"nyc": "^14.1.1"
"sinon": "^13.0.1"
}
}

@@ -17,3 +17,16 @@ # Argumentate

// slice to remove path args
let { options, variables } = argumentate(process.argv.slice(2));
let { options, variables } = argumentate({
args: process.argv.slice(2),
mapping: {
c: 'config',
p: {
key: 'port',
help: 'Port to use when launching this command'
}
},
config: {
name: 'My CLI',
command: 'invoke-like-this'
}
);

@@ -26,7 +39,14 @@ // be happy

```js
argumentate(['start', '-p=8080', '-c', './myconfig.json'], {
p: 'port',
c: 'config'
});
const { options, variables } = argumentate({
args: ['start', '-p=8080', '-c', './myconfig.json'],
mapping: {
c: 'config',
p: {
key: 'port',
help: 'Port to use when launching this command'
}
}
);
// returns
{

@@ -40,3 +60,1 @@ options: {

````
More info in comments!!

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

const Sinon = require('sinon');
const { expect } = require('chai');

@@ -8,3 +9,3 @@

let args = ['-c'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -19,3 +20,3 @@ expect(argumentate).to.have.property('options');

let args = ['-c'];
let argumentate = Argumentate(args, { c: 'config' });
let argumentate = Argumentate({ args, mapping: { c: 'config' } });

@@ -30,3 +31,3 @@ expect(argumentate).to.have.property('options');

let args = ['start'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -40,3 +41,3 @@ expect(argumentate).to.have.property('variables');

let args = ['start', '-c'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -55,3 +56,3 @@ expect(argumentate).to.have.property('options');

let args = ['-c', 'start'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -66,3 +67,3 @@ expect(argumentate).to.have.property('options');

let args = ['-c=start'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -77,3 +78,3 @@ expect(argumentate).to.have.property('options');

let args = ['-c=start', '-c', 'plep', '-c=45'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -89,3 +90,3 @@ expect(argumentate).to.have.property('options');

let args = ['start', '-c', 'poulet'];
let argumentate = Argumentate(args);
let argumentate = Argumentate({ args });

@@ -104,3 +105,3 @@ expect(argumentate).to.have.property('options');

let args = ['start', '-c', 'poulet'];
let argumentate = Argumentate(args, { c: 'config' });
let argumentate = Argumentate({ args, mapping: { c: 'config' }});

@@ -116,2 +117,28 @@ expect(argumentate).to.have.property('options');

});
for(const arg of ['-h', '--help']) {
it(`Should automatically call the help function if ${arg} is used`, () => {
let args = [arg];
let help = Sinon.fake();
Argumentate({ args, config: { help, autoExit: false }});
expect(help.calledOnce).to.be.true;
});
}
for(const arg of ['-o', '--ooo']) {
it(`Should call the help function with custom arg ${arg}`, () => {
let args = [arg];
let help = Sinon.fake();
Argumentate({args, config: { help, autoExit: false, helpKey: 'o', helpWord: 'ooo' }});
expect(help.calledOnce).to.be.true;
});
}
it('Should not call the help function if useHelp is false', () => {
const args = ['-h'];
const help = Sinon.fake();
Argumentate({args, config: { help, autoExit: false, useHelp: false }});
expect(help.callCount).to.be.eql(0);
});
});
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