Comparing version 1.0.1 to 1.0.3
// Import the optparse script | ||
require.paths.unshift(__dirname); //make local paths accessible | ||
var optparse = require('../lib/optparse'); | ||
var optparse = require('lib/optparse'); | ||
var sys= require('sys'); | ||
// Define some options | ||
@@ -38,3 +34,3 @@ var SWITCHES = [ | ||
// Handle the --include-file switch | ||
parser.on('include-file', function(value) { | ||
parser.on('include-file', function(name, value) { | ||
options.files.push(value); | ||
@@ -44,8 +40,8 @@ }); | ||
// Handle the --print switch | ||
parser.on('print', function(value) { | ||
sys.puts('PRINT: ' + (value || 'No message entered')); | ||
parser.on('print', function(name, value) { | ||
console.log('PRINT: ' + (value || 'No message entered')); | ||
}); | ||
// Handle the --date switch | ||
parser.on('date', function(value) { | ||
parser.on('date', function(name, value) { | ||
options.date = value; | ||
@@ -55,3 +51,3 @@ }); | ||
// Handle the --number switch | ||
parser.on('number', function(value) { | ||
parser.on('number', function(name, value) { | ||
options.number = value; | ||
@@ -67,3 +63,3 @@ }); | ||
parser.on('help', function() { | ||
sys.puts(parser.toString()); | ||
console.log(parser.toString()); | ||
print_summary = false; | ||
@@ -74,3 +70,3 @@ }); | ||
parser.on('*', function(opt, value) { | ||
sys.puts('wild handler for ' + opt + ', value=' + value); | ||
console.log('wild handler for ' + opt + ', value=' + value); | ||
}); | ||
@@ -82,15 +78,15 @@ | ||
if(print_summary) { | ||
sys.puts("First non-switch argument is: " + first_arg); | ||
console.log("First non-switch argument is: " + first_arg); | ||
// Output all files that was included. | ||
sys.puts("No of files to include: " + options.files.length); | ||
console.log("No of files to include: " + options.files.length); | ||
for(var i = 0; i < options.files.length; i++) { | ||
sys.puts("File [" + (i + 1) + "]:" + options.files[i]); | ||
console.log("File [" + (i + 1) + "]:" + options.files[i]); | ||
} | ||
// Is debug-mode enabled? | ||
sys.puts("Debug mode is set to: " + options.debug); | ||
console.log("Debug mode is set to: " + options.debug); | ||
sys.puts("Number value is: " + options.number); | ||
sys.puts("Date value is: " + options.date); | ||
} | ||
console.log("Number value is: " + options.number); | ||
console.log("Date value is: " + options.date); | ||
} |
@@ -1,11 +0,11 @@ | ||
// Optparse.js 1.0.2 - Option Parser for Javascript | ||
// | ||
// Optparse.js 1.0.3 - Option Parser for Javascript | ||
// | ||
// Copyright (c) 2009 Johan Dahlberg | ||
// | ||
// | ||
// See README.md for license. | ||
// | ||
// | ||
var optparse = {}; | ||
try{ optparse = exports } catch(e) {}; // Try to export the lib for node.js | ||
(function(self) { | ||
var VERSION = '1.0.2'; | ||
var VERSION = '1.0.3'; | ||
var LONG_SWITCH_RE = /^--\w/; | ||
@@ -20,3 +20,3 @@ var SHORT_SWITCH_RE = /^-\w/; | ||
// The default switch argument filter to use, when argument name doesnt match | ||
// any other names. | ||
// any other names. | ||
var DEFAULT_FILTER = '_DEFAULT'; | ||
@@ -30,7 +30,7 @@ var PREDEFINED_FILTERS = {}; | ||
// Switch argument filter that expects an integer, HEX or a decimal value. An | ||
// exception is throwed if the criteria is not matched. | ||
// Switch argument filter that expects an integer, HEX or a decimal value. An | ||
// exception is throwed if the criteria is not matched. | ||
// Valid input formats are: 0xFFFFFFF, 12345 and 1234.1234 | ||
function filter_number(value) { | ||
var m = NUMBER_RE(value); | ||
var m = NUMBER_RE.exec(value); | ||
if(m == null) throw OptError('Expected a number representative'); | ||
@@ -41,3 +41,3 @@ if(m[1]) { | ||
} else { | ||
// The number is in regular- or decimal form. Just run in through | ||
// The number is in regular- or decimal form. Just run in through | ||
// the float caster. | ||
@@ -49,6 +49,6 @@ return parseFloat(m[2] || m[3]); | ||
// Switch argument filter that expects a Date expression. The date string MUST be | ||
// formated as: "yyyy-mm-dd" An exception is throwed if the criteria is not | ||
// matched. An DATE object is returned on success. | ||
// formated as: "yyyy-mm-dd" An exception is throwed if the criteria is not | ||
// matched. An DATE object is returned on success. | ||
function filter_date(value) { | ||
var m = DATE_RE(value); | ||
var m = DATE_RE.exec(value); | ||
if(m == null) throw OptError('Expected a date representation in the "yyyy-mm-dd" format.'); | ||
@@ -59,5 +59,5 @@ return new Date(parseInt(m[0]), parseInt(m[1]), parseInt(m[2])); | ||
// Switch argument filter that expects an email address. An exception is throwed | ||
// if the criteria doesn`t match. | ||
// if the criteria doesn`t match. | ||
function filter_email(value) { | ||
var m = EMAIL_RE(value); | ||
var m = EMAIL_RE.exec(value); | ||
if(m == null) throw OptError('Excpeted an email address.'); | ||
@@ -67,5 +67,5 @@ return m[1]; | ||
// Register all predefined filters. This dict is used by each OptionParser | ||
// instance, when parsing arguments. Custom filters can be added to the parser | ||
// instance by calling the "add_filter" -method. | ||
// Register all predefined filters. This dict is used by each OptionParser | ||
// instance, when parsing arguments. Custom filters can be added to the parser | ||
// instance by calling the "add_filter" -method. | ||
PREDEFINED_FILTERS[DEFAULT_FILTER] = filter_text; | ||
@@ -78,3 +78,3 @@ PREDEFINED_FILTERS['TEXT'] = filter_text; | ||
// Buildes rules from a switches collection. The switches collection is defined | ||
// when constructing a new OptionParser object. | ||
// when constructing a new OptionParser object. | ||
function build_rules(filters, arr) { | ||
@@ -90,3 +90,3 @@ var rules = []; | ||
case 2: | ||
var expr = LONG_SWITCH_RE(r[0]) ? 0 : 1; | ||
var expr = LONG_SWITCH_RE.test(r[0]) ? 0 : 1; | ||
var alias = expr == 0 ? -1 : 0; | ||
@@ -108,4 +108,4 @@ var desc = alias == -1 ? 1 : -1; | ||
// Builds a rule with specified expression, short style switch and help. This | ||
// function expects a dict with filters to work correctly. | ||
// Builds a rule with specified expression, short style switch and help. This | ||
// function expects a dict with filters to work correctly. | ||
// | ||
@@ -119,4 +119,4 @@ // Return format: | ||
// optional_arg Indicates that switch argument is optional | ||
// filter The filter to use when parsing the arg. An | ||
// <<undefined>> value means that the switch does | ||
// filter The filter to use when parsing the arg. An | ||
// <<undefined>> value means that the switch does | ||
// not take anargument. | ||
@@ -131,3 +131,3 @@ function build_rule(filters, short, expr, desc) { | ||
// then find a filter that suites. | ||
var optional_match = ARG_OPTIONAL_RE(m[2]); | ||
var optional_match = ARG_OPTIONAL_RE.test(m[2]); | ||
var filter_name = optional_match === null ? m[2] : optional_match[1]; | ||
@@ -139,9 +139,9 @@ optional = optional_match !== null; | ||
return { | ||
name: long.substr(2), | ||
short: short, | ||
name: long.substr(2), | ||
short: short, | ||
long: long, | ||
decl: expr, | ||
desc: desc, | ||
desc: desc, | ||
optional_arg: optional, | ||
filter: filter | ||
filter: filter | ||
} | ||
@@ -151,3 +151,3 @@ } | ||
// Loop's trough all elements of an array and check if there is valid | ||
// options expression within. An valid option is a token that starts | ||
// options expression within. An valid option is a token that starts | ||
// double dashes. E.G. --my_option | ||
@@ -157,3 +157,3 @@ function contains_expr(arr) { | ||
var l = arr.length; | ||
while(l-- > 0) if(LONG_SWITCH_RE(arr[l])) return true; | ||
while(l-- > 0) if(LONG_SWITCH_RE.test(arr[l])) return true; | ||
return false; | ||
@@ -175,3 +175,3 @@ } | ||
if(arg1.constructor === Number) { | ||
l = arg1; | ||
l = arg1; | ||
} else { | ||
@@ -216,3 +216,3 @@ if(arg1.length == arg2) return arg1; | ||
OptionParser.prototype = { | ||
// Adds args and switchs handler. | ||
@@ -228,12 +228,12 @@ on: function(value, fn) { | ||
}, | ||
// Adds a custom filter to the parser. It's possible to override the | ||
// default filter by passing the value "_DEFAULT" to the ´´name´´ | ||
// argument. The name of the filter is automatically transformed into | ||
// upper case. | ||
// argument. The name of the filter is automatically transformed into | ||
// upper case. | ||
filter: function(name, fn) { | ||
this.filters[name.toUpperCase()] = fn; | ||
}, | ||
// Parses specified args. Returns remaining arguments. | ||
// Parses specified args. Returns remaining arguments. | ||
parse: function(args) { | ||
@@ -244,6 +244,6 @@ var result = [], callback; | ||
while((token = tokens.shift()) && this._halt == false) { | ||
if(LONG_SWITCH_RE(token) || SHORT_SWITCH_RE(token)) { | ||
if(LONG_SWITCH_RE.test(token) || SHORT_SWITCH_RE.test(token)) { | ||
var arg = undefined; | ||
// The token is a long or a short switch. Get the corresponding | ||
// rule, filter and handle it. Pass the switch to the default | ||
// The token is a long or a short switch. Get the corresponding | ||
// rule, filter and handle it. Pass the switch to the default | ||
// handler if no rule matched. | ||
@@ -255,3 +255,3 @@ for(var i = 0; i < rules.length; i++) { | ||
arg = tokens.shift(); | ||
if(!LONG_SWITCH_RE(arg) && !SHORT_SWITCH_RE(arg)) { | ||
if(!LONG_SWITCH_RE.test(arg) && !SHORT_SWITCH_RE.test(arg)) { | ||
try { | ||
@@ -267,3 +267,3 @@ arg = rule.filter(arg); | ||
} | ||
} | ||
} | ||
callback = this.on_switches[rule.name]; | ||
@@ -273,7 +273,7 @@ if (!callback) callback = this.on_switches['*']; | ||
break; | ||
} | ||
} | ||
} | ||
if(i == rules.length) this.default_handler.apply(this, [token]); | ||
} else { | ||
// Did not match long or short switch. Parse the token as a | ||
// Did not match long or short switch. Parse the token as a | ||
// normal argument. | ||
@@ -287,4 +287,4 @@ callback = this.on_args[result.length]; | ||
}, | ||
// Returns an Array with all defined option rules | ||
// Returns an Array with all defined option rules | ||
options: function() { | ||
@@ -294,3 +294,3 @@ return build_rules(this.filters, this._rules); | ||
// Add an on_halt callback if argument ´´fn´´ is specified. on_switch handlers can | ||
// Add an on_halt callback if argument ´´fn´´ is specified. on_switch handlers can | ||
// call instance.halt to abort the argument parsing. This can be useful when | ||
@@ -302,6 +302,6 @@ // displaying help or version information. | ||
}, | ||
// Returns a string representation of this OptionParser instance. | ||
toString: function() { | ||
var builder = [this.banner, '', this.options_title], | ||
var builder = [this.banner, '', this.options_title], | ||
shorts = false, longest = 0, rule; | ||
@@ -311,3 +311,3 @@ var rules = build_rules(this.filters, this._rules); | ||
rule = rules[i]; | ||
// Quick-analyze the options. | ||
// Quick-analyze the options. | ||
if(rule.short) shorts = true; | ||
@@ -317,3 +317,3 @@ if(rule.decl.length > longest) longest = rule.decl.length; | ||
for(var i = 0; i < rules.length; i++) { | ||
var text; | ||
var text; | ||
rule = rules[i]; | ||
@@ -335,2 +335,2 @@ if(shorts) { | ||
})(optparse); | ||
})(optparse); |
@@ -6,4 +6,4 @@ { | ||
"keywords": ["option", "parser", "command-line", "cli", "terminal"], | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"main": "./lib/optparse" | ||
} |
@@ -93,5 +93,7 @@ optparse-js | ||
And this example show how to hook an argument (an option without the leading - or --): | ||
And this example show how to hook a positional argument (an option without the leading - or --). | ||
Note that positional argument 0 will be "node" and positional argument 1 will be the path of the | ||
script being run. Positional argument 2 will be the first positional argument after the script path: | ||
parser.on(0, function(opt) { | ||
parser.on(2, function(opt) { | ||
puts('The first non-switch option is:' + opt); | ||
@@ -98,0 +100,0 @@ }); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
22458
164
0
1