posix-getopt
Advanced tools
Comparing version 0.0.1 to 1.0.0
@@ -1,5 +0,5 @@ | ||
var mod_getopt = require('getopt') | ||
var mod_getopt = require('..'); | ||
var parser, option; | ||
console.error("Example 1: simple short options"); | ||
console.error('Example 1: simple short options'); | ||
parser = new mod_getopt.BasicParser('la', | ||
@@ -10,3 +10,3 @@ ['node', 'script', '-l', '-a', 'stuff']); | ||
console.error("Example 2: invalid option specified"); | ||
console.error('Example 2: invalid option specified'); | ||
parser = new mod_getopt.BasicParser('la', | ||
@@ -18,3 +18,3 @@ ['node', 'script', '-l', '-b', 'stuff']); | ||
console.error("Example 3: long options"); | ||
console.error('Example 3: long options'); | ||
parser = new mod_getopt.BasicParser('lar(recurse)', | ||
@@ -25,3 +25,3 @@ ['node', 'script', '-l', '--recurse', 'stuff']); | ||
console.error("Example 4: options with arguments"); | ||
console.error('Example 4: options with arguments'); | ||
parser = new mod_getopt.BasicParser('f:lad:', | ||
@@ -32,3 +32,3 @@ ['node', 'script', '-l', '-f', 'filename', '-dtype', 'stuff']); | ||
console.error("Example 5: options with missing arguments"); | ||
console.error('Example 5: options with missing arguments'); | ||
parser = new mod_getopt.BasicParser('f:la', | ||
@@ -40,3 +40,3 @@ ['node', 'script', '-l', '-a', '-f']); | ||
console.error("Example 6: options specified multiple times"); | ||
console.error('Example 6: options specified multiple times'); | ||
parser = new mod_getopt.BasicParser('la', | ||
@@ -43,0 +43,0 @@ ['node', 'script', '-l', '-a', '-l']); |
@@ -12,6 +12,6 @@ /* | ||
* 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 | ||
@@ -23,3 +23,3 @@ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* SOFTWARE. | ||
*/ | ||
@@ -32,3 +32,3 @@ | ||
return (new Error('getopt: ' + msg)); | ||
}; | ||
} | ||
@@ -44,6 +44,6 @@ /* | ||
ASSERT(optstring || optstring === '', "optstring is required"); | ||
ASSERT(optstring.constructor === String, "optstring must be a string"); | ||
ASSERT(argv, "argv is required"); | ||
ASSERT(argv.constructor === Array, "argv must be an array"); | ||
ASSERT(optstring || optstring === '', 'optstring is required'); | ||
ASSERT(optstring.constructor === String, 'optstring must be a string'); | ||
ASSERT(argv, 'argv is required'); | ||
ASSERT(argv.constructor === Array, 'argv must be an array'); | ||
@@ -58,3 +58,3 @@ this.gop_argv = new Array(argv.length); | ||
ASSERT(argv[ii].constructor === String, | ||
"argv must be string array"); | ||
'argv must be string array'); | ||
this.gop_argv[ii] = argv[ii]; | ||
@@ -174,8 +174,8 @@ } | ||
arg = this.gop_argv[this.gop_optind]; | ||
var arg = this.gop_argv[this.gop_optind]; | ||
if (this.gop_subind == 0) { | ||
if (this.gop_subind === 0) { | ||
if (arg == '-' || arg === '' || arg[0] != '-') | ||
return (undefined); | ||
if (arg == '--') { | ||
@@ -209,5 +209,2 @@ this.gop_optind++; | ||
if (!(chr in this.gop_options)) | ||
return (this.errInvalidOption(chr)); | ||
if (++this.gop_subind >= arg.length) { | ||
@@ -218,2 +215,5 @@ this.gop_optind++; | ||
if (!(chr in this.gop_options)) | ||
return (this.errInvalidOption(chr)); | ||
if (!this.gop_options[chr]) | ||
@@ -223,3 +223,3 @@ return ({ option: chr }); | ||
return (this.getoptArgument(chr)); | ||
} | ||
}; | ||
@@ -249,3 +249,3 @@ /* | ||
return (this.errExtraArg(alias)); | ||
this.gop_optind++; /* eat this argument */ | ||
@@ -288,6 +288,6 @@ return ({ option: chr }); | ||
if (this.gop_silent) | ||
return ({ option: '?', optopt: chr }); | ||
return ({ option: ':', optopt: chr }); | ||
process.stderr.write('option requires an argument -- ' + chr + '\n'); | ||
return ({ option: ':', optopt: chr, error: true }); | ||
return ({ option: '?', optopt: chr, error: true }); | ||
}; | ||
@@ -294,0 +294,0 @@ |
{ | ||
"name": "posix-getopt", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "POSIX-style getopt()", | ||
@@ -5,0 +5,0 @@ "author": "Dave Pacheco (dap@cs.brown.edu)", |
257
README.md
@@ -8,10 +8,103 @@ | ||
node-getopt is a Node.js module providing an interface to the POSIX-defined | ||
getopt() function, a general-purpose command line parser that follows the POSIX | ||
node-getopt implements the POSIX getopt() function for Node. getopt() provides | ||
a functional interface for option parsing. | ||
Install the npm package in the usual way: | ||
$ npm install posix-getopt | ||
Here's how you'd typically use it for a command that takes options "-a" and | ||
"-b" with no arguments, option "-o" (also called "--output") with one argument, | ||
and another mandatory argument: | ||
var mod_getopt = require('posix-getopt'); | ||
var parser, option; | ||
parser = new mod_getopt.BasicParser('abo:(output)', process.argv); | ||
while ((option = parser.getopt()) !== undefined) { | ||
switch (option.option) { | ||
case 'a': | ||
console.log('option "a" is set'); | ||
break; | ||
case 'b': | ||
console.log('option "b" is set'); | ||
break; | ||
case 'o': | ||
console.error('option "o" has value "%s"', | ||
option.optarg); | ||
break; | ||
default: | ||
/* error message already emitted by getopt */ | ||
mod_assert.equal('?', option.option); | ||
break; | ||
} | ||
} | ||
if (parser.optind() >= process.argv.length) | ||
usage('missing required argument: "input"'); | ||
console.log('input = %s', process.argv[parser.optind()]); | ||
Examples: | ||
$ cmd | ||
error: missing required argument: "input" | ||
usage: cmd [-ab] [-o file] input | ||
$ cmd foo | ||
input = foo | ||
$ cmd -a foo | ||
option "a" is set | ||
input = foo | ||
$ cmd -ba foo | ||
option "b" is set | ||
option "a" is set | ||
input = foo | ||
$ cmd -ba -obar foo | ||
option "b" is set | ||
option "a" is set | ||
option "o" has value "bar" | ||
input = foo | ||
$ cmd -ba --output=bar foo | ||
option "b" is set | ||
option "a" is set | ||
option "o" has value "bar" | ||
input = foo | ||
$ cmd --output= foo | ||
option "o" has value "" | ||
input = foo | ||
$ cmd -o | ||
option requires an argument -- o | ||
error: missing required argument: "input" | ||
usage: cmd [-ab] [-o file] input | ||
$ cmd -- -a | ||
input = -a | ||
$ cmd -q | ||
illegal option -- q | ||
error: missing required argument: "input" | ||
usage: cmd [-ab] [-o file] input | ||
Background | ||
-------- | ||
getopt() is a general-purpose command line parser that follows the POSIX | ||
guidelines for command-line utilities. Using these guidelines encourages | ||
common conventions among applications, including use of: | ||
o short option names (e.g., "-r") | ||
o options with arguments (e.g., "-f filename or -ffilename") | ||
o chaining short option names when options have no arguments (e.g., "-ra") | ||
- short option names (e.g., "-r") | ||
- options with arguments (e.g., "-f filename or -ffilename") | ||
- chaining short option names when options have no arguments (e.g., "-ra") | ||
@@ -39,27 +132,6 @@ This implementation mirrors the Solaris getopt() implementation and supports | ||
This module is considered complete except that | ||
This module is considered complete except that there's minimal automated test | ||
coverage. There are no known bugs. | ||
o test coverage is pretty minimal | ||
o npm install has not been tested | ||
o the source is not javascriptlint-clean | ||
There are no known bugs, but the module has not been extensively tested. | ||
Platforms | ||
--------- | ||
This module should work on all platforms that support node.js. The module is | ||
tested on MacOS X 10.6.5 and OpenSolaris based on build 111b and Illumos build | ||
147. | ||
Installation | ||
------------ | ||
As an npm package, node-getopt is installed in the usual way: | ||
% npm install posix-getopt | ||
API | ||
@@ -85,9 +157,10 @@ --- | ||
Example option strings: | ||
':r' Command takes one option with no args: -r | ||
':ra' Command takes two option with no args: -r and -a | ||
':raf:' Command takes two option with no args: -r and -a | ||
and a single option that takes an arg: -f | ||
':f:(file)' Command takes a single option with an argument: -f | ||
-f can also be specified as --file | ||
':r' Command takes one option with no args: -r | ||
':ra' Command takes two option with no args: -r and -a | ||
':raf:' Command takes two option with no args: -r and -a | ||
and a single option that takes an arg: -f | ||
':f:(file)' Command takes a single option with an argument: -f | ||
-f can also be specified as --file | ||
The presence of a leading colon in the option string determines the behavior | ||
@@ -132,27 +205,27 @@ when an argument is not specified for an option which takes an argument. See | ||
o If the end of command line arguments is reached, an undefined value is | ||
returned. The end of arguments is signified by a single '-' argument, a | ||
single '--' argument, an argument that's neither an option nor a previous | ||
option's argument, the end of argv, or an error. | ||
- If the end of command line arguments is reached, an undefined value is | ||
returned. The end of arguments is signified by a single '-' argument, a | ||
single '--' argument, an argument that's neither an option nor a previous | ||
option's argument, the end of argv, or an error. | ||
o If an unrecognized command line option is found (i.e. an option character | ||
not defined in "optstring"), the returned object's "option" member | ||
is just "?". "optopt" is set to the unrecognized option letter. "error" | ||
is set to a true value. | ||
- If an unrecognized command line option is found (i.e. an option character | ||
not defined in "optstring"), the returned object's "option" member | ||
is just "?". "optopt" is set to the unrecognized option letter. "error" | ||
is set to a true value. | ||
o If a known command line option is found and the option takes no arguments | ||
then the returned object's "option" member is the option's short name | ||
(i.e. the single character specifier in "optstring"). | ||
- If a known command line option is found and the option takes no arguments | ||
then the returned object's "option" member is the option's short name | ||
(i.e. the single character specifier in "optstring"). | ||
o If a known command line option is found and that option takes an argument | ||
and the argument is also found, then the returned object's "option" | ||
member is the option's short name and the "optarg" member contains the | ||
argument's value. | ||
- If a known command line option is found and that option takes an argument | ||
and the argument is also found, then the returned object's "option" | ||
member is the option's short name and the "optarg" member contains the | ||
argument's value. | ||
o If a known command line option is found and that option takes an argument | ||
but the argument is not found, then the returned object's "option" member | ||
is "?" unless the first character of "optstring" was a colon, in which | ||
case the "option" member is set to ":". Either way, the "optopt" member | ||
is set to the option character that caused the error and "error" is set to | ||
a true value. | ||
- If a known command line option is found and that option takes an argument | ||
but the argument is not found, then the returned object's "option" member | ||
is "?" unless the first character of "optstring" was a colon, in which | ||
case the "option" member is set to ":". Either way, the "optopt" member | ||
is set to the option character that caused the error and "error" is set to | ||
a true value. | ||
@@ -163,22 +236,22 @@ | ||
o Global state in the C implementation (e.g., optind, optarg, and optopt) is | ||
encapsulated in the BasicParser object. optind is available as a method | ||
call on the parser object. optarg and optopt are returned directly by | ||
getopt(). | ||
- Global state in the C implementation (e.g., optind, optarg, and optopt) is | ||
encapsulated in the BasicParser object. optind is available as a method | ||
call on the parser object. optarg and optopt are returned directly by | ||
getopt(). | ||
o Rather than returning an integer or character, getopt() returns an object | ||
with the "option" field corresponding to the processed option character | ||
and possibly the additional "optarg" and "optopt" fields. If an error | ||
occurs on a particular option, "error" is also set. If an error occurs on | ||
no particular option or if the end of input is encountered, undefined is | ||
returned. | ||
- Rather than returning an integer or character, getopt() returns an object | ||
with the "option" field corresponding to the processed option character | ||
and possibly the additional "optarg" and "optopt" fields. If an error | ||
occurs on a particular option, "error" is also set. If an error occurs on | ||
no particular option or if the end of input is encountered, undefined is | ||
returned. | ||
o Long option forms are supported as described above. This introduces an | ||
additional error case which is where an argument of the form | ||
--option=value is encountered, where "option" does not take a value. | ||
- Long option forms are supported as described above. This introduces an | ||
additional error case which is where an argument of the form | ||
--option=value is encountered, where "option" does not take a value. | ||
o POSIX starts "optind" at 1, since argv[0] is generally the name of the | ||
command and options start at argv[1]. This implementation starts "optind" | ||
at 2, since argv[0] is generally the path to the node binary and argv[1] | ||
is the path to the script, so options start with argv[2]. | ||
- POSIX starts "optind" at 1, since argv[0] is generally the name of the | ||
command and options start at argv[1]. This implementation starts "optind" | ||
at 2, since argv[0] is generally the path to the node binary and argv[1] | ||
is the path to the script, so options start with argv[2]. | ||
@@ -199,2 +272,8 @@ | ||
outputs: | ||
{ option: 'l' } | ||
{ option: 'a' } | ||
### Example 2: invalid option specified | ||
@@ -211,2 +290,9 @@ | ||
outputs: | ||
{ option: 'l' } | ||
illegal option -- b | ||
{ option: '?', optopt: 'b', error: true } | ||
### Example 3: long options | ||
@@ -222,2 +308,8 @@ | ||
outputs: | ||
{ option: 'l' } | ||
{ option: 'r' } | ||
### Example 4: options with arguments | ||
@@ -233,2 +325,9 @@ | ||
outputs: | ||
{ option: 'l' } | ||
{ option: 'f', optarg: 'filename' } | ||
{ option: 'd', optarg: 'type' } | ||
### Example 5: options with missing arguments | ||
@@ -245,2 +344,10 @@ | ||
outputs: | ||
{ option: 'l' } | ||
{ option: 'a' } | ||
option requires an argument -- f | ||
{ option: '?', optopt: 'f', error: true } | ||
### Example 6: options specified multiple times | ||
@@ -255,1 +362,7 @@ | ||
console.error(option); | ||
outputs: | ||
{ option: 'l' } | ||
{ option: 'a' } | ||
{ option: 'l' } |
@@ -5,6 +5,5 @@ /* | ||
var mod_path = require('path'); | ||
require.paths.unshift(mod_path.dirname(__dirname) + '/lib'); | ||
var mod_sys = require('sys'); | ||
var mod_getopt = require('getopt'); | ||
var mod_getopt = require('..'); | ||
var mod_assert = require('assert'); | ||
@@ -11,0 +10,0 @@ |
@@ -5,6 +5,5 @@ /* | ||
var mod_path = require('path'); | ||
require.paths.unshift(mod_path.dirname(__dirname) + '/lib'); | ||
var mod_sys = require('sys'); | ||
var mod_getopt = require('getopt'); | ||
var mod_getopt = require('..'); | ||
var mod_assert = require('assert'); | ||
@@ -11,0 +10,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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
26922
8
483
1
358
0
1