// tslint:disable: no-string-literal import { CliCommandDefinition, CliProgramDefinition, ParsedCliCommand } from '../src'; import { createProgram, parseCommandLine } from './helpers/utils'; describe('Basic Command', () => { const buildCmdDef: CliCommandDefinition = { name: 'build', description: 'my description', handler: parsed => { // console.log(`running ${parsed.parsedCommandName}`); }, arguments: [ { name: 'src', description: 'Source file' }, { name: 'dest', description: 'Desination File', isOptional: true, default: 'Default Value' }, { name: 'thirdarg', description: 'Another arg', // isOptional: true, // default value should affect this as set default: 'Another Default', validate: /^(large|medium|small)$/i, }, ], options: [ { name: 'output', flag: '-o', description: 'output description', }, { name: 'transform', flag: '-t', description: 'test transform', transform: (val) => { return `tranformed:${val}` } }, { name: 'noValueType', description: 'to test for default valueType', }, { name: 'tobool', flag: '-b', description: 'test bool valuetype', valueType: 'boolean' }, { name: 'tobooldefaulttrue', flag: '-b', description: 'test bool valuetype', valueType: 'boolean', default: true }, { name: 'tostring', description: 'test string valuetype', valueType: 'string' }, { name: 'tonumber', description: 'test number valuetype', valueType: 'number' }, { name: 'validation', description: 'Validation', validate: /^(large|medium|small)$/i, valueType: 'string' }, { name: 'dashedName', description: 'Dashed Name', valueType: 'string', }, ], }; const defNoDefaultCommandName = createProgram('test1', buildCmdDef); const defWithDefaultCommandName = createProgram('test1', buildCmdDef, 'build'); const p1 = parseCommandLine('build a b', defNoDefaultCommandName); it('Parse a command line with args', () => { expect(p1.command.name).toEqual('build'); expect(p1.command.description).toEqual('my description'); expect(p1.parsedCommandName).toEqual('build'); expect(p1.command.arguments).toHaveLength(3); // expect(p1.optionDefinitions['noValueType'].valueType).toEqual('boolean'); expect(p1.arguments).toMatchObject({ src: 'a', dest: 'b'}); expect(p1.options).toMatchObject({}); expect(p1.version).toEqual('1.0.0'); expect(p1.errors.allErrors).toHaveLength(0); }) const p2 = parseCommandLine('a b', defWithDefaultCommandName); it('Parse a command line without command name and default to proper command', () => { expect(p2.command.name).toEqual('build'); expect(p2.parsedCommandName).toEqual(''); expect(p2.arguments).toMatchObject({ src: 'a', dest: 'b'}); expect(p2.options).toMatchObject({}); expect(p2.version).toEqual('1.0.0'); expect(p1.errors.allErrors).toHaveLength(0); }) const p3 = parseCommandLine('a', defWithDefaultCommandName) it('Substitute a default argument value', () => { expect(p3.command.name).toEqual('build'); expect(p3.parsedCommandName).toEqual(''); expect(p3.arguments).toMatchObject({ src: 'a', dest: 'Default Value'}); expect(p3.options).toMatchObject({}); expect(p3.version).toEqual('1.0.0'); expect(p1.errors.allErrors).toHaveLength(0); }) const p4 = parseCommandLine('a -o myoutputfile.txt', defWithDefaultCommandName) it('Parse a command line with a default argument value and an option', () => { expect(p4.command.name).toEqual('build'); expect(p4.parsedCommandName).toEqual(''); expect(p4.arguments).toMatchObject({ src: 'a', dest: 'Default Value'}); expect(p4.options).toHaveProperty('output', 'myoutputfile.txt') expect(p4.version).toEqual('1.0.0'); expect(p1.errors.allErrors).toHaveLength(0); }) const p5 = parseCommandLine('a -t yes', defWithDefaultCommandName) it('Transform an option value', () => { expect(p5.options).toHaveProperty('transform', 'tranformed:yes') expect(p1.errors.allErrors).toHaveLength(0); }) const p6 = parseCommandLine('a --tobool 1 --tonumber 5 --tostring 5', defWithDefaultCommandName) it('Coerce option values', () => { expect(p6.options).toHaveProperty('tobool', true) expect(p6.options).toHaveProperty('tonumber', 5) expect(p6.options).toHaveProperty('tostring', '5') expect(p6.errors.allErrors).toHaveLength(0); }) const p6a = parseCommandLine('a --tobool false', defWithDefaultCommandName) it('Coerce option values (bool)', () => { expect(p6a.options).toHaveProperty('tobool', false) }) const p6b = parseCommandLine('a --tobool true', defWithDefaultCommandName) it('Coerce option values (bool2)', () => { expect(p6b.options).toHaveProperty('tobool', true) }) const p6c = parseCommandLine('a --tobool', defWithDefaultCommandName) it('Coerce option values (bool3)', () => { expect(p6c.options).toHaveProperty('tobool', true) }) const p6d = parseCommandLine('a', defWithDefaultCommandName) it('Coerce bool option without passed value to false', () => { expect(p6d.options).toHaveProperty('tobool', false) }) const p6e = parseCommandLine('a', defWithDefaultCommandName) it('Coerce bool option with default true to true', () => { expect(p6e.options).toHaveProperty('tobooldefaulttrue', true) }) const p7 = parseCommandLine('a --validation medium', defWithDefaultCommandName) it('Validate values', () => { expect(p7.options).toHaveProperty('validation', 'medium') expect(p1.errors.allErrors).toHaveLength(0); }) const p8 = parseCommandLine('a --validation xxx', defWithDefaultCommandName) it('Parse error for invalid option value', () => { expect(p8.errors.allErrors).toHaveLength(1); }) const p9 = parseCommandLine('a b medium', defWithDefaultCommandName) it('Parse no errors for valid validated argument', () => { expect(p9.errors.hasErrors).toBeFalsy(); }) const p10 = parseCommandLine('a b c', defWithDefaultCommandName) it('Parse error for invalid validated argument', () => { expect(p10.errors.allErrors).toHaveLength(1); }) const p11 = parseCommandLine('a b c --dashed-name dashedvalue', defWithDefaultCommandName) it('Accept dashed cli option name for camel cased option name', () => { expect(p11.options).toHaveProperty('dashedName', 'dashedvalue'); }) const p11a = parseCommandLine('a b c --dashedName dashedvalue', defWithDefaultCommandName) it('Accept camel cased option name with camel-cased value', () => { expect(p11a.options).toHaveProperty('dashedName', 'dashedvalue'); }) const p12 = parseCommandLine('a b --tostring', defWithDefaultCommandName) it('Parse error for option with a non-boolean valueType when option value not specified', () => { expect(p12.errors.allErrors).toHaveLength(1); }) const p13 = parseCommandLine('a b', defWithDefaultCommandName) it('Parse argument values and wasset flag to ParsedArg', () => { expect(p13.parsedArguments['src']).toHaveProperty('value', 'a') expect(p13.parsedArguments['src']).toHaveProperty('wasPassed', true) expect(p13.parsedArguments['dest']).toHaveProperty('value', 'b') expect(p13.parsedArguments['dest']).toHaveProperty('wasPassed', true) expect(p13.parsedArguments['thirdarg']).toHaveProperty('value', 'Another Default') expect(p13.parsedArguments['thirdarg']).toHaveProperty('wasPassed', false) }) const p14 = parseCommandLine('a b --output test', defWithDefaultCommandName) it('Parse argument values and wasset flag to ParsedArg', () => { expect(p14.parsedOptions['output']).toHaveProperty('value', 'test') expect(p14.parsedOptions['output']).toHaveProperty('wasPassed', true) expect(p14.parsedOptions['tobooldefaulttrue']).toHaveProperty('value', true) expect(p14.parsedOptions['tobooldefaulttrue']).toHaveProperty('wasPassed', false); }) })