mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
things are in motion -- bin/node_coffee is the new JS-only command line ... it can pass some of the tests
This commit is contained in:
88
vendor/optparse-js/examples/nodejs-test.js
vendored
Normal file
88
vendor/optparse-js/examples/nodejs-test.js
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
// Import the optparse script
|
||||
var optparse = require('../src/optparse');
|
||||
|
||||
process.mixin(require("utils"));
|
||||
|
||||
// Define some options
|
||||
var SWITCHES = [
|
||||
['-i', '--include-file FILE', "Includes a file"],
|
||||
['-p', '--print [MESSAGE]', "Prints an optional message on screen"],
|
||||
['-d', '--debug', "Enables debug mode"],
|
||||
['-H', '--help', "Shows this help section"],
|
||||
['--date DATE', "A date. A date is expected E.G. 2009-01-14"],
|
||||
['--number NUMBER', "A Number. Supported formats are 123, 123.123, 0xA123"],
|
||||
['--other NAME', "No handler defined for this option. Will be handled by the wildcard handler."],
|
||||
];
|
||||
|
||||
// Create a new OptionParser with defined switches
|
||||
var parser = new optparse.OptionParser(SWITCHES), print_summary = true,
|
||||
first_arg;
|
||||
parser.banner = 'Usage: nodejs-test.js [options]';
|
||||
|
||||
// Internal variable to store options.
|
||||
var options = {
|
||||
debug: false,
|
||||
files: [],
|
||||
number: undefined,
|
||||
date: undefined
|
||||
};
|
||||
|
||||
// Handle the first argument (switches excluded)
|
||||
parser.on(0, function(value) {
|
||||
first_arg = value;
|
||||
});
|
||||
|
||||
// Handle the --include-file switch
|
||||
parser.on('include-file', function(value) {
|
||||
options.files.push(value);
|
||||
});
|
||||
|
||||
// Handle the --print switch
|
||||
parser.on('print', function(value) {
|
||||
puts('PRINT: ' + (value || 'No message entered'));
|
||||
});
|
||||
|
||||
// Handle the --date switch
|
||||
parser.on('date', function(value) {
|
||||
options.date = value;
|
||||
});
|
||||
|
||||
// Handle the --number switch
|
||||
parser.on('number', function(value) {
|
||||
options.number = value;
|
||||
});
|
||||
|
||||
// Handle the --debug switch
|
||||
parser.on('debug', function() {
|
||||
options.debug = true;
|
||||
});
|
||||
|
||||
// Handle the --help switch
|
||||
parser.on('help', function() {
|
||||
puts(parser.toString());
|
||||
print_summary = false;
|
||||
});
|
||||
|
||||
// Set a default handler
|
||||
parser.on('*', function(opt, value) {
|
||||
puts('wild handler for ' + opt + ', value=' + value);
|
||||
});
|
||||
|
||||
// Parse command line arguments
|
||||
parser.parse(process.ARGV);
|
||||
|
||||
if(print_summary) {
|
||||
puts("First non-switch argument is: " + first_arg);
|
||||
|
||||
// Output all files that was included.
|
||||
puts("No of files to include: " + options.files.length);
|
||||
for(var i = 0; i < options.files.length; i++) {
|
||||
puts("File [" + (i + 1) + "]:" + options.files[i]);
|
||||
}
|
||||
|
||||
// Is debug-mode enabled?
|
||||
puts("Debug mode is set to: " + options.debug);
|
||||
|
||||
puts("Number value is: " + options.number);
|
||||
puts("Date value is: " + options.date);
|
||||
}
|
||||
Reference in New Issue
Block a user