Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 14, 2011
0 parents commit 672c7d0
Show file tree
Hide file tree
Showing 17 changed files with 619 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
*.sock
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
support
test
examples
*.sock
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

0.0.1 / 2010-01-03
==================

* Initial release
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

TESTS = $(shell find test/test.*.js)

test:
@./test/run $(TESTS)

.PHONY: test
29 changes: 29 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

# commander

the complete solution for node.js command-line programs

## License

(The MIT License)

Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions examples/coercion
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

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

function range(val) {
return val.split('..').map(Number);
}

function list(val) {
return val.split(',');
}

program
.version('0.0.1')
.description('Pizza configurator')
.option('-i, --integer <n>', 'An integer argument', parseInt)
.option('-f, --float <n>', 'A float argument', parseFloat)
.option('-r, --range <a>..<b>', 'A range', range)
.option('-l, --list <items>', 'A list', list)
.option('-o, --optional [value]', 'An optional value')
.parse(process.argv);

console.log(' int: %j', program.integer);
console.log(' float: %j', program.float);
console.log(' optional: %j', program.optional);
program.range = program.range || [];
console.log(' range: %j..%j', program.range[0], program.range[1]);
console.log(' list: %j', program.list);
console.log(' args: %j', program.args);
39 changes: 39 additions & 0 deletions examples/deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

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

program
.version('0.0.1')
.description('application deployer')
.option('-C, --chdir <path>', 'change the working directory')
.option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
.option('-T, --no-tests', 'ignore test hook')

program
.command('setup [env]')
.description('run remote setup commands')
.action(function(){
console.log('setup');
});

program
.command('exec <cmd>')
.description('run the given remote command')
.action(function(cmd){
console.log('exec "%s"', cmd);
});

program
.command('*')
.description('deploy the given env')
.action(function(env){
console.log('deploying "%s"', env);
});

program.parse(process.argv);


15 changes: 15 additions & 0 deletions examples/express
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

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

program
.version('0.0.1')
.description('Express application generator')
.option('-s, --sessions', 'add session support')
.option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]')
.option('-c, --css <engine>', 'specify stylesheet engine (stylus|sass|less) [css]')
.parse(process.argv);
81 changes: 81 additions & 0 deletions examples/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

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

// ask('bool?')
// ask('Username: ')
// ask('Password: ')
// ask('Description:')
// ask('Birthday: ', Date)
// ask('Age: ', Number)
// ask('Fav colors: ', Array)
// askForArray('Fav colors: ')
// choose(options)

program.askForNumber = function(str, fn){
this.askSingleLine(str, function(val){
val = Number(val);
if (isNaN(val)) return program.askForNumber(str + '(must be a number) ', fn);
fn(val);
});
};

program.askForDate = function(str, fn){
this.askSingleLine(str, function(val){
val = new Date(val);
if (isNaN(val.getTime())) return program.askForDate(str + '(must be a date) ', fn);
fn(val);
});
};

program.askSingleLine = function(str, fn){
if ('function' == typeof arguments[2]) {
return this['askFor' + (fn.name || fn)](str, arguments[2]);
}

process.stdout.write(str);
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(val){
fn(val);
}).resume();
};

program.askMultiLine = function(str, fn){
var buf = '';
console.log(str);
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(val){
if ('\n' == val) {
process.stdin.removeAllListeners('data');
fn(buf);
} else {
buf += val;
}
}).resume();
};

program.ask = function(str, fn){
if (/ $/.test(str)) return this.askSingleLine.apply(this, arguments);
this.askMultiLine(str, fn);
};

program.ask('Username: ', function(name){
console.log('hi %s', name);

program.ask('Description:', function(desc){
console.log('description was "%s"', desc.trim());

program.ask('Age: ', Number, function(age){
console.log('age: %j', age);

program.ask('Birthdate: ', Date, function(date){
console.log('date: %s', date);
process.stdin.destroy();
});
});
});
});
28 changes: 28 additions & 0 deletions examples/pizza
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

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

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

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

var cheese = false === program.cheese
? 'no'
: program.cheese || 'cheddar';

console.log(' - %s cheese', cheese);
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require('./lib/commander');
Loading

0 comments on commit 672c7d0

Please sign in to comment.