Repository: yargs/yargs
Pull request #619 (2016-09-03 18:34:49)

For args that have skipValidation set to `true`, check if the parsed arg is `true`
When an arg is defined, it will always exist in the parsed argv. Thus, validation would always be skipped, regardless of whether an arg that has `skipValidation` set to `true` is actually passed on the command line. This commit checks to see if the parsed argv value is actually set to true.

bcoe: @faazshift awesome \o/

mind adding a test for this behavior? otherwise, looks great.
faazshift: @bcoe Thanks! A test is now added.
maxrimue: LGTM :+1: 

Patch:
diff --git a/README.md b/README.md
index 1f441f9..5547ca6 100644
--- a/README.md
+++ b/README.md
@@ -1082,8 +1082,8 @@ For example:
 
 ```js
 require('yargs')
-  .option('foobar', {})
-  .option('foobaz', {})
+  .option('foobar')
+  .option('foobaz')
   .completion()
   .getCompletion(['./test.js', '--foo'], function (completions) {
     console.log(completions)
@@ -1310,13 +1310,14 @@ var argv = require('yargs')
   .argv
 ```
 
-.option(key, opt)
+.option(key, [opt])
 -----------------
-.options(key, opt)
+.options(key, [opt])
 ------------------
 
-Instead of chaining together `.alias().demand().default().describe().string()`, you can specify
-keys in `opt` for each of the chainable methods.
+This method can be used to make yargs aware of options that _could_
+exist. You can also pass an `opt` object which can hold further
+customization, like `.alias()`, `.demand()` etc. for that option.
 
 For example:
 
diff --git a/package.json b/package.json
index 59ecaac..cd3232e 100644
--- a/package.json
+++ b/package.json
@@ -33,12 +33,12 @@
     "coveralls": "^2.11.11",
     "cpr": "^2.0.0",
     "cross-spawn": "^4.0.0",
-    "es6-promise": "^3.0.2",
+    "es6-promise": "^4.0.2",
     "hashish": "0.0.4",
     "mocha": "^3.0.1",
     "nyc": "^8.1.0",
     "rimraf": "^2.5.0",
-    "standard": "^8.0.0",
+    "standard": "^8.2.0",
     "standard-version": "^2.2.1",
     "which": "^1.2.9"
   },
diff --git a/yargs.js b/yargs.js
index 99c102f..7dac74d 100644
--- a/yargs.js
+++ b/yargs.js
@@ -1,4 +1,3 @@
-const assert = require('assert')
 const Command = require('./lib/command')
 const Completion = require('./lib/completion')
 const Parser = require('yargs-parser')
@@ -394,7 +393,9 @@ function Yargs (processArgs, cwd, parentRequire) {
         self.options(k, key[k])
       })
     } else {
-      assert(typeof opt === 'object', 'second argument to option must be an object')
+      if (typeof opt !== 'object') {
+        opt = {}
+      }
 
       options.key[key] = true // track manually set keys.
 
@@ -823,7 +824,7 @@ function Yargs (processArgs, cwd, parentRequire) {
     // Check if any of the options to skip validation were provided
     if (!skipValidation && options.skipValidation.length > 0) {
       skipValidation = Object.keys(argv).some(function (key) {
-        return options.skipValidation.indexOf(key) >= 0
+        return options.skipValidation.indexOf(key) >= 0 && argv[key] === true
       })
     }
 


Test patch:
diff --git a/test/yargs.js b/test/yargs.js
index a349264..75d129d 100644
--- a/test/yargs.js
+++ b/test/yargs.js
@@ -124,6 +124,16 @@ describe('yargs dsl tests', function () {
     })
   })
 
+  it('should not require config object for an option', function () {
+    var r = checkOutput(function () {
+      return yargs([])
+        .option('x')
+        .argv
+    })
+
+    expect(r.errors).to.deep.equal([])
+  })
+
   describe('showHelpOnFail', function () {
     it('should display custom failure message, if string is provided as first argument', function () {
       var r = checkOutput(function () {
@@ -1041,6 +1051,20 @@ describe('yargs dsl tests', function () {
           .argv
       argv.koala.should.equal(true)
     })
+
+    it('allows having an option that skips validation but not skipping validation if that option is not used', function () {
+      var skippedValidation = true
+      yargs(['--no-skip'])
+          .demand(5)
+          .option('skip', {
+            skipValidation: true
+          })
+          .fail(function (msg) {
+            skippedValidation = false
+          })
+          .argv
+      expect(skippedValidation).to.equal(false)
+    })
   })
 
   describe('.help()', function () {