github:https://github.com/tj/commander.js/blob/master/examples/arguments.js
1.示例
#!/usr/bin/env node
const BatchHttp = require('../src/index')
const commander = require('commander');
const program = new commander.Command();
const loginConfig = require('../src/data/login')
const createKeywordConfig = require('../src/data/create_keyword')
const createTaskConfig = require('../src/data/create_task')
const ParseXls = require('../src/tools/parse')
const getInstanceConfig = require('../src/data/get_running_instance')
const stopInstanceConfig = require('../src/data/stop_instance')
const runTaskConfig = require('../src/data/run_task')
const deleteConfig = require('../src/data/delete_task')
const resetConfig = require('../src/data/reset_task')
const getConfig = require('../src/data/get_task')
const checkRunningConfig = require('../src/data/check_running')
const getCancelInstanceConfig = require('../src/data/get_cancel_instance')
const runInstanceConfig = require('../src/data/run_instance')
program
.name('run')
.description('Batch process site tasks')
.usage('[command] -[options]')
.description('Example: run website --help')
.version('1.0.1')
.helpOption('-h, --help', '<command> list')//
.addHelpCommand(false) //关闭默认的内建子命令帮助信息
.showHelpAfterError(true)
program.command('website')
.description('Batch run')
.option('-c,--create', 'Batch create all tasks from file')
.option('-s,--stop', 'Batch stop all instances')
.option('-d,--delete', "Batch delete all tasks,if can't delete,then reset")
.option('-r,--reset', 'Batch reset tasks')
.option('-a,--all', 'Batch run all tasks,if task is running,then skip')
.option('-e,--execute', 'Batch create all tasks from file and run them')
.option('-o,--open', 'Batch start all canceled instances')
.helpOption('-h,--help', 'Read more information')
.action(async (options) => {
if (Object.entries(options).length !== 0) {
const xls = new ParseXls('./src/file/数据列表.xls', 0)
const array = xls.readXls(1)
const batchHttp = new BatchHttp()
if (options.all) {
await batchHttp.batchRunTasks(loginConfig, getConfig, runTaskConfig, checkRunningConfig)
} else if (options.create) {
await batchHttp.batchOnlyCreateTask(loginConfig, createTaskConfig, createKeywordConfig, array, 0, 4, 1)
} else if (options.stop) {
await batchHttp.batchStopInstance(loginConfig, stopInstanceConfig, getInstanceConfig)
} else if (options.delete) {
await batchHttp.batchDelete(loginConfig, deleteConfig, getConfig, resetConfig)
} else if (options.reset) {
await batchHttp.batchReset(loginConfig, resetConfig, getConfig)
} else if (options.execute) {
await batchHttp.batchCreateAndRun(loginConfig, createTaskConfig, runTaskConfig, createKeywordConfig, array, 0, 4, 1)
} else if (options.open) {
let cookie = await batchHttp.login(loginConfig)
await batchHttp.batchRunInstance(runInstanceConfig, loginConfig, getCancelInstanceConfig)
}
} else {
console.log('<options> required,add -h or --help for additional information')
}
}).showHelpAfterError(true)
program.parse(process.argv);
2. package.json
"bin": {
"run": "bin/cli.js"
},
3. npm link 建立软链