Repository: lirantal/lockfile-lint
Pull request #72 (2020-02-19 20:23:25)

feat(cli): error on conflicting options
## Description

BREAKING CHANGE: CLI may show an error when arguments conflict
    and the order of short and long options was reversed to be
    more descriptive on CLI options errors.

## Types of changes

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Related Issue

#64 


lirantal: @XhmikosR unfortunately there doesn't seem to be any way to figure out which argument was used with yargs so even trying to set a custom failure message would not work.

The most improvement I could find at the moment is specifying the long name of the option instead of the short one, for which I also flipped them in the help/usage message.

I'll merge this as a breaking change to bump a major version.
codecov-io: # [Codecov](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=h1) Report
> Merging [#72](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=desc) into [master](https://codecov.io/gh/lirantal/lockfile-lint/commit/53d57bad844b6ba509dea46652a72bc2bd84f557?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/lirantal/lockfile-lint/pull/72/graphs/tree.svg?width=650&token=P36VJa7ksa&height=150&src=pr)](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #72   +/-   ##
=======================================
  Coverage   95.08%   95.08%           
=======================================
  Files          11       11           
  Lines         183      183           
  Branches       29       29           
=======================================
  Hits          174      174           
  Misses          8        8           
  Partials        1        1
```


| [Impacted Files](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...s/lockfile-lint-api/src/validators/ValidateHost.js](https://codecov.io/gh/lirantal/lockfile-lint/pull/72/diff?src=pr&el=tree#diff-cGFja2FnZXMvbG9ja2ZpbGUtbGludC1hcGkvc3JjL3ZhbGlkYXRvcnMvVmFsaWRhdGVIb3N0Lmpz) | `100% <100%> (ø)` | :arrow_up: |
| [...lockfile-lint-api/src/validators/ValidateScheme.js](https://codecov.io/gh/lirantal/lockfile-lint/pull/72/diff?src=pr&el=tree#diff-cGFja2FnZXMvbG9ja2ZpbGUtbGludC1hcGkvc3JjL3ZhbGlkYXRvcnMvVmFsaWRhdGVTY2hlbWUuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=footer). Last update [53d57ba...f773455](https://codecov.io/gh/lirantal/lockfile-lint/pull/72?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

XhmikosR: @lirantal no worries, thanks for looking into it :)

PS. it does sound weird that you can't access the used arg, but I haven't spent a lot of time with yargs to be able to help
lirantal: I'll merge this in as a major change anyway and I also pinged @bcoe for his take on the yargs setup. We can always push a fix if we find a better way and friendlier way. 

Patch:
diff --git a/packages/lockfile-lint/src/cli.js b/packages/lockfile-lint/src/cli.js
index fb5147f..4bd5bf7 100644
--- a/packages/lockfile-lint/src/cli.js
+++ b/packages/lockfile-lint/src/cli.js
@@ -38,7 +38,8 @@ const argv = yargs
     o: {
       alias: ['allowed-schemes'],
       type: 'array',
-      describe: 'validates a whitelist of allowed schemes to be used for resources in the lockfile'
+      describe: 'validates a whitelist of allowed schemes to be used for resources in the lockfile',
+      conflicts: ['validate-https', 's']
     }
   })
   .example('lockfile-lint --path yarn.lock --validate-https')


Test patch:
diff --git a/packages/lockfile-lint/__tests__/cli.test.js b/packages/lockfile-lint/__tests__/cli.test.js
index cc0281b..12d2147 100644
--- a/packages/lockfile-lint/__tests__/cli.test.js
+++ b/packages/lockfile-lint/__tests__/cli.test.js
@@ -78,4 +78,26 @@ describe('CLI tests', () => {
       done()
     })
   })
+
+  test('Providing conflicting arguments should display an error', done => {
+    const process = childProcess.spawn(cliExecPath, [
+      '--type',
+      'yarn',
+      '--path',
+      '__tests__/fixtures/yarn-only-http.lock',
+      '--validate-https',
+      '--allowed-schemes',
+      'https:'
+    ])
+
+    let output = ''
+    process.stderr.on('data', chunk => {
+      output += chunk
+    })
+
+    process.stderr.on('close', _ => {
+      expect(output.indexOf('Arguments o and validate-https are mutually exclusive')).not.toBe(-1)
+      done()
+    })
+  })
 })