Skip to content

Commit b425a77

Browse files
authored
resolves #149 show --help and --version in usage (#150)
1 parent 0103fe2 commit b425a77

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

lib/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,14 @@ class Options {
218218
describe: 'require the specified library before executing the processor, using the standard Node require',
219219
type: 'string'
220220
})
221+
.version(false)
221222
.option('version', {
222223
alias: 'V',
223224
default: false,
224225
describe: 'display the version and runtime environment (or -v if no other flags or arguments)',
225226
type: 'boolean'
226227
})
228+
.help(false)
227229
.option('help', {
228230
describe: `print a help message
229231
show this usage if TOPIC is not specified or recognized
@@ -267,8 +269,6 @@ By default, the output is written to a file with the basename of the source file
267269
.detectLocale(false)
268270
.wrap(Math.min(120, this.yargs.terminalWidth()))
269271
.command('$0 [files...]', '', () => this.cmd)
270-
.version(false)
271-
.help(false)
272272
.parserConfiguration({
273273
'boolean-negation': false
274274
})

test/test.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,40 @@ describe('Help', () => {
181181
process.exit.restore()
182182
}
183183
})
184+
185+
it('should show --help option on command usage', () => {
186+
sinon.stub(console, 'error')
187+
sinon.stub(process, 'exit')
188+
try {
189+
new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '--help'])).invoke()
190+
expect(process.exit.called).to.be.true()
191+
expect(process.exit.calledWith(0)).to.be.true()
192+
expect(console.error.called).to.be.true()
193+
const usage = console.error.getCall(0).args[0]
194+
expect(usage).to.includes('--help')
195+
expect(usage).to.includes('show an overview of the AsciiDoc syntax if TOPIC is syntax')
196+
} finally {
197+
console.error.restore()
198+
process.exit.restore()
199+
}
200+
})
201+
202+
it('should show --version option on command usage', () => {
203+
sinon.stub(console, 'error')
204+
sinon.stub(process, 'exit')
205+
try {
206+
new Invoker(defaultOptions.parse(['/path/to/node', '/path/to/asciidoctor', '--help'])).invoke()
207+
expect(process.exit.called).to.be.true()
208+
expect(process.exit.calledWith(0)).to.be.true()
209+
expect(console.error.called).to.be.true()
210+
const usage = console.error.getCall(0).args[0]
211+
expect(usage).to.includes('--version')
212+
expect(usage).to.includes('display the version and runtime environment (or -v if no other flags or arguments)')
213+
} finally {
214+
console.error.restore()
215+
process.exit.restore()
216+
}
217+
})
184218
})
185219

186220
describe('Print timings report', () => {
@@ -190,7 +224,7 @@ describe('Print timings report', () => {
190224
sinon.stub(process, 'exit')
191225
try {
192226
const file = path.join(__dirname, 'fixtures', 'sample.adoc')
193-
new Invoker(new Options().parse(['node', 'asciidoctor', file, '--timings', '-o', '-'])).invoke()
227+
new Invoker(defaultOptions.parse(['node', 'asciidoctor', file, '--timings', '-o', '-'])).invoke()
194228
expect(process.stderr.write.called).to.be.true()
195229
expect(process.stderr.write.getCall(0).args[0]).to.equal(`Input file: ${file}`)
196230
expect(process.stderr.write.getCall(1).args[0]).to.include('Time to read and parse source:')
@@ -326,7 +360,7 @@ describe('Template directory', () => {
326360
expect(result['template-dir']).to.include('/path/to/others')
327361
})
328362
it('should set template_dirs option when --template-dir is defined', () => {
329-
const opts = new Options({}).parse('node asciidoctor --template-dir /path/to/templates -b html5 file.adoc')
363+
const opts = defaultOptions.parse('node asciidoctor --template-dir /path/to/templates -b html5 file.adoc')
330364
expect(opts.options.backend).to.equal('html5')
331365
expect(opts.options.template_dirs).to.have.length(1)
332366
expect(opts.options.template_dirs).to.include('/path/to/templates')
@@ -351,7 +385,7 @@ describe('Template engine', () => {
351385
expect(result['template-engine']).to.eq('nunjucks')
352386
})
353387
it('should set the template_engine option when the -E argument is defined', () => {
354-
const opts = new Options({}).parse('node asciidoctor -E pug -b html5 file.adoc')
388+
const opts = defaultOptions.parse('node asciidoctor -E pug -b html5 file.adoc')
355389
expect(opts.options.backend).to.equal('html5')
356390
expect(opts.options.template_engine).to.equal('pug')
357391
})
@@ -380,7 +414,7 @@ describe('Array option', () => {
380414

381415
describe('Options', () => {
382416
it('should create options', () => {
383-
const opts = new Options({}).parse('node asciidoctor -a foo=bar -b html5')
417+
const opts = defaultOptions.parse('node asciidoctor -a foo=bar -b html5')
384418
expect(opts.options.backend).to.equal('html5')
385419
expect(opts.options.attributes).to.include('foo=bar')
386420
})
@@ -414,13 +448,13 @@ describe('Options', () => {
414448

415449
describe('Extend', () => {
416450
it('should not recognize an unknown option', () => {
417-
const opts = new Options({}).parse('node asciidoctor -a foo=bar -b html5')
451+
const opts = defaultOptions.parse('node asciidoctor -a foo=bar -b html5')
418452
expect(opts.args.watch).to.be.undefined()
419453
expect(opts.options.backend).to.equal('html5')
420454
expect(opts.options.attributes).to.include('foo=bar')
421455
})
422456
it('should add option to the command (default value)', () => {
423-
const opts = new Options({})
457+
const opts = defaultOptions
424458
.addOption('watch', {
425459
alias: 'w',
426460
default: false,
@@ -433,7 +467,7 @@ describe('Extend', () => {
433467
expect(opts.options.attributes).to.include('foo=bar')
434468
})
435469
it('should add option to the command', () => {
436-
const opts = new Options({})
470+
const opts = defaultOptions
437471
.addOption('watch', {
438472
alias: 'w',
439473
default: false,
@@ -450,7 +484,7 @@ describe('Extend', () => {
450484
describe('Convert', () => {
451485
it('should convert using a custom doctype (defined as document attribute)', () => {
452486
const file = path.join(__dirname, 'fixtures', 'doctype.adoc')
453-
const options = new Options().parse(['node', 'asciidoctor', file, '-s'])
487+
const options = defaultOptions.parse(['node', 'asciidoctor', file, '-s'])
454488
const asciidoctor = require('@asciidoctor/core')()
455489
const asciidoctorOptions = options.options
456490
Object.assign(asciidoctorOptions, { to_file: false })
@@ -461,7 +495,7 @@ describe('Convert', () => {
461495
it('should convert using the default backend (html5)', () => {
462496
const asciidoctor = require('@asciidoctor/core')()
463497
const file = path.join(__dirname, 'fixtures', 'sample.adoc')
464-
const options = new Options().parse(['node', 'asciidoctor', file, '-s'])
498+
const options = defaultOptions.parse(['node', 'asciidoctor', file, '-s'])
465499
const asciidoctorOptions = options.options
466500
Object.assign(asciidoctorOptions, { to_file: false })
467501
Invoker.prepareProcessor(argsParser.parse(file), asciidoctor)

0 commit comments

Comments
 (0)