Skip to content

Commit

Permalink
Merge branch 'TheRoSS-master' into release/3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jun 23, 2019
2 parents 143cc84 + e8b463d commit ad6cf12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
22 changes: 16 additions & 6 deletions index.js
Expand Up @@ -470,7 +470,8 @@ Command.prototype.parse = function(argv) {
}

// process argv
var parsed = this.parseOptions(this.normalize(argv.slice(2)));
var normalized = this.normalize(argv.slice(2));
var parsed = this.parseOptions(normalized);
var args = this.args = parsed.args;

var result = this.parseArgs(this.args, parsed.unknown);
Expand Down Expand Up @@ -607,7 +608,9 @@ Command.prototype.normalize = function(args) {
var ret = [],
arg,
lastOpt,
index;
index,
short,
opt;

for (var i = 0, len = args.length; i < len; ++i) {
arg = args[i];
Expand All @@ -621,10 +624,17 @@ Command.prototype.normalize = function(args) {
break;
} else if (lastOpt && lastOpt.required) {
ret.push(arg);
} else if (arg.length > 1 && arg[0] === '-' && arg[1] !== '-') {
arg.slice(1).split('').forEach(function(c) {
ret.push('-' + c);
});
} else if (arg.length > 2 && arg[0] === '-' && arg[1] !== '-') {
short = arg.slice(0, 2);
opt = this.optionFor(short);
if (opt && (opt.required || opt.optional)) {
ret.push(short);
ret.push(arg.slice(2));
} else {
arg.slice(1).split('').forEach(function(c) {
ret.push('-' + c);
});
}
} else if (/^--/.test(arg) && ~(index = arg.indexOf('='))) {
ret.push(arg.slice(0, index), arg.slice(index + 1));
} else {
Expand Down
18 changes: 18 additions & 0 deletions test/test.options.short.adjacent-value.js
@@ -0,0 +1,18 @@
var program = require('../')
, should = require('should');

program
.option('-c <required>', 'required with adjacent value')
.option('-q <required>', 'required with separated value')
.option('-o [optional]', 'optional with adjacent value')
.option('-p [optional]', 'optional with separated value')
.option('-n [optional]', 'optional without value in the middle')
.option('-r [optional]', 'optional without value in the end');

program.parse(['node', 'test', '-c22', '-q', '33', '-o44', '-n', '-p', '55', '-r']);
program.C.should.equal('22');
program.Q.should.equal('33');
program.O.should.equal('44');
program.N.should.equal(true);
program.P.should.equal('55');
program.R.should.equal(true);

0 comments on commit ad6cf12

Please sign in to comment.