-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
941 additions
and
785 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
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,95 @@ | ||
```js | ||
function myParseInt(value, dummyPrevious) { | ||
// parseInt takes a string and a radix | ||
const parsedValue = parseInt(value, 10); | ||
if (isNaN(parsedValue)) { | ||
throw new commander.InvalidArgumentError('Not a number.'); | ||
} | ||
return parsedValue; | ||
} | ||
|
||
function increaseVerbosity(dummyValue, previous) { | ||
return previous + 1; | ||
} | ||
|
||
function collect(value, previous) { | ||
return previous.concat([value]); | ||
} | ||
|
||
function commaSeparatedList(value, dummyPrevious) { | ||
return value.split(','); | ||
} | ||
|
||
const botConfig = { | ||
adminRoute: '/admin', // optional | ||
botRoute: '/bot', // optional | ||
models: { // optional | ||
Bot: 'your bot data model defination' // check src/models/Bot.ts as a example, optional | ||
}, | ||
commandLineConfigs: { // optional | ||
{ | ||
command: 'adjust' | ||
options: [ | ||
['-f, --float <number>', 'float argument', parseFloat], | ||
['-i, --integer <number>', 'integer argument', myParseInt], | ||
['-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0], | ||
['-c, --collect <value>', 'repeatable value', collect, []], | ||
['-l, --list <items>', 'comma separated list', commaSeparatedList] | ||
] | ||
}, | ||
{ | ||
command: 'list' // this command no option | ||
}, | ||
{ | ||
command: 'add' // this command no option | ||
} | ||
} | ||
// if set commandLineConfigs in botConfig, would get parsed options object from text, check doc/command-line-parser.md for detail | ||
} | ||
|
||
|
||
function eventHandler ({ | ||
type, // could be 'BotRemoved', 'Message4Bot', 'Message4Others', 'BotGroupLeft', 'BotJoinGroup', 'Maintain', 'SetupDatabase' | ||
bot, // the bot instance, check src/models/Bot.ts for instance methods | ||
text, // the text message user posted in chat group | ||
group, // the group object, can get chat group id from group.id | ||
userId, // message creator's id | ||
isPrivateChat, // if it is a private chat | ||
message, // message object, check ringcentral api document for detail | ||
commandLineOptions, // only if set commandLineConfigs in botConfig, would get parse result from text, check doc/command-line-parser.md for detail | ||
}) { | ||
console.log( | ||
type, | ||
bot, | ||
text, | ||
group, | ||
userId, | ||
message | ||
) | ||
// when text === 'list' | ||
expect(commandLineOptions).tobeEqual({ | ||
command: 'list', | ||
rest: '' | ||
}) | ||
// when text = 'add more text desc' | ||
expect(commandLineOptions).tobeEqual({ | ||
command: 'add', | ||
rest: 'more text desc', | ||
options: { | ||
verbose: 0, | ||
collect: [] | ||
} | ||
}) | ||
// when text = 'adjust -f 5.8 -i 5654 some other not related text ' | ||
expect(commandLineOptions).tobeEqual({ | ||
command: 'add', | ||
rest: ' -f 5.8 -i 5654 some other not related text ', | ||
options: { | ||
verbose: 0, | ||
collect: [], | ||
integer: 5654, | ||
float: 5.8 | ||
} | ||
}) | ||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* parse text to command line | ||
*/ | ||
|
||
import { program } from 'commander' | ||
|
||
export default (text: string, commandLineConfigs: any []): any => { | ||
const arr = text.split(' ') | ||
const cmd: any = arr[0] | ||
if (!cmd) { | ||
return {} | ||
} | ||
const conf: any = commandLineConfigs.find(c => c.command === cmd) | ||
if (!conf) { | ||
return {} | ||
} | ||
const rest = arr.slice(1) | ||
const res: any = { | ||
command: cmd, | ||
rest: rest.join(' ') | ||
} | ||
if (conf.options && rest.length) { | ||
for (const opt of conf.options) { | ||
(program.option as any)(...opt) | ||
} | ||
program.parse(rest, { from: 'user' }) | ||
const options = program.opts() | ||
res.options = options | ||
} | ||
return res | ||
} |
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
Oops, something went wrong.