nyx_server
Version:
Node内容发布
195 lines (170 loc) • 5.08 kB
JavaScript
/* global process */
/* global process */
var Validator = require('jsonschema').Validator;
var analysis = require('./analysis');
var help = require('./help');
var colors = require('./colors');
var jsonschemaValidator = new Validator();
var defaultValue = {
type: 'string',
require: false,
}
var createProperty = function(obj, key) {
if (!obj.key) {
return Object.create(defaultValue);
}
return obj.key;
};
// 命令行参数解析格式
// { options: { age: '123', sex: 'kaka', name: 'zhanglin' },
// shortKeys: {},
// remains: [],
// name: 'test' }
// +++++++++++++++++
// 命令行参数配置解析格式
// { name:
// { type: 'string',
// default: 'zhanglin',
// describe: '姓名',
// alias: 'n',
// require: true },
// sex:
// { default: 'male',
// describe: '性别',
// alias: 's',
// enum: [ 'male', 'female' ] },
// age: { type: 'string', describe: '年龄', alias: 'a', require: true } }
var booleanMap = {
'true': true,
'false': false
};
// 将缩写参数转换成全称参数
var dealArgv = function (argv, options) {
for (var shortKey in argv.shortKeys) {
for (var key in options) {
if (options[key].alias === shortKey) {
argv.options[key] = argv.shortKeys[shortKey];
}
}
}
for (key in options) {
if ('default' in options[key] && !argv.options[key]) {
argv.options[key] = options[key].default;
}
}
for (key in argv.options) {
if (options[key] && options[key].type === 'number') {
if (!isNaN(argv.options[key])) {
argv.options[key] = Number(argv.options[key]);
}
} else if (options[key] && options[key].type === 'boolean') {
if (booleanMap.hasOwnProperty(argv.options[key])) {
argv.options[key] = booleanMap[argv.options[key]];
}
}
}
return argv.options;
};
// 创建一个jsonschema,回头会把jsonschema去掉,自己写一个的验证。
var creatJsonschema = function (json) {
var schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://jsonschema.net",
"type": "object",
"properties": {},
"required": []
};
for (var key in json) {
schema.properties[key] = json[key];
if (json[key].require && !json[key].default) {
schema.required.push(key);
}
}
return schema;
};
var Optimist = function () {
this.currentProperty = null;
this._usage = '';
this._info = '';
this.analysis = analysis(process.argv.slice(2));
this.options = {};
};
Optimist.prototype = {
define: function (key) {
this.currentProperty = key;
this.options[key] = createProperty(this, key);
return this;
},
validator: function (json) {
var schema = creatJsonschema(this.options);
return jsonschemaValidator.validate(json, schema);
},
getArgv: function (isExit) {
if (typeof isExit === 'undefined') {
isExit = true;
}
var params = dealArgv(this.analysis, this.options);
var result = this.validator(params);
if (result.errors.length > 0) {
if (isExit) {
console.log(colors.red('参数有误,请检查后重新输入,可以在命令后面使用 --help查看帮助'));
console.log(result.errors);
process.exit(1);
}
return {errors: result.errors};
}
return params;
},
getAnalysis: function () {
return this.analysis;
},
usage: function (result) {
if (result) {
this._usage = result;
return this;
} else {
return this._usage;
}
},
info: function (result) {
if (result) {
this._info = result;
return this;
} else {
return this._info
}
},
enum: function () {
this.options[this.currentProperty]['enum'] = [].slice.call(arguments, 0);
return this;
}
};
['default', 'describe', 'alias', 'require', 'type'].forEach(function(key) {
Optimist.prototype[key] = function (result) {
if (this.currentProperty) {
this.options[this.currentProperty][key] = result;
return this;
} else {
console.log('please use define first');
}
}
});
module.exports = Optimist;
// var optimist = new Optimist();
// var a = optimist
// .usage('nyx info --name name --age age [--sex (male | femal)]')
// .info('获取用户信息')
// .define('name').type('string').default('zhanglin').describe('姓名').alias('n').require(true)
// .define('sex').default('male').describe('性别').alias('s').enum('male', 'female')
// .define('age').type('number').describe('年龄').alias('a').require(true)
// .define('open').type('boolean').describe('打开')
// console.log(a.analysis);
// console.log("+++++++++++++++++");
// console.log(a.options);
// // console.log("+++++++++++++++++");
// // console.log(creatJsonschema(a.options));
// // console.log("+++++++++++++++++");
// // console.log(optimist.validator(dealArgv(optimist.options)));
// console.log("+++++++++++++++++");
// console.log(a.getArgv(false));
// help.detailed(optimist);