Skip to content

Commit

Permalink
overhauled the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jan 31, 2017
1 parent 8b98b9d commit c7a43d9
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 70 deletions.
31 changes: 0 additions & 31 deletions example/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions example/alias.js

This file was deleted.

4 changes: 0 additions & 4 deletions example/defaultValue.js

This file was deleted.

7 changes: 0 additions & 7 deletions example/group.js

This file was deleted.

28 changes: 23 additions & 5 deletions example/mocha.js
@@ -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)
*/
3 changes: 0 additions & 3 deletions example/multiple.js

This file was deleted.

5 changes: 0 additions & 5 deletions example/name.js

This file was deleted.

38 changes: 31 additions & 7 deletions example/type.js
@@ -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 } ] }
*/

54 changes: 53 additions & 1 deletion example/typical.js
@@ -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 }
*/
13 changes: 12 additions & 1 deletion example/unknown.js
Expand Up @@ -2,10 +2,21 @@
const commandLineArgs = require('../')

const optionDefinitions = [
{ name: 'help', type: Boolean },
{ name: 'depth', type: Number },
{ name: 'files', alias: 'f', type: String, multiple: true, defaultOption: true }
]

const options = commandLineArgs(optionDefinitions, { partial: true })

console.log(options)

/*
Example output:
$ node example/unknown.js --depth 3 package.json README.md --unknown1 --unknown2
{ depth: 3,
files: [ 'package.json', 'README.md' ],
_unknown: [ '--unknown1', '--unknown2' ] }
*/
2 changes: 1 addition & 1 deletion example/validate.js
@@ -1,6 +1,6 @@
/*
command-line-args parses the command line but does not validate what was collected.
This is an example of how values collected can be validated.
This example demonstrates how the values collected can be validated.
*/

'use strict'
Expand Down

0 comments on commit c7a43d9

Please sign in to comment.