jiangmiao / node-getopt Goto Github PK
View Code? Open in Web Editor NEWFeatured command line options parser.
Home Page: https://npmjs.org/package/node-getopt
License: MIT License
Featured command line options parser.
Home Page: https://npmjs.org/package/node-getopt
License: MIT License
There is a bad value in the package.json
file that causes the application to print an error message at every start:
(node:8) [DEP0128] DeprecationWarning: Invalid 'main' field in '/usr/local/lib/node_modules/@alo5/availability/node_modules/node-getopt/package.json' of './lib'. Please either fix that or report it to the module author
--
(Use `node --trace-deprecation ...` to show where the warning was created)
When used in a docker container, this error message is not printed on STDERR
but redirected to STDOUT
causing side effect when it is desired to redirect semantically relevant output to a file.
node simple.js -S'one two tree'
getopt.toffee(133):
argv.unshift short_names.slice(i+1)
replace to:
argv.unshift arg.slice(i+2)
P.S. And thank you very much for this very useful command line options parser
Currently this library does not have good error messages by default. For example, the example program:
const getopt = require('node-getopt');
const info = getopt.create([
['t', 'test=ARG', 'Test']
]).bindHelp().parseSystem();
This has a few problems that result in poor error messages:
./example.js --test
results in: option test need argument
; The argument I used is "--test" not "test", this is what should be reported to the user. This is what GNU "ls" returns: ls: option '--format' requires an argument
./example -t
results in: option test need argument
; The argument I used is "-t" not "test" (or even "--test"), this is what should be reported to the user. This is what GNU "ls" returns: ls: option requires an argument -- 'I'
Further, there is no capability to fix this without modifying the library because the error()
method is passed in an Error object with a string as its error, which does not contain enough information to determine what the error should actually be. This is poor extensibility.
It'll be good to have 👍
Best I can tell there is no way to specify that a short option should have an argument. I've tried the following:
This errors:
opts = getopt.create([
["d=ARG"]
}).parseSystem();
This is what I gather from the documentation:
opts = getopt.create([
["d", "="]
}).parseSystem();
This is literally copying the documentation:
opts = getopt.create([
["S", "="]
}).parseSystem();
With each of these it lets me pass in -d
(or -S
in the last one), but any attempts to specify -d=bla
or -d bla
get ignored and querying opts["d"]
is undefined.
Would be nice to have it.
Ability to make options required:
Example: the --tel option is required (not just the argument)
myapp.js --tel=555-555-1234
Also required ARGs should remove 1st = and alltrim(spaces) of argument
e.g. myapp.js tel: '=555-555-1234'
Also a required ARG should not take the next option as an ARG
e.g.
myapp.s --tel -v
Currently it accepts it as an argument shows {tel: '-v'}
of course this could cause an issue if someone wants an argument that starts with '-' or '--'
so maybe it can be escaped (or they can use =)
E.g. myapp.js --tel=-v
Thanks!
Currently lines are wrapped, but it would be sweet if we fit to the terminal window and indent the wrapped lines.
The only way to get the terminal size is listed below (according to toby)
function getTermSize(cb){
require('child_process').spawn('resize').stdout.on('data', function(data){
data = String(data)
var lines = data.split('\n'),
cols = Number(lines[0].match(/^COLUMNS=([0-9]+);$/)[1]),
lines = Number(lines[1].match(/^LINES=([0-9]+);$/)[1])
if (cb)
cb(cols, lines)
})
}
We could then determine how much padding is required.
For code
opt = require('..').create([
['h' , 'help' , 'display this help'],
])
.bindHelp()
.parseSystem();
Getopt object is lost. If you want to call something like opt.showHelp()
, you have to split the code.
So returns Getopt instead of ParsedOption to solve the issue.
Sorry, #1 wasn't fixed completely.
node simple.js -S"<?one?> two three"
the option -S should be last in arguments.
getopt.toffee(132) need to replace from:
if i < short_names.length - 1
to:
if i < arg.length - 1
require('node-getopt').create([
['c', 'constructor', 'constructor'],
]);
... node_modules\node-getopt\lib\getopt.js:104
throw new Error("option " + long_name + " redefined.");
^
thanks.
It would be helpful if I could access the command-name (the basename of process.argv[1]
) from the opts structure, so I can use it in other error messages. Something like:
opt.parseSystem();
if (!parseFile(opt.argv[0])) {
console.error(`${opt.command}: could not parse file ${opt.argv[0]}`);
process.exit(1);
}
Say I have distinct arguments whose order matters. For example:
-A «glob» Add files matching the glob to the accumulator.
-R «glob» Remove files matching the glob from the accumulator.
So that you might do something like:
./my-program -A 'dir1/**' -R '*~' -A '**.txt'
For my-program
to correctly interpret the arguments, it needs to know that -A
was specified, then -R
, and then -A
with a different value. I saw the Getopt.on()
which, based on its name, I thought might exhibit the sort of behavior of calling callbacks when encountering options. But it appears that it doesn’t support this sort of thing. Also, the multiple option support doesn’t let you know that the -R
was between the two -A
.
So, I was wondering, could you add a SAX-like Getopt.onParse()
streaming-like API which would allow the program to handle arguments as they are parsed to handle situations when order matters? I think this would add a lot of flexibility and probably not be too hard to implement. If this sounds good, I could see about making a PR.
The error when a long-option is missing an argument should read
- throw new Error("option " + option.long_name + " need argument"); + throw new Error("option " + option.long_name + " needs argument");
The help message that's autogenerated from the options is of the form
Usage:
node myprog [OPTION]
Options:
-v, --verbose Verbose mode
[etc.]
But often the usage of myprog includes other, non-option, arguments, such as a filename to read. So I'd like the first part to read (say) Usage: node myprog [OPTION] <filename>
.
It would be nice to have a simpler way to do this than by copying the default this.help
value from the source, modifying it as needed and feeding it to setHelp
.
Maybe something like a setHelpExtraArgs('<filename>')
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.