Comparing version 3.4.5 to 3.5.0
109
index.js
@@ -1,2 +0,4 @@ | ||
var path = require('path'), | ||
var assert = require('assert'), | ||
path = require('path'), | ||
Completion = require('./lib/completion'), | ||
Parser = require('./lib/parser'), | ||
@@ -6,20 +8,11 @@ Usage = require('./lib/usage'), | ||
/* Hack an instance of Argv with process.argv into Argv | ||
so people can do | ||
require('yargs')(['--beeble=1','-z','zizzle']).argv | ||
to parse a list of args and | ||
require('yargs').argv | ||
to get a parsed version of process.argv. | ||
*/ | ||
var inst = Argv(process.argv.slice(2)); | ||
Object.keys(inst).forEach(function (key) { | ||
Argv[key] = typeof inst[key] == 'function' | ||
? inst[key].bind(inst) | ||
: inst[key]; | ||
}); | ||
sigletonify(inst); | ||
var exports = module.exports = Argv; | ||
function Argv (processArgs, cwd) { | ||
processArgs = processArgs || []; // handle calling yargs(). | ||
var self = {}; | ||
var completion = null; | ||
var usage = null; | ||
@@ -33,2 +26,5 @@ var validation = null; | ||
.map(function (x) { | ||
// ignore the node bin, specify this in your | ||
// bin file with #!/usr/bin/env node | ||
if (~x.indexOf('node')) return; | ||
var b = rebase(cwd, x); | ||
@@ -38,3 +34,3 @@ return x.match(/^\//) && b.length < x.length | ||
}) | ||
.join(' ') | ||
.join(' ').trim(); | ||
; | ||
@@ -70,2 +66,3 @@ | ||
validation = Validation(self, usage); // handle arg validation. | ||
completion = Completion(self, usage); | ||
@@ -75,4 +72,6 @@ demanded = {}; | ||
exitProcess = true; | ||
strict = false; | ||
helpOpt = null; | ||
versionOpt = null; | ||
completionOpt = null; | ||
@@ -242,2 +241,4 @@ return self; | ||
else { | ||
assert(typeof opt === 'object', 'second argument to option must be an object'); | ||
options.key[key] = true; // track manually set keys. | ||
@@ -300,2 +301,5 @@ | ||
}; | ||
self.getStrict = function () { | ||
return strict; | ||
} | ||
@@ -347,2 +351,32 @@ self.showHelp = function (fn) { | ||
var completionOpt = null, | ||
completionCommand = null; | ||
self.completion = function(cmd, desc, fn) { | ||
// a function to execute when generating | ||
// completions can be provided as the second | ||
// or third argument to completion. | ||
if (typeof desc === 'function') { | ||
fn = desc; | ||
desc = null; | ||
} | ||
// register the completion command. | ||
completionCommand = cmd; | ||
completionOpt = completion.completionKey; | ||
self.command(completionCommand, desc || 'generate bash completion script'); | ||
// a function can be provided | ||
if (fn) completion.registerFunction(fn); | ||
if (!self.parsed) parseArgs(processArgs); // run parser, if it has not already been executed. | ||
return self; | ||
}; | ||
self.showCompletionScript = function($0) { | ||
$0 = $0 || self.$0; | ||
console.log(completion.generateCompletionScript($0)); | ||
return self; | ||
}; | ||
self.getUsageInstance = function () { | ||
@@ -380,2 +414,10 @@ return usage; | ||
// generate a completion script for adding to ~/.bashrc. | ||
if (completionCommand && ~argv._.indexOf(completionCommand)) { | ||
self.showCompletionScript(); | ||
if (exitProcess){ | ||
process.exit(0); | ||
} | ||
} | ||
Object.keys(argv).forEach(function(key) { | ||
@@ -394,2 +436,16 @@ if (key === helpOpt) { | ||
} | ||
else if (key === completionOpt) { | ||
// we allow for asynchronous completions, | ||
// e.g., loading in a list of commands from an API. | ||
completion.getCompletion(function(completions) { | ||
(completions || []).forEach(function(completion) { | ||
console.log(completion); | ||
}); | ||
if (exitProcess){ | ||
process.exit(0); | ||
} | ||
}); | ||
return; | ||
} | ||
}); | ||
@@ -407,2 +463,3 @@ | ||
validation.implications(argv); | ||
setPlaceholderKeys(argv); | ||
@@ -412,2 +469,9 @@ return argv; | ||
function setPlaceholderKeys (argv) { | ||
Object.keys(options.key).forEach(function(key) { | ||
if (typeof argv[key] === 'undefined') argv[key] = undefined; | ||
}); | ||
} | ||
sigletonify(self); | ||
return self; | ||
@@ -422,1 +486,16 @@ }; | ||
}; | ||
/* Hack an instance of Argv with process.argv into Argv | ||
so people can do | ||
require('yargs')(['--beeble=1','-z','zizzle']).argv | ||
to parse a list of args and | ||
require('yargs').argv | ||
to get a parsed version of process.argv. | ||
*/ | ||
function sigletonify(inst) { | ||
Object.keys(inst).forEach(function (key) { | ||
Argv[key] = typeof inst[key] == 'function' | ||
? inst[key].bind(inst) | ||
: inst[key]; | ||
}); | ||
} |
@@ -70,2 +70,5 @@ // this file handles outputting usage instructions, | ||
}; | ||
self.getCommands = function () { | ||
return commands; | ||
}; | ||
@@ -72,0 +75,0 @@ var descriptions = {}; |
{ | ||
"name": "yargs", | ||
"version": "3.4.5", | ||
"version": "3.5.0", | ||
"description": "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
@@ -22,3 +22,3 @@ "main": "./index.js", | ||
"hashish": "0.0.4", | ||
"mocha": "^2.1.0", | ||
"mocha": "2.1.0", | ||
"mocha-lcov-reporter": "0.0.1", | ||
@@ -25,0 +25,0 @@ "mocoverage": "^1.0.0" |
@@ -705,2 +705,54 @@ yargs | ||
.completion(cmd, [description], [fn]); | ||
------------- | ||
Enable bash-completion shortcuts for commands and options. | ||
`cmd`: when present in `argv._`, will result in the `.bashrc` completion script | ||
being outputted. To enable bash completions, concat the generated script to your | ||
`.bashrc`, or `.bash_profile`. | ||
`description`: provide a description in your usage instructions for the command | ||
that generates bash completion scripts. | ||
`fn`, rather than relying on yargs' default completion functionlity, which | ||
shiver me timbers is pretty awesome, you can provide your own completion | ||
method. | ||
```js | ||
var argv = require('yargs') | ||
.completion('completion', function(current, argv) { | ||
// 'current' is the current command being completed. | ||
// 'argv' is the parsed arguments so far. | ||
// simply return an array of completions. | ||
return [ | ||
'foo', | ||
'bar' | ||
]; | ||
}) | ||
.argv; | ||
``` | ||
But wait, there's more! you can provide asynchronous completions. | ||
```js | ||
var argv = require('yargs') | ||
.completion('completion', function(current, argv, done) { | ||
setTimeout(function() { | ||
done([ | ||
'apple', | ||
'banana' | ||
]); | ||
}, 500); | ||
}) | ||
.argv; | ||
``` | ||
.showCompletionScript() | ||
---------------------- | ||
Generate a bash completion script. Users of your application can install this | ||
script in their `.bashrc`, and yargs will provide completion shortcuts for | ||
commands and options. | ||
.exitProcess(enable) | ||
@@ -707,0 +759,0 @@ ---------------------------------- |
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
69358
8
1299
902
5