Factory routine to create a new unattached argument (#1497)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Factory routine to create a new unattached argument * ESM export improvements * createArgument ESM test * Add typings for createArgument * Add tests for createArgument
authored by Sergei Vizel · committed by GitHub · parent 0504945 · Browse files · 6 files·+83 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: John Gee <john@ruru.gen.nz>
Diff stats
6 files+83 added−3 removed
- tests:+43 added−1 removed
- other:+40 added−2 removed
Provenance
- Commit
387b6fc- Tree
d8269b5- Author
Sergei Vizel <cravler@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0504945
6 files changed+83 added−3 removed
added:tests/command.createArgument.test.js+40 added
12345678910111213141516171819202122232425262728293031323334353637383940const commander = require('../');
class MyArgument extends commander.Argument { constructor(name, description) { super(name, description); this.myProperty = 'MyArgument'; };}
class MyCommand extends commander.Command { createArgument(name, description) { return new MyArgument(name, description); };
// createCommand for testing .command('sub <file>') createCommand(name) { return new MyCommand(name); }}
test('when override createArgument then used for argument()', () => { const program = new MyCommand(); program.argument('<file>'); expect(program._args.length).toEqual(1); expect(program._args[0].myProperty).toEqual('MyArgument');});
test('when override createArgument then used for arguments()', () => { const program = new MyCommand(); program.arguments('<file>'); expect(program._args.length).toEqual(1); expect(program._args[0].myProperty).toEqual('MyArgument');});
test('when override createArgument and createCommand then used for argument of command()', () => { const program = new MyCommand(); const sub = program.command('sub <file>'); expect(sub._args.length).toEqual(1); expect(sub._args[0].myProperty).toEqual('MyArgument');});modified:tests/esm-imports-test.mjs+3 added-1 removed
11234523 unmodified lines29303132333435363738import { program, Command, Option, Argument, CommanderError, InvalidOptionArgumentError, Help, createCommand, createOption } from '../esm.mjs';import { program, Command, Option, Argument, CommanderError, InvalidOptionArgumentError, Help, createCommand, createArgument, createOption } from '../esm.mjs';
// Do some simple checks that expected imports are available at runtime.// Run using `npm run test-esm`.// Similar tests to test-imports.test.ts23 unmodified linescheckClass(new Argument('<file>'), 'Argument');
console.log('Checking createCommand');check(typeof createCommand === 'function', 'createCommand is function');console.log('Checking createArgument');check(typeof createArgument === 'function', 'createArgument is function');console.log('Checking createOption');check(typeof createOption === 'function', 'createOption is function');
console.log('No problems');modified:typings/index.d.ts+8 added
248 unmodified lines249 */250 addCommand(cmd: Command, opts?: CommandOptions): this;251 252 /**253+ * Factory routine to create a new unattached argument.254+ *255+ * See .argument() for creating an attached argument, which uses this routine to256+ * create the argument. You can override createArgument to return a custom argument.257+ */258+ createArgument(name: string, description?: string): Argument;259+260+ /**261 * Define argument syntax for command.262 *263 * The default is that the argument is required, and you can explicitly264 * indicate this with <> around the name. Put [] around the name for an optional argument.419 unmodified linesmodified:typings/index.test-d.ts+4 added
343 unmodified lines344 345 // Argument methods346 // name347 expectType<string>(baseArgument.name());348+349+// createArgument350+expectType<commander.Argument>(program.createArgument('<name>'));351+expectType<commander.Argument>(program.createArgument('<name>', 'description'));modified:esm.mjs+12 added-1 removed
1234456789101112131415import commander from './index.js';
// wrapper to provide named exports for ESM.export const { program, Option, Command, Argument, CommanderError, InvalidOptionArgumentError, Help, createCommand, createOption } = commander;export const { program, createCommand, createArgument, createOption, CommanderError, InvalidOptionArgumentError, Command, Argument, Option, Help} = commander;modified:index.js+16 added-1 removed
835 unmodified lines836 return this;837 };838 839 /**840+ * Factory routine to create a new unattached argument.841+ *842+ * See .argument() for creating an attached argument, which uses this routine to843+ * create the argument. You can override createArgument to return a custom argument.844+ *845+ * @param {string} name846+ * @param {string} [description]847+ * @return {Argument} new argument848+ */849+850+ createArgument(name, description) {851+ return new Argument(name, description);852+ };853+854+ /**855 * Define argument syntax for command.856 *857 * The default is that the argument is required, and you can explicitly858 * indicate this with <> around the name. Put [] around the name for an optional argument.7 unmodified lines866 * @param {string} [description]867 * @return {Command} `this` command for chaining868 */869 argument(name, description) {855- const argument = new Argument(name, description);870+ const argument = this.createArgument(name, description);871 this.addArgument(argument);872 return this;873 }874 1450 unmodified lines