Repository: yargs/yargs
Pull request #600 (2016-08-18 20:51:38)

#540 - Changed parsing of the command string to ignore extra spaces
Changed the parsing of the command string to ignore extra spaces between positional arguments, rather than create an additional required positional argument with a value of an empty string for each extra space. Wrote a test to confirm the fix.

maxrimue: This looks great, thanks for working out the fix! :+1: Btw, if you wonder why Travis failed, it's not because of your code but because of #596 
jmbyerly: Ah okay, yeah I was wondering about that. I checked out some of the other open pull requests and saw that they were throwing the same error so I was guessing (and hoping!) that it was unrelated to my code. Thanks for confirming, that's definitely a relief! Glad my first pull request went off without a hitch!

Patch:
diff --git a/.travis.yml b/.travis.yml
index ed2607c..2d9520d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,7 +2,7 @@ language: node_js
 
 os:
   - linux
-  # - osx
+  - osx
 
 after_success: npm run coverage
 
@@ -11,4 +11,4 @@ node_js:
   - "0.12"
   - "4"
   - "5"
-  - "node"
+  - "6.3.1"
diff --git a/appveyor.yml b/appveyor.yml
index 9cd5fa7..322315d 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -6,7 +6,7 @@ environment:
     - nodejs_version: "0.10"
     - nodejs_version: "4"
     - nodejs_version: "5"
-    - nodejs_version: "6"
+    - nodejs_version: "6.3.1"
 
 # Install scripts. (runs after repo cloning)
 install:
diff --git a/lib/command.js b/lib/command.js
index f56bbfa..3d6615d 100644
--- a/lib/command.js
+++ b/lib/command.js
@@ -85,7 +85,8 @@ module.exports = function (yargs, usage, validation) {
   }
 
   function parseCommand (cmd) {
-    var splitCommand = cmd.split(/\s/)
+    var extraSpacesStrippedCommand = cmd.replace(/\s{2,}/g, ' ')
+    var splitCommand = extraSpacesStrippedCommand.split(/\s/)
     var bregex = /\.*[\][<>]/g
     var parsedCommand = {
       cmd: (splitCommand.shift()).replace(bregex, ''),


Test patch:
diff --git a/test/command.js b/test/command.js
index 7ca7d55..6c405b5 100644
--- a/test/command.js
+++ b/test/command.js
@@ -621,4 +621,19 @@ describe('Command', function () {
       .argv
     argv.should.have.property('someone').and.equal('Leslie')
   })
+
+  // addresses https://github.com/yargs/yargs/issues/540
+  it('ignores extra spaces in command string', function () {
+    var y = yargs([])
+      .command('foo  [awesome]', 'my awesome command', function (yargs) {
+        return yargs
+      })
+    var command = y.getCommandInstance()
+    var handlers = command.getCommandHandlers()
+    handlers.foo.demanded.should.not.include({
+      cmd: '',
+      variadic: false
+    })
+    handlers.foo.demanded.should.have.lengthOf(0)
+  })
 })
diff --git a/test/helpers/utils.js b/test/helpers/utils.js
index cc4a6af..d896dae 100644
--- a/test/helpers/utils.js
+++ b/test/helpers/utils.js
@@ -40,11 +40,16 @@ exports.checkOutput = function (f, argv, cb) {
 
     f()
   } else {
-    result = f()
+    try {
+      result = f()
+    } finally {
+      reset()
+    }
+
     return done()
   }
 
-  function done () {
+  function reset () {
     process.exit = _exit
     process.emit = _emit
     process.env = _env
@@ -52,6 +57,10 @@ exports.checkOutput = function (f, argv, cb) {
 
     console.error = _error
     console.log = _log
+  }
+
+  function done () {
+    reset()
 
     return {
       errors: errors,
diff --git a/test/integration.js b/test/integration.js
index 0751ea5..1605cbe 100644
--- a/test/integration.js
+++ b/test/integration.js
@@ -30,14 +30,16 @@ describe('integration tests', function () {
     it('should match the actual path to the script file', function (done) {
       which('node', function (err, path) {
         if (err) return done(err)
-        testArgs(path.replace('Program Files', 'Progra~1') + ' bin.js', [], done)
+        testArgs(path.replace('Program Files (x86)', 'Progra~2')
+                     .replace('Program Files', 'Progra~1') + ' bin.js', [], done)
       })
     })
 
     it('should match the actual path to the script file, with arguments', function (done) {
       which('node', function (err, path) {
         if (err) return done(err)
-        testArgs(path.replace('Program Files', 'Progra~1') + ' bin.js', [ 'q', 'r' ], done)
+        testArgs(path.replace('Program Files (x86)', 'Progra~2')
+                     .replace('Program Files', 'Progra~1') + ' bin.js', [ 'q', 'r' ], done)
       })
     })
   })
@@ -58,6 +60,11 @@ describe('integration tests', function () {
 
   // see #497
   it('flushes all output when --help is executed', function (done) {
+    if (process.platform === 'win32' && process.version.match(/^v0.10/)) {
+      // Doesn’t work in CI for some reason.
+      return this.skip()
+    }
+
     testCmd('./issue-497.js', [ '--help' ], function (code, stdout) {
       if (code) {
         done(new Error('cmd exited with code ' + code))
@@ -184,15 +191,20 @@ function testCmd (cmd, args, cb) {
   bin.stdout.setEncoding('utf8')
   bin.stdout.on('data', function (str) { stdout += str })
 
-  bin.on('exit', function (code) {
-    cb(code, stdout)
+  var stderr = ''
+  bin.stderr.setEncoding('utf8')
+  bin.stderr.on('data', function (str) { stderr += str })
+
+  bin.on('close', function (code) {
+    cb(code, stdout, stderr)
   })
 }
 
 function testArgs (cmd, args, done) {
-  testCmd(cmd, args, function (code, stdout) {
+  testCmd(cmd, args, function (code, stdout, stderr) {
     if (code) {
-      done(new Error('cmd exited with code ' + code))
+      done(new Error('cmd ' + cmd + ' ' + args + ' exited with code ' + code +
+          '\n' + stdout + '\n' + stderr))
       return
     }
 
diff --git a/test/usage.js b/test/usage.js
index 4a97779..bc4fa7e 100644
--- a/test/usage.js
+++ b/test/usage.js
@@ -1258,6 +1258,10 @@ describe('usage tests', function () {
     })
 
     it('should wrap based on window-size if no wrap is provided', function () {
+      if (!process.stdout.isTTY) {
+        return this.skip()
+      }
+
       var width = require('window-size').width
 
       var r = checkUsage(function () {
diff --git a/test/yargs.js b/test/yargs.js
index fe1459f..601c96e 100644
--- a/test/yargs.js
+++ b/test/yargs.js
@@ -431,6 +431,10 @@ describe('yargs dsl tests', function () {
 
   describe('terminalWidth', function () {
     it('returns the maximum width of the terminal', function () {
+      if (!process.stdout.isTTY) {
+        return this.skip()
+      }
+
       yargs.terminalWidth().should.be.gte(0)
     })
   })