Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
120 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
'use strict' | ||
|
||
/* demonstrates use in a mocha test script */ | ||
|
||
const assert = require('assert') | ||
const commandLineArgs = require('../') | ||
|
||
/* enable partial parsing to prevent exceptions being thrown | ||
if the user sets undefined, mocha-specific options (e.g. --no-colors) */ | ||
/* | ||
enable partial parsing to prevent exceptions being thrown | ||
if the user sets undefined, mocha-specific options (e.g. --no-colors) | ||
*/ | ||
const options = commandLineArgs({ name: 'value', type: Number }, { partial: true }) | ||
|
||
describe('Array', function () { | ||
describe('#indexOf()', function () { | ||
it('should return -1 when the value is not present', function () { | ||
assert.equal(options.value, [ 1, 2, 3 ].indexOf(4)) | ||
it('should pass when the supplied value is between 1 and 3', function () { | ||
assert.ok([ 1, 2, 3 ].indexOf(options.value) > -1) | ||
}) | ||
}) | ||
}) | ||
|
||
console.log(JSON.stringify(options, null, ' ')) | ||
/* | ||
Example output: | ||
$ mocha example/mocha.js --value 3 --no-colors | ||
Array | ||
#indexOf() | ||
✓ should pass when the supplied value is between 1 and 3 | ||
1 passing (7ms) | ||
*/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
var fs = require('fs') | ||
'use strict' | ||
const commandLineArgs = require('../') | ||
|
||
function FileDetails (filename) { | ||
if (!(this instanceof FileDetails)) return new FileDetails(filename) | ||
this.filename = filename | ||
this.exists = fs.existsSync(filename) | ||
/* demonstrates a custom `type` function which returns a class instance */ | ||
|
||
class FileDetails { | ||
constructor (filename) { | ||
const fs = require('fs') | ||
this.filename = filename | ||
this.exists = fs.existsSync(filename) | ||
} | ||
} | ||
|
||
module.exports = [ | ||
{ name: 'file', type: FileDetails }, | ||
const optionDefinitions = [ | ||
{ | ||
name: 'file', | ||
multiple: true, | ||
defaultOption: true, | ||
type: filename => new FileDetails(filename) | ||
}, | ||
{ name: 'depth', type: Number } | ||
] | ||
|
||
const options = commandLineArgs(optionDefinitions) | ||
|
||
console.log(options) | ||
|
||
/* | ||
Example output: | ||
$ node example/type.js package.json nothing.js | ||
{ file: | ||
[ FileDetails { filename: 'package.json', exists: true }, | ||
FileDetails { filename: 'nothing.js', exists: false } ] } | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,58 @@ | ||
module.exports = [ | ||
'use strict' | ||
const commandLineArgs = require('../') | ||
const commandLineUsage = require('command-line-usage') | ||
|
||
/* | ||
This example shows typical use alongside command-line-usage | ||
https://github.com/75lb/command-line-usage | ||
*/ | ||
|
||
const optionDefinitions = [ | ||
{ name: 'help', alias: 'h', type: Boolean, description: 'Display this usage guide.' }, | ||
{ name: 'src', type: String, multiple: true, defaultOption: true, description: 'The input files to process', typeLabel: '<files>' }, | ||
{ name: 'timeout', alias: 't', type: Number, description: 'Timeout value in ms', typeLabel: '<ms>' }, | ||
{ name: 'log', alias: 'l', type: Boolean, description: 'info, warn or error' } | ||
] | ||
|
||
const options = commandLineArgs(optionDefinitions) | ||
|
||
if (options.help) { | ||
const usage = commandLineUsage([ | ||
{ | ||
header: 'Typical Example', | ||
content: 'A simple example demonstrating typical usage.' | ||
}, | ||
{ | ||
header: 'Options', | ||
optionList: optionDefinitions | ||
}, | ||
{ | ||
content: 'Project home: [underline]{https://github.com/me/example}' | ||
} | ||
]) | ||
console.log(usage) | ||
} | ||
|
||
console.log(options) | ||
|
||
/* | ||
Example output: | ||
$ node example/typical.js --help | ||
Typical Example | ||
A simple example demonstrating typical usage. | ||
Options | ||
-h, --help Display this usage guide. | ||
--src <files> The input files to process | ||
-t, --timeout <ms> Timeout value in ms | ||
-l, --log info, warn or error | ||
Project home: https://github.com/me/example | ||
{ help: true } | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters