-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factory routine to create a new unattached argument (#1497)
* Factory routine to create a new unattached argument * ESM export improvements * createArgument ESM test * Add typings for createArgument * Add tests for createArgument Co-authored-by: John Gee <[email protected]>
- Loading branch information
1 parent
0504945
commit 387b6fc
Showing
6 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
import 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const 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'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters