Giter VIP home page Giter VIP logo

node-getopt's People

Contributors

jiangmiao avatar nickcarneiro avatar reiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-getopt's Issues

Bad value in package.json

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.

short option spaced value parsing bug

how to check:

node simple.js -S'one two tree'

how to fix:

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

Poor error messages, poor extensibility

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:

  • Using it as ./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
  • Using it as ./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.

Short options with argument?

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.

Improvements to options parsing

  1. Ability to make options required:
    Example: the --tel option is required (not just the argument)
    myapp.js --tel=555-555-1234

  2. Also required ARGs should remove 1st = and alltrim(spaces) of argument
    e.g. myapp.js tel: '=555-555-1234'

  3. 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!

Fit to terminal

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.

parse returns Getopt instead of ParsedOption

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.

last short option spaced value parsing bug

Sorry, #1 wasn't fixed completely.

Test

node simple.js -S"<?one?> two three"

the option -S should be last in arguments.

Fix

getopt.toffee(132) need to replace from:

if i < short_names.length - 1

to:

if i < arg.length - 1

the 'constructor' option generate an exception

require('node-getopt').create([
['c', 'constructor', 'constructor'],
]);
... node_modules\node-getopt\lib\getopt.js:104
throw new Error("option " + long_name + " redefined.");
^

thanks.

Access to command name

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);
}

Support a stream-style interface to handle ordered arguments

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.

Tiny grammar nit

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");

Way to emit help message that includes non-option arguments

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>')?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.