Skip to content

Commit

Permalink
played a little with nopt/commander options and basic completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mklabs committed Oct 18, 2011
1 parent cfb2894 commit c6fa6de
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
62 changes: 54 additions & 8 deletions bin/pkgrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ var completion = require('../lib/completion');

// this file is mainly just an example of usage

// test with nopt options
var noptions = require('../examples/nopt/nopt'),
opts = Object.keys(noptions.opts),
alias = Object.keys(noptions.alias),
all = opts.concat(alias);

// test with commander options
var program = require('../examples/commander/pizza'),
short = program.options.map(function(opt) { return opt.short; })
long = program.options.map(function(opt) { return opt.long; }),
commanderAll = short.concat(long);

// pass the module name, will change to module
// reference + dir walk to find a package.json
completion.complete('pkgrc', function(err, o) {
Expand All @@ -18,14 +30,48 @@ completion.complete('pkgrc', function(err, o) {
// Setting completion output is simply a matter of writing to the console
//

console.log(o.line);
console.log(o.partial);
console.log(o.words);
console.log(o.point);
console.log(o.last);
console.log(o.lastPartial);
// console.log(o.line);
// console.log(o.partial);
// console.log(o.words);
// console.log(o.point);
// console.log(o.last);
// console.log(o.lastPartial);

if(o.last === 'hello') {
return console.log('World');
}

if(o.last === 'js') {
return console.log('javascript');
}

// basic config - and -- catch up
if(o.last === '-' && o.prev !== 'commander') {
return log(alias, '-');
}

if(o.partial === 'hello') {
console.log('World');
if(o.last === '--' && o.prev !== 'commander') {
return log(opts, '--');
}

if(o.prev === 'config' || o.last === 'config') {
return log(all);
}

if(o.prev === 'commander') {
if(o.last === '--') return log(long);
return log(commanderAll);
}


console.log('line:' + o.line);
console.log('last:' + o.last);
console.log('prev: ' + o.prev);
});

function log(arr, prefix) {
prefix = prefix || '';
arr.forEach(function(v) {
console.log(prefix + v);
});
}
2 changes: 1 addition & 1 deletion examples/commander/custom-help
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

var program = require('../');
var program = require('commander');

function list(val) {
return val.split(',').map(Number);
Expand Down
2 changes: 1 addition & 1 deletion examples/commander/defaults
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

var program = require('../');
var program = require('commander');

function list(val) {
return val.split(',').map(Number);
Expand Down
2 changes: 1 addition & 1 deletion examples/commander/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

var program = require('../');
var program = require('commander');

program
.version('0.0.1')
Expand Down
4 changes: 2 additions & 2 deletions examples/commander/express
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

var program = require('../');
var program = require('commander');

program
.version('0.0.1')
Expand All @@ -15,4 +15,4 @@ program

console.log(' - sessions %j', program.sessions);
console.log(' - template %j', program.template);
console.log(' - css %j', program.css);
console.log(' - css %j', program.css);
25 changes: 13 additions & 12 deletions examples/commander/pizza
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
* Module dependencies.
*/

var program = require('../');
var program = require('commander');

program
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese <type>', 'Add the specified type of cheese [marble]')
.option('-C, --no-cheese', 'You do not want any cheese')
.parse(process.argv);
.option('-C, --no-cheese', 'You do not want any cheese');

console.log('you ordered a pizza with:');
if (program.peppers) console.log(' - peppers');
if (program.pineappe) console.log(' - pineapple');
if (program.bbq) console.log(' - bbq');

var cheese = true === program.cheese
? 'marble'
: program.cheese || 'no';
// expose programs so that we can play with commander options
module.exports = program;

console.log(' - %s cheese', cheese);
console.log(program.args);
//console.log('you ordered a pizza with:');
//if (program.peppers) console.log(' - peppers');
//if (program.pineappe) console.log(' - pineapple');
//if (program.bbq) console.log(' - bbq');

// var cheese = true === program.cheese ? 'marble' : program.cheese || 'no';
// console.log(' - %s cheese', cheese);

// console.log(program.args);
2 changes: 1 addition & 1 deletion examples/nopt/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ var nopt = require('nopt')
exports.opts = knownOpts
exports.alias = shortHands

console.log("parsed =\n"+ require("util").inspect(parsed, false, 2, true))
// console.log("parsed =\n"+ require("util").inspect(parsed, false, 2, true))
2 changes: 1 addition & 1 deletion lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.complete = function(name, cb) {
var partial = line.substr(0, point),
last = line.split(' ').slice(-1).join(''),
lastPartial = partial.split(' ').slice(-1).join(''),
prev = line.split(' ').slice(0, -1).slice(-1);
prev = line.split(' ').slice(0, -1).slice(-1)[0];

cb(null, {
line: line,
Expand Down

0 comments on commit c6fa6de

Please sign in to comment.