diff --git a/.eslintrc b/.eslintrc index a2cb583..a8b29b2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,7 +5,8 @@ "es6": true }, "parserOptions": { - "ecmaVersion": 2018 + "ecmaVersion": 2018, + "sourceType": "module" }, "rules": { "no-duplicate-case": 2, diff --git a/CHANGELOG.md b/CHANGELOG.md index 186a5a9..0a80fb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Allowed args after and between options - Replaced `chalk` with `ansi-colors` +- Migrated to ESM ## 3.0.0-beta.1 diff --git a/lib/command.js b/lib/command.js index f07c5f3..7248123 100644 --- a/lib/command.js +++ b/lib/command.js @@ -1,7 +1,7 @@ -const getCommandHelp = require('./help'); -const parseArgv = require('./parse-argv'); -const Params = require('./params'); -const Option = require('./option'); +import getCommandHelp from './help.js'; +import parseArgv from './parse-argv.js'; +import Params from './params.js'; +import Option from './option.js'; const noop = () => {}; // nothing todo const self = value => value; @@ -20,7 +20,7 @@ const handlers = ['init', 'applyConfig', 'finishContext', 'action'].reduce((res, return res; }, { initial: {}, setters: {} }); -module.exports = class Command { +export default class Command { constructor(usage = '') { const [name, params] = usage.trim().split(/(\s+.*)$/); diff --git a/lib/help.js b/lib/help.js index 1d9c4d7..6defaa4 100644 --- a/lib/help.js +++ b/lib/help.js @@ -1,4 +1,4 @@ -const colors = require('ansi-colors'); +import colors from 'ansi-colors'; const MAX_LINE_WIDTH = process.stdout.columns || 200; const MIN_OFFSET = 20; @@ -115,7 +115,7 @@ function optionsHelp(command) { * @return {String} * @api private */ -module.exports = function getCommandHelp(command, commandPath) { +export default function getCommandHelp(command, commandPath) { commandPath = Array.isArray(commandPath) && commandPath.length ? commandPath.concat(command.name).join(' ') : command.name; diff --git a/lib/index.js b/lib/index.js index a7fdedb..d49e763 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,24 +1,26 @@ -const path = require('path'); -const Params = require('./params'); -const Option = require('./option'); -const Command = require('./command'); -const Error = require('./parse-argv-error'); -const getCommandHelp = require('./help'); +import { basename, extname } from 'path'; +import Params from './params.js'; +import Option from './option.js'; +import Command from './command.js'; +import Error from './parse-argv-error.js'; +import getCommandHelp from './help.js'; function nameFromProcessArgv() { - return path.basename(process.argv[1], path.extname(process.argv[1])); + return basename(process.argv[1], extname(process.argv[1])); } -module.exports = { +function command(name, params, config) { + name = name || nameFromProcessArgv() || 'command'; + + return new Command(name, params, config); +} + +export { Error, Params, Command, Option, getCommandHelp, - command: function(name, params, config) { - name = name || nameFromProcessArgv() || 'command'; - - return new Command(name, params, config); - } + command }; diff --git a/lib/option.js b/lib/option.js index 9887d00..3668144 100644 --- a/lib/option.js +++ b/lib/option.js @@ -1,9 +1,10 @@ -const Params = require('./params'); +import Params from './params.js'; + const camelcase = name => name.replace(/-(.)/g, (m, ch) => ch.toUpperCase()); const ensureFunction = (fn, fallback) => typeof fn === 'function' ? fn : fallback; const self = value => value; -module.exports = class Option { +export default class Option { static normalizeOptions(opt1, opt2) { const raw = typeof opt1 === 'function' ? { normalize: opt1, default: opt2 } diff --git a/lib/params.js b/lib/params.js index 4835f24..e5ac984 100644 --- a/lib/params.js +++ b/lib/params.js @@ -1,4 +1,4 @@ -module.exports = class Params { +export default class Params { constructor(params = '', context) { // params = .. ..[optional] // - required diff --git a/lib/parse-argv-error.js b/lib/parse-argv-error.js index 5c3a3d8..3e63caf 100644 --- a/lib/parse-argv-error.js +++ b/lib/parse-argv-error.js @@ -1,4 +1,4 @@ -module.exports = class CliError extends Error { +export default class CliError extends Error { constructor(...args) { super(...args); this.name = 'CliError'; diff --git a/lib/parse-argv.js b/lib/parse-argv.js index a9be39c..38058c7 100644 --- a/lib/parse-argv.js +++ b/lib/parse-argv.js @@ -1,4 +1,4 @@ -const CliError = require('./parse-argv-error'); +import CliError from './parse-argv-error.js'; function findVariants(command, entry) { return [ @@ -47,7 +47,7 @@ function consumeOptionParams(option, rawOptions, argv, index, suggestPoint) { return index + tokens.length - 1; } -module.exports = function parseArgv(command, argv, context, suggestMode) { +export default function parseArgv(command, argv, context, suggestMode) { const suggestPoint = suggestMode ? argv.length - 1 : -1; const rawOptions = []; const result = { diff --git a/package.json b/package.json index ab07b7b..e9f4978 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,15 @@ "argument", "completion" ], + "type": "module", "main": "lib/index.js", + "exports": { + ".": { + "import": "./lib/index.js", + "require": "./cjs/index.cjs" + }, + "./package.json": "./package.json" + }, "files": [ "lib" ], diff --git a/test/command-action.js b/test/command-action.js index 87891af..083a6ec 100644 --- a/test/command-action.js +++ b/test/command-action.js @@ -1,13 +1,12 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { equal, notDeepEqual, deepEqual } from 'assert'; +import * as clap from 'clap'; -describe('action()', function() { - it('should have an expected input', function() { +describe('action()', () => { + it('should have an expected input', () => { const calls = []; - const command = cli - .command('test [foo]') + const command = clap.command('test [foo]') .option('--bar', 'bar option') - .action(function(...args) { + .action((...args) => { calls.push({ this: this, arguments: args @@ -16,9 +15,9 @@ describe('action()', function() { command.run(['abc', '--', 'rest', 'args']); - assert.equal(calls.length, 1); - assert.notDeepEqual(calls[0].this, command); - assert.deepEqual(calls[0].arguments, [{ + equal(calls.length, 1); + notDeepEqual(calls[0].this, command); + deepEqual(calls[0].arguments, [{ commandPath: ['test'], options: { bar: false }, args: ['abc'], diff --git a/test/command-args-and-options.js b/test/command-args-and-options.js index de9e38e..f748ca9 100644 --- a/test/command-args-and-options.js +++ b/test/command-args-and-options.js @@ -1,20 +1,20 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepStrictEqual, throws, equal } from 'assert'; +import * as clap from 'clap'; -describe('command run', function() { +describe('command run', () => { describe('args and options', () => { let command; - beforeEach(function() { - command = cli.command('test [foo]') + beforeEach(() => { + command = clap.command('test [foo]') .option('--foo', 'Foo') .option('--bar ', 'Bar', Number); }); - it('no arguments', function() { + it('no arguments', () => { const actual = command.run([]); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -25,10 +25,10 @@ describe('command run', function() { }); }); - it('args', function() { + it('args', () => { const actual = command.run(['qux']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -39,10 +39,10 @@ describe('command run', function() { }); }); - it('options', function() { + it('options', () => { const actual = command.run(['--foo', '--bar', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -54,10 +54,10 @@ describe('command run', function() { }); }); - it('literal args', function() { + it('literal args', () => { const actual = command.run(['--', '--one', '--two', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -68,10 +68,10 @@ describe('command run', function() { }); }); - it('args & options', function() { + it('args & options', () => { const actual = command.run(['qux', '--foo', '--bar', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -83,10 +83,10 @@ describe('command run', function() { }); }); - it('args & options before', function() { + it('args & options before', () => { const actual = command.run(['--foo', '--bar', '123', 'qux']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -98,10 +98,10 @@ describe('command run', function() { }); }); - it('args & literal args', function() { + it('args & literal args', () => { const actual = command.run(['qux', '--', '--one', '--two', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -112,10 +112,10 @@ describe('command run', function() { }); }); - it('options & literal args', function() { + it('options & literal args', () => { const actual = command.run(['--foo', '--bar', '123', '--', '--one', '--two', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -127,10 +127,10 @@ describe('command run', function() { }); }); - it('args & options & literal args', function() { + it('args & options & literal args', () => { const actual = command.run(['qux', '--foo', '--bar', '123', '--', '--one', '--two', '123']); - assert.deepStrictEqual(actual, { + deepStrictEqual(actual, { commandPath: ['test'], options: { __proto__: null, @@ -145,12 +145,12 @@ describe('command run', function() { describe('multi arg option', () => { it('x', () => { - const command = cli.command() + const command = clap.command() .option('--option [arg2]', 'description', function(value, oldValue) { return (oldValue || []).concat(value); }); - assert.deepStrictEqual( + deepStrictEqual( command.run(['--option','foo', 'bar', '--option', 'baz']).options, { __proto__: null, @@ -162,36 +162,36 @@ describe('command run', function() { describe('required argument', () => { let action; - const command = cli + const command = clap .command('test ') .action(() => action = '1') .command('nested ') .action(() => action = '2') .end(); - beforeEach(function() { + beforeEach(() => { action = ''; }); - it('should throw exception if no first argument', function() { - assert.throws( + it('should throw exception if no first argument', () => { + throws( () => command.run([]), /Missed required argument\(s\) for command "test"/ ); }); - it('should throw exception if no second argument', function() { - assert.throws( + it('should throw exception if no second argument', () => { + throws( () => command.run(['one', 'nested']), /Missed required argument\(s\) for command "nested"/ ); }); - it('should treat first argument as value', function() { + it('should treat first argument as value', () => { command.run(['nested']); - assert.equal(action, '1'); + equal(action, '1'); }); - it('should run nested action', function() { + it('should run nested action', () => { command.run(['one', 'nested', 'two']); - assert.equal(action, '2'); + equal(action, '2'); }); }); }); diff --git a/test/command-clone.js b/test/command-clone.js index 9e1bbe7..260ab3a 100644 --- a/test/command-clone.js +++ b/test/command-clone.js @@ -1,13 +1,12 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual, notStrictEqual, notDeepEqual } from 'assert'; +import { command as cli } from 'clap'; describe('Command#clone()', () => { let command; let clone; beforeEach(() => { - command = cli - .command('test') + command = cli('test') .description('test') .option('--test-option', 'xxx') .command('foo') @@ -17,50 +16,50 @@ describe('Command#clone()', () => { }); it('should be deep equal, but dictionaries should not be the same', () => { - assert.deepEqual(clone, command); - assert.notStrictEqual(clone.commands, command.commands); - assert.notStrictEqual(clone.options, command.options); + deepEqual(clone, command); + notStrictEqual(clone.commands, command.commands); + notStrictEqual(clone.options, command.options); }); it('should be deep equal if set the same version', () => { command.version('1.1.1'); - assert.notDeepEqual(clone, command); + notDeepEqual(clone, command); clone.version('1.1.1'); - assert.deepEqual(clone, command); + deepEqual(clone, command); }); it('should be deep equal if add the same option', () => { command.option('--extra', 'zzz'); - assert.notDeepEqual(clone, command); + notDeepEqual(clone, command); clone.option('--extra', 'zzz'); - assert.deepEqual(clone, command); + deepEqual(clone, command); }); it('should be deep equal if add the same subcommand', () => { command.command('bar').option('--abc', 'aaa'); - assert.notDeepEqual(clone, command); + notDeepEqual(clone, command); clone.command('bar').option('--abc', 'aaa'); - assert.deepEqual(clone.commands.bar, command.commands.bar); + deepEqual(clone.commands.bar, command.commands.bar); }); it('should be deep equal if add the same option to nested command with deep cloning', () => { clone = command.clone(true); command.getCommand('foo').option('--extra', 'zzz'); - assert.notDeepEqual(clone, command); + notDeepEqual(clone, command); clone.getCommand('foo').option('--extra', 'zzz'); - assert.deepEqual(clone, command); + deepEqual(clone, command); }); it('should apply handlers as expected', () => { const actual = clone .run(['--test-option']); - assert.deepEqual(actual.options, { + deepEqual(actual.options, { testOption: true }); }); diff --git a/test/command-createOptionValues.js b/test/command-createOptionValues.js index d75ea62..470f380 100644 --- a/test/command-createOptionValues.js +++ b/test/command-createOptionValues.js @@ -1,12 +1,12 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepStrictEqual } from 'assert'; +import * as clap from 'clap'; -describe('createOptionValues()', function() { - it('boolean option', function() { - const command = cli.command() +describe('createOptionValues()', () => { + it('boolean option', () => { + const command = clap.command() .option('--option', 'description', Boolean); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ option: 'bad value' }), { __proto__: null, @@ -15,13 +15,13 @@ describe('createOptionValues()', function() { ); }); - it('number option', function() { - const command = cli.command() + it('number option', () => { + const command = clap.command() .option('--option ', 'description', function(value) { return isNaN(value) ? 0 : value; }, 1); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ option: 'bad value' }), { __proto__: null, @@ -30,13 +30,13 @@ describe('createOptionValues()', function() { ); }); - it('multi arg option', function() { - const command = cli.command() + it('multi arg option', () => { + const command = clap.command() .option('--option [arg2]', 'description', function(value, oldValue) { return (oldValue || []).concat(value); }); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ option: ['foo', 'bar'] }), { __proto__: null, @@ -45,11 +45,11 @@ describe('createOptionValues()', function() { ); }); - it('option with no default value and argument should be set', function() { - const command = cli.command() + it('option with no default value and argument should be set', () => { + const command = clap.command() .option('--option '); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ option: 'ok' }), { __proto__: null, @@ -58,24 +58,24 @@ describe('createOptionValues()', function() { ); }); - it('should ignore unknown keys', function() { - const command = cli.command() + it('should ignore unknown keys', () => { + const command = clap.command() .option('--option '); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ foo: 'ok' }), Object.create(null) ); }); - it('general test', function() { - const command = cli.command() + it('general test', () => { + const command = clap.command() .option('--foo ', '', Number) .option('--bar [value]') .option('--with-default [x]', '', { default: 'default' }) .option('--bool'); - assert.deepStrictEqual( + deepStrictEqual( command.createOptionValues({ foo: '123', option: 'ok' diff --git a/test/command-extend.js b/test/command-extend.js index 18c001e..c0dee41 100644 --- a/test/command-extend.js +++ b/test/command-extend.js @@ -1,5 +1,5 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual } from 'assert'; +import * as clap from 'clap'; describe('Command#extend()', () => { it('basic', () => { @@ -8,8 +8,7 @@ describe('Command#extend()', () => { invocations.push(args); }; - const command = cli - .command('test') + const command = clap.command('test') .extend(extension, 1, 2); const nested = command .command('nested') @@ -18,7 +17,7 @@ describe('Command#extend()', () => { command.extend(extension, 2, 3); - assert.deepEqual(invocations, [ + deepEqual(invocations, [ [command, 1, 2], [nested], [nested, 1, 2, 3, 4], diff --git a/test/command-help.js b/test/command-help.js index 9686ad9..aa56dfd 100644 --- a/test/command-help.js +++ b/test/command-help.js @@ -1,22 +1,22 @@ -const assert = require('assert'); -const stdout = require('test-console').stdout; -const cli = require('../lib'); +import { strictEqual, equal } from 'assert'; +import { stdout } from 'test-console'; +import * as clap from 'clap'; describe('Command help', () => { let inspect; beforeEach(() => inspect = stdout.inspect()); afterEach(() => inspect.restore()); - it('should remove default help when .help(false)', function() { - const command = cli.command('test').help(false); + it('should remove default help when .help(false)', () => { + const command = clap.command('test').help(false); - assert.strictEqual(command.getOption('help'), null); + strictEqual(command.getOption('help'), null); }); it('should show help', () => { - cli.command('test', false).run(['--help']); + clap.command('test', false).run(['--help']); - assert.equal(inspect.output, [ + equal(inspect.output, [ 'Usage:', '', ' \u001b[36mtest\u001b[39m [\u001b[33moptions\u001b[39m]', @@ -29,13 +29,13 @@ describe('Command help', () => { ].join('\n')); }); - it('help with no short options', function() { - cli.command('test', false, { defaultHelp: false }) + it('help with no short options', () => { + clap.command('test', false, { defaultHelp: false }) .help('--help') .option('--foo', 'Foo') .run(['--help']); - assert.equal(inspect.output, [ + equal(inspect.output, [ 'Usage:', '', ' \u001b[36mtest\u001b[39m [\u001b[33moptions\u001b[39m]', @@ -50,8 +50,7 @@ describe('Command help', () => { }); it('should show help all cases', () => { - cli - .command('test [qux]') + clap.command('test [qux]') .description('Test description') .option('-f, --foo', 'Foo') .option('--bar ', 'Bar', 8080) @@ -62,7 +61,7 @@ describe('Command help', () => { .end() .run(['--help']); - assert.equal(inspect.output, [ + equal(inspect.output, [ 'Test description', '', 'Usage:', @@ -85,15 +84,14 @@ describe('Command help', () => { }); it('should show help for nested command', () => { - cli - .command('test [qux]') + clap.command('test [qux]') .option('-f, --foo', 'Foo') .command('nested [nested-arg]') .option('--bar ', 'Bar') .end() .run(['nested', '--help']); - assert.equal(inspect.output, [ + equal(inspect.output, [ 'Usage:', '', ' \u001b[36mtest nested\u001b[39m \u001b[35m[nested-arg]\u001b[39m [\u001b[33moptions\u001b[39m]', @@ -107,14 +105,13 @@ describe('Command help', () => { ].join('\n')); }); - it('should show help message when Command#outputHelp called', function() { - const command = cli - .command('test [qux]') + it('should show help message when Command#outputHelp called', () => { + const command = clap.command('test [qux]') .option('-f, --foo', 'Foo'); command.outputHelp(); - assert.equal(inspect.output, [ + equal(inspect.output, [ 'Usage:', '', ' \u001b[36mtest\u001b[39m \u001b[35m[qux]\u001b[39m [\u001b[33moptions\u001b[39m]', diff --git a/test/command-parse-handlers.js b/test/command-parse-handlers.js index 1331b11..f552d8c 100644 --- a/test/command-parse-handlers.js +++ b/test/command-parse-handlers.js @@ -1,13 +1,13 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual } from 'assert'; +import * as clap from 'clap'; -describe('init()/applyConfig()/finishContext()', function() { +describe('init()/applyConfig()/finishContext()', () => { let command; let calls; - beforeEach(function() { + beforeEach(() => { calls = []; - command = cli.command('test [arg1]') + command = clap.command('test [arg1]') .init(() => calls.push('init')) .applyConfig(() => calls.push('applyConfig')) .finishContext(() => calls.push('finishContext')); @@ -17,18 +17,18 @@ describe('init()/applyConfig()/finishContext()', function() { .finishContext(() => calls.push('nested finishContext')); }); - it('with no arguments should init/finishContext top level command only', function() { + it('with no arguments should init/finishContext top level command only', () => { command.run([]); - assert.deepEqual(calls, ['init', 'applyConfig', 'finishContext']); + deepEqual(calls, ['init', 'applyConfig', 'finishContext']); }); - it('with one argument should init and finishContext top level command', function() { + it('with one argument should init and finishContext top level command', () => { command.run(['foo']); - assert.deepEqual(calls, ['init', 'applyConfig', 'finishContext']); + deepEqual(calls, ['init', 'applyConfig', 'finishContext']); }); - it('with first argument as command should init/finishContext both commands', function() { + it('with first argument as command should init/finishContext both commands', () => { command.run(['nested']); - assert.deepEqual(calls, ['init', 'applyConfig', 'finishContext', 'nested init', 'nested applyConfig', 'nested finishContext']); + deepEqual(calls, ['init', 'applyConfig', 'finishContext', 'nested init', 'nested applyConfig', 'nested finishContext']); }); }); diff --git a/test/command-shortcut-option.js b/test/command-shortcut-option.js index 065f9ce..3309db6 100644 --- a/test/command-shortcut-option.js +++ b/test/command-shortcut-option.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual } from 'assert'; +import * as clap from 'clap'; describe('Command#shortcutOption()', () => { it('basic', () => { - const command = cli.command('test') + const command = clap.command('test') .option('--foo [foo]', 'Foo', Number) .option('--no-bar', 'Bar') .option('--baz [x]', 'Baz', { @@ -17,15 +17,15 @@ describe('Command#shortcutOption()', () => { } }); - assert.deepEqual(command.run([]).options, { + deepEqual(command.run([]).options, { bar: true }); - assert.deepEqual(command.run(['--baz', '5']).options, { + deepEqual(command.run(['--baz', '5']).options, { bar: true, baz: '55', foo: 55 }); - assert.deepEqual(command.run(['--baz', '0']).options, { + deepEqual(command.run(['--baz', '0']).options, { bar: false, baz: '00', foo: 0 diff --git a/test/command-version.js b/test/command-version.js index f0dee6f..cbe80e0 100644 --- a/test/command-version.js +++ b/test/command-version.js @@ -1,18 +1,17 @@ -const assert = require('assert'); -const stdout = require('test-console').stdout; -const cli = require('../lib'); +import { equal } from 'assert'; +import { stdout } from 'test-console'; +import * as clap from 'clap'; -describe('command run', function() { +describe('command run', () => { let inspect; beforeEach(() => inspect = stdout.inspect()); afterEach(() => inspect.restore()); it('should output version when specified', () => { - cli - .command('test', false) + clap.command('test', false) .version('1.2.3') .run(['--version']); - assert.equal(inspect.output, '1.2.3\n'); + equal(inspect.output, '1.2.3\n'); }); }); diff --git a/test/names.js b/test/names.js index 08bd94b..f985f7d 100644 --- a/test/names.js +++ b/test/names.js @@ -1,61 +1,61 @@ -const assert = require('assert'); -const cli = require('../lib'); +import assert, { notStrictEqual, throws, strictEqual } from 'assert'; +import * as clap from 'clap'; -describe('names', function() { - it('bool option should be in values, options and long', function() { - const command = cli.command() +describe('names', () => { + it('bool option should be in values, options and long', () => { + const command = clap.command() .option('--bool'); assert([...command.options.keys()], ['--bool', 'bool']); - assert.notStrictEqual(command.getOption('--bool'), null); - assert.notStrictEqual(command.getOption('bool'), null); + notStrictEqual(command.getOption('--bool'), null); + notStrictEqual(command.getOption('bool'), null); }); - it('inverted bool option should be in values and options as normal name and as is in long', function() { - const command = cli.command() + it('inverted bool option should be in values and options as normal name and as is in long', () => { + const command = clap.command() .option('--no-bool'); assert([...command.options.keys()], ['--no-bool', 'bool']); - assert.notStrictEqual(command.getOption('--no-bool'), null); - assert.notStrictEqual(command.getOption('bool'), null); + notStrictEqual(command.getOption('--no-bool'), null); + notStrictEqual(command.getOption('bool'), null); }); - it('dasherized option should store as camelName in options', function() { - const command = cli.command() + it('dasherized option should store as camelName in options', () => { + const command = clap.command() .option('--bool-option'); assert([...command.options.keys()], ['--bool-option', 'boolOption']); - assert.notStrictEqual(command.getOption('--bool-option'), null); - assert.notStrictEqual(command.getOption('boolOption'), null); + notStrictEqual(command.getOption('--bool-option'), null); + notStrictEqual(command.getOption('boolOption'), null); }); - it('non-bool option should have name as is', function() { - const command = cli.command() + it('non-bool option should have name as is', () => { + const command = clap.command() .option('--no-bool '); assert([...command.options.keys()], ['--no-bool', 'noBool']); - assert.notStrictEqual(command.getOption('--no-bool'), null); - assert.notStrictEqual(command.getOption('noBool'), null); + notStrictEqual(command.getOption('--no-bool'), null); + notStrictEqual(command.getOption('noBool'), null); }); - it('should be exception if no long form', function() { - assert.throws( - () => cli.command().option('-b'), + it('should be exception if no long form', () => { + throws( + () => clap.command().option('-b'), /Usage has no long name: -b/ ); }); - it('#getOption() should not resolve option name by long form', function() { - const command = cli.command() + it('#getOption() should not resolve option name by long form', () => { + const command = clap.command() .option('--long-form'); - assert.strictEqual(command.getOption('long-form'), null); + strictEqual(command.getOption('long-form'), null); }); - it('#getOption() should resolve option name by camelName', function() { - const command = cli.command() + it('#getOption() should resolve option name by camelName', () => { + const command = clap.command() .option('--long-form'); - assert.notStrictEqual(command.getOption('longForm'), null); + notStrictEqual(command.getOption('longForm'), null); }); }); diff --git a/test/option-bool.js b/test/option-bool.js index eda689d..b132abe 100644 --- a/test/option-bool.js +++ b/test/option-bool.js @@ -1,95 +1,95 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { strictEqual, throws } from 'assert'; +import * as clap from 'clap'; -describe('boolean options', function() { - describe('positive', function() { - it('should be false by default', function() { - const command = cli.command() +describe('boolean options', () => { + describe('positive', () => { + it('should be false by default', () => { + const command = clap.command() .option('--bool'); const { options } = command.run([]); - assert.strictEqual(options.bool, false); + strictEqual(options.bool, false); }); - it('should throw an exception if oposite option defined already', function() { - assert.throws( - () => cli.command() + it('should throw an exception if oposite option defined already', () => { + throws( + () => clap.command() .option('--no-bool') .option('--bool'), /Option name "bool" already in use by --no-bool/ ); }); - it('should be true if option present', function() { - const command = cli.command() + it('should be true if option present', () => { + const command = clap.command() .option('--bool'); const { options } = command.run(['--bool']); - assert.strictEqual(options.bool, true); + strictEqual(options.bool, true); }); - it('should throw an exception for inverted option', function() { - const command = cli.command() + it('should throw an exception for inverted option', () => { + const command = clap.command() .option('--bool'); - assert.throws( + throws( () => command.run(['--no-bool']), /Unknown option: --no-bool/ ); }); - it('normalize function result should be ignored', function() { - const command = cli.command() + it('normalize function result should be ignored', () => { + const command = clap.command() .option('--bool', 'description', () => false); const { options } = command.run(['--bool']); - assert.strictEqual(options.bool, true); + strictEqual(options.bool, true); }); }); - describe('negative', function() { - it('should be true by default', function() { - const command = cli.command() + describe('negative', () => { + it('should be true by default', () => { + const command = clap.command() .option('--no-bool'); const { options } = command.run([]); - assert.strictEqual(options.bool, true); + strictEqual(options.bool, true); }); - it('should throw an exception if oposite option defined already', function() { - assert.throws( - () => cli.command() + it('should throw an exception if oposite option defined already', () => { + throws( + () => clap.command() .option('--bool') .option('--no-bool'), /Option name "bool" already in use by --bool/ ); }); - it('should be false if option present', function() { - const command = cli.command() + it('should be false if option present', () => { + const command = clap.command() .option('--no-bool'); const { options } = command.run(['--no-bool']); - assert.strictEqual(options.bool, false); + strictEqual(options.bool, false); }); - it('should throw an exception for non-inverted option', function() { - const command = cli.command() + it('should throw an exception for non-inverted option', () => { + const command = clap.command() .option('--no-bool'); - assert.throws( + throws( () => command.run(['--bool']), /Unknown option: --bool/ ); }); - it('normalize function result should be ignored', function() { - const command = cli.command() + it('normalize function result should be ignored', () => { + const command = clap.command() .option('--no-bool', 'description', () => true); const { options } = command.run(['--no-bool']); - assert.strictEqual(options.bool, false); + strictEqual(options.bool, false); }); }); }); diff --git a/test/option-one-arg.js b/test/option-one-arg.js index 640e5a9..b925426 100644 --- a/test/option-one-arg.js +++ b/test/option-one-arg.js @@ -1,48 +1,48 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual, notStrictEqual, strictEqual, throws, deepStrictEqual, doesNotThrow } from 'assert'; +import * as clap from 'clap'; -describe('one arg options', function() { - describe('required param', function() { - it('should not be in values by default', function() { - const command = cli.command() +describe('one arg options', () => { + describe('required param', () => { + it('should not be in values by default', () => { + const command = clap.command() .option('--option '); const { options } = command.run([]); - assert.deepEqual(options, Object.create(null)); - assert.notStrictEqual(command.getOption('option'), null); + deepEqual(options, Object.create(null)); + notStrictEqual(command.getOption('option'), null); }); - it('should store default value', function() { - const command = cli.command() + it('should store default value', () => { + const command = clap.command() .option('--option ', 'description', 123); const { options } = command.run([]); - assert.strictEqual(options.option, 123); + strictEqual(options.option, 123); }); - it('default value should be wrapped by normalize function', function() { - const command = cli.command() + it('default value should be wrapped by normalize function', () => { + const command = clap.command() .option('--option ', 'description', value => value * 2, 123); const { options } = command.run([]); - assert.strictEqual(options.option, 246); + strictEqual(options.option, 246); }); - it('should not be in values when normalize function preset but no default value', function() { - const command = cli.command() - .option('--option ', 'description', function() { + it('should not be in values when normalize function preset but no default value', () => { + const command = clap.command() + .option('--option ', 'description', () => { return 123; }); const { options } = command.run([]); - assert.deepEqual(options, Object.create(null)); + deepEqual(options, Object.create(null)); }); - it('should read only one argument', function() { + it('should read only one argument', () => { let ok = false; let values; - const command = cli.command() + const command = clap.command() .option('--option ', 'description') .finishContext(({ options }) => values = options) .command('test') @@ -50,97 +50,97 @@ describe('one arg options', function() { .end(); command.run(['--option', '1', 'test']); - assert.strictEqual(values.option, '1'); - assert.strictEqual(ok, true); + strictEqual(values.option, '1'); + strictEqual(ok, true); }); - it('should ignore commands', function() { + it('should ignore commands', () => { let ok = true; - const command = cli.command() + const command = clap.command() .option('--option ', 'description') .command('test') .action(() => ok = false) .end(); const { options } = command.run(['--option', 'test']); - assert.strictEqual(ok, true); - assert.strictEqual(options.option, 'test'); + strictEqual(ok, true); + strictEqual(options.option, 'test'); }); - it('should be exception if arg is not specified (no more arguments)', function() { - const command = cli.command() + it('should be exception if arg is not specified (no more arguments)', () => { + const command = clap.command() .option('--option ', 'description'); - assert.throws( + throws( () => command.run(['--option']), /Option --option should be used with at least 1 argument\(s\)/ ); }); - it('should be exception if arg is not specified (another option next)', function() { - const command = cli.command() + it('should be exception if arg is not specified (another option next)', () => { + const command = clap.command() .option('--test') .option('--option ', 'description'); - assert.throws( + throws( () => command.run(['--option', '--test']), /Option --option should be used with at least 1 argument\(s\)/ ); }); - it('#setValue should normalizenew value', function() { - const command = cli.command() + it('#setValue should normalizenew value', () => { + const command = clap.command() .option('--option ', 'description', value => value * 2); const { options } = command.run([]); options.option = 123; - assert.strictEqual(options.option, 246); + strictEqual(options.option, 246); }); }); - describe('optional param', function() { - it('should not be in values by default', function() { - const command = cli.command() + describe('optional param', () => { + it('should not be in values by default', () => { + const command = clap.command() .option('--option [arg]'); const { options } = command.run([]); - assert.deepEqual(options, Object.create(null)); - assert.notStrictEqual(command.getOption('option'), null); + deepEqual(options, Object.create(null)); + notStrictEqual(command.getOption('option'), null); }); - it('should store default value', function() { - const command = cli.command() + it('should store default value', () => { + const command = clap.command() .option('--option [arg]', 'description', 123); const actual = command.run([]); - assert.strictEqual(actual.options.option, 123); + strictEqual(actual.options.option, 123); }); - it('default value should be wrapped by normalize function', function() { - const command = cli.command() + it('default value should be wrapped by normalize function', () => { + const command = clap.command() .option('--option [arg]', 'description', function(value) { return value * 2; }, 123); const actual = command.run([]); - assert.strictEqual(actual.options.option, 246); + strictEqual(actual.options.option, 246); }); - it('should not be in values when normalize function preset but no default value', function() { - const command = cli.command() - .option('--option [arg]', 'description', function() { + it('should not be in values when normalize function preset but no default value', () => { + const command = clap.command() + .option('--option [arg]', 'description', () => { return 123; }); const actual = command.run([]); - assert.deepStrictEqual(actual.options, Object.create(null)); + deepStrictEqual(actual.options, Object.create(null)); }); - it('should read only one argument', function() { + it('should read only one argument', () => { let ok = false; let values; - const command = cli.command() + const command = clap.command() .option('--option [arg]', 'description') .finishContext(({ options }) => values = options) .command('test') @@ -148,50 +148,50 @@ describe('one arg options', function() { .end(); command.run(['--option', '1', 'test']); - assert.strictEqual(ok, true); - assert.strictEqual(values.option, '1'); + strictEqual(ok, true); + strictEqual(values.option, '1'); }); - it('should ignore commands', function() { + it('should ignore commands', () => { let ok = true; - const command = cli.command() + const command = clap.command() .option('--option [arg]', 'description') .command('test') .action(() => ok = false) .end(); const { options } = command.run(['--option', 'test']); - assert.strictEqual(ok, true); - assert.strictEqual(options.option, 'test'); + strictEqual(ok, true); + strictEqual(options.option, 'test'); }); - it('should not be exception if arg is not specified (no more arguments)', function() { - const command = cli.command() + it('should not be exception if arg is not specified (no more arguments)', () => { + const command = clap.command() .option('--option [arg]', 'description'); - assert.doesNotThrow(function() { + doesNotThrow(() => { command.run(['--option']); }); }); - it('should not be exception if arg is not specified (another option next)', function() { - const command = cli.command() + it('should not be exception if arg is not specified (another option next)', () => { + const command = clap.command() .option('--test') .option('--option [arg]', 'description'); - assert.doesNotThrow(function() { + doesNotThrow(() => { command.run(['--option', '--test']); }); }); - it('set value to options should normalize new value', function() { - const command = cli.command() + it('set value to options should normalize new value', () => { + const command = clap.command() .option('--option [arg]', 'description', value => value * 2); const { options } = command.run([]); options.option = 123; - assert.strictEqual(options.option, 246); + strictEqual(options.option, 246); }); }); }); diff --git a/test/option-short.js b/test/option-short.js index 9704c10..1655402 100644 --- a/test/option-short.js +++ b/test/option-short.js @@ -1,9 +1,9 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual, throws } from 'assert'; +import * as clap from 'clap'; describe('short options', function() { describe('sequence of boolean options', function() { - const command = cli.command('test') + const command = clap.command('test') .option('-f, --foo', 'Foo') .option('-b, --bar', 'Bar') .option('-x, --baz', 'Baz') @@ -19,19 +19,19 @@ describe('short options', function() { ].forEach(testcase => it(testcase.test, () => { const actual = command.run([testcase.test]); - assert.deepEqual(testcase.expected, actual.options); + deepEqual(testcase.expected, actual.options); }) ); }); describe('should throws when unknown short', function() { - const command = cli.command('test') + const command = clap.command('test') .option('-f, --foo', 'Foo') .option('-b, --bar', 'Bar'); ['-z', '-fz', '-fbz'].forEach((test) => { it(test, () => - assert.throws( + throws( () => command.run(['-fz']), /Unknown option "z" in short option sequence: -fz/ ) @@ -40,15 +40,15 @@ describe('short options', function() { }); it('should throws when non-boolean in sequence', function() { - const command = cli.command('test') + const command = clap.command('test') .option('-f, --foo', 'Foo') .option('-b, --bar ', 'Bar'); - assert.throws( + throws( () => command.run(['-fb']), /Non-boolean option "-b" can't be used in short option sequence: -fb/ ); - assert.throws( + throws( () => command.run(['-bf']), /Non-boolean option "-b" can't be used in short option sequence: -bf/ ); diff --git a/test/suggest.js b/test/suggest.js index 56ecb4e..22566e7 100644 --- a/test/suggest.js +++ b/test/suggest.js @@ -1,7 +1,7 @@ -const assert = require('assert'); -const cli = require('../lib'); +import { deepEqual } from 'assert'; +import * as clap from 'clap'; -describe('suggest', function() { +describe('suggest', () => { function getSuggestions(startWith) { return all.filter(name => name.startsWith(startWith)).sort(); } @@ -17,8 +17,7 @@ describe('suggest', function() { ]; /* eslint-disable indent */ - const command = cli - .command('test [arg1]') + const command = clap.command('test [arg1]') .option('-f, --foo', 'foo') .option('-b, --bar', 'bar') .option('--required-arg ', 'option with required argument') @@ -35,58 +34,58 @@ describe('suggest', function() { .end(); /* eslint-enable indent */ - it('should suggest commands and options when no input', function() { - assert.deepEqual(command.parse([], true), getSuggestions('')); + it('should suggest commands and options when no input', () => { + deepEqual(command.parse([], true), getSuggestions('')); }); - it('should suggest options names when one dash', function() { - assert.deepEqual(command.parse(['-'], true), getSuggestions('-')); + it('should suggest options names when one dash', () => { + deepEqual(command.parse(['-'], true), getSuggestions('-')); }); - it('should suggest options names when double dash', function() { - assert.deepEqual(command.parse(['--'], true), getSuggestions('--')); + it('should suggest options names when double dash', () => { + deepEqual(command.parse(['--'], true), getSuggestions('--')); }); - it('should suggest matched options', function() { - assert.deepEqual(command.parse(['--b'], true), getSuggestions('--b')); + it('should suggest matched options', () => { + deepEqual(command.parse(['--b'], true), getSuggestions('--b')); }); - it('should suggest matched commands', function() { - assert.deepEqual(command.parse(['b'], true), getSuggestions('bar')); + it('should suggest matched commands', () => { + deepEqual(command.parse(['b'], true), getSuggestions('bar')); }); - it('should suggest nothing when no matches', function() { - assert.deepEqual(command.parse(['--miss'], true), []); + it('should suggest nothing when no matches', () => { + deepEqual(command.parse(['--miss'], true), []); }); - it('should suggest matched commands and options of subcommands when no input', function() { - assert.deepEqual(command.parse(['bar', ''], true), ['--help', '--test', 'baz', 'qux']); + it('should suggest matched commands and options of subcommands when no input', () => { + deepEqual(command.parse(['bar', ''], true), ['--help', '--test', 'baz', 'qux']); }); - it('should suggest matched commands of subcommands', function() { - assert.deepEqual(command.parse(['bar', 'b'], true), ['baz']); + it('should suggest matched commands of subcommands', () => { + deepEqual(command.parse(['bar', 'b'], true), ['baz']); }); - it('should suggest options of subcommands', function() { - assert.deepEqual(command.parse(['foo', '-'], true), ['--bar', '--baz', '--foo', '--help']); - assert.deepEqual(command.parse(['foo', '--'], true), ['--bar', '--baz', '--foo', '--help']); + it('should suggest options of subcommands', () => { + deepEqual(command.parse(['foo', '-'], true), ['--bar', '--baz', '--foo', '--help']); + deepEqual(command.parse(['foo', '--'], true), ['--bar', '--baz', '--foo', '--help']); }); - it('should suggest matched options of subcommands', function() { - assert.deepEqual(command.parse(['foo', '--b'], true), ['--bar', '--baz']); + it('should suggest matched options of subcommands', () => { + deepEqual(command.parse(['foo', '--b'], true), ['--bar', '--baz']); }); - it('should suggest nothing for option arguments', function() { - assert.deepEqual(command.parse(['--required-arg', ''], true), []); - assert.deepEqual(command.parse(['--required-arg', 'a'], true), []); - assert.deepEqual(command.parse(['--optional-arg', ''], true), []); - assert.deepEqual(command.parse(['--optional-arg', 'a'], true), []); + it('should suggest nothing for option arguments', () => { + deepEqual(command.parse(['--required-arg', ''], true), []); + deepEqual(command.parse(['--required-arg', 'a'], true), []); + deepEqual(command.parse(['--optional-arg', ''], true), []); + deepEqual(command.parse(['--optional-arg', 'a'], true), []); }); - it('should suggest nothing after double dash', function() { - assert.deepEqual(command.parse(['--', ''], true), []); - assert.deepEqual(command.parse(['--', 'a'], true), []); - assert.deepEqual(command.parse(['--', '-'], true), []); - assert.deepEqual(command.parse(['--', '--'], true), []); + it('should suggest nothing after double dash', () => { + deepEqual(command.parse(['--', ''], true), []); + deepEqual(command.parse(['--', 'a'], true), []); + deepEqual(command.parse(['--', '-'], true), []); + deepEqual(command.parse(['--', '--'], true), []); }); });