This commit is contained in:
Cheney 2025-03-11 14:59:31 +08:00
parent 4b6508a606
commit 5713e9e663
3 changed files with 177 additions and 109 deletions

View File

@ -0,0 +1,68 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliArrayNamingShouldHaveBracket" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliControlFlowStatementWithoutBraces" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliDeprecation" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliEqualsAvoidNull" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliLongLiteralsEndingWithLowercaseL" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliMissingOverrideAnnotation" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AliWrapperTypeEquality" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAbstractClassShouldStartWithAbstractNaming" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidApacheBeanUtilsCopy" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidCallStaticSimpleDateFormat" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidCommentBehindStatement" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidComplexCondition" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidConcurrentCompetitionRandom" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidDoubleOrFloatEqualCompare" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidManuallyCreateThread" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidMissUseOfMathRandom" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidNegationOperator" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidNewDateGetTime" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidPatternCompileInMethod" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidReturnInFinally" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidStartWithDollarAndUnderLineNaming" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaAvoidUseTimer" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaBigDecimalAvoidDoubleConstructor" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaBooleanPropertyShouldNotStartWithIs" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaClassCastExceptionWithSubListToArrayList" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaClassCastExceptionWithToArray" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaClassMustHaveAuthor" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaClassNamingShouldBeCamel" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaCollectionInitShouldAssignCapacity" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaCommentsMustBeJavadocFormat" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaConcurrentExceptionWithModifyOriginSubList" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaConstantFieldShouldBeUpperCase" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaCountDownShouldInFinally" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaDontModifyInForeachCircle" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaEnumConstantsMustHaveComment" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaExceptionClassShouldEndWithException" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaIbatisMethodQueryForList" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaLockShouldWithTryFinally" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaLowerCamelCaseVariableNaming" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaMethodReturnWrapperType" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaMethodTooLong" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaPackageNaming" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaPojoMustOverrideToString" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaPojoMustUsePrimitiveField" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaPojoNoDefaultValue" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaRemoveCommentedCode" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaServiceOrDaoClassShouldEndWithImpl" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaSneakyThrowsWithoutExceptionType" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaStringConcat" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaSwitchExpression" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaSwitchStatement" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaTestClassShouldEndWithTestNaming" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaThreadLocalShouldRemove" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaThreadPoolCreation" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaThreadShouldSetName" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaTransactionMustHaveRollback" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaUndefineMagicConstant" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaUnsupportedExceptionWithModifyAsList" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaUseQuietReferenceNotation" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AlibabaUseRightCaseForDateFormat" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="MapOrSetKeyShouldOverrideHashCodeEquals" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

0
Readme.md Normal file
View File

View File

@ -1,110 +1,110 @@
const CmdLine = require("./CmdLine")
const CliCmdLine = require("./CliCmdLine")
const Config = require("./Config")
const gConfig = new Config();
module.exports = class CliCmdParser {
constructor(name) {
// 软件名称
this.name = name
this.cmdLines = {}
this.funcs = {}
}
/**
* 声明一条命令
* @param cmdLine 命令模板
* @param info 命令说明
* @param func 响应函数
*/
addCmdLine(cmdLine, info, func) {
let cl = CmdLine.parse(cmdLine, info);
if( this.cmdLines[ cl.cmd ] ) {
throw "重复注册的命令 " + cl.cmd
}
this.cmdLines[ cl.cmd ] = cl;
if ( func ) {
this.funcs[ cl.cmd ] = func;
}
return cl;
}
/**
* 打印命令帮助
*/
help(){
let bs = ""
if( this.name ) {
bs += this.name + gConfig.crlf
}
for ( let cmd in this.cmdLines ) {
bs = bs.concat("\t")
.concat( this.cmdLines[ cmd ].line )
.concat("\t")
.concat( this.cmdLines[ cmd ].info )
.concat( gConfig.crlf );
}
return bs;
}
canEval( cliCmdLine ){
return !! this.funcs[ cliCmdLine.cmdline.cmd ];
}
async eval( cliCmdLine ){
let func = this.funcs[ cliCmdLine.cmdline.cmd ];
if ( null == func ) {
throw "没找到命令相应函数";
}
return await func( cliCmdLine );
}
parse(argv){
if ( ! argv || argv.length < 1 ) {
return null;
}
if ( argv[0].startsWith("-") ) {
throw "入参命令格式错误 : 没有入参 cmd"
}
if ( ! this.cmdLines[ argv[0] ] ) {
return null;
}
// 标记: 去除 - 之后的值
const flags = new Set([])
// 命名参数: 去除 -- 之后的值为 key之后值为 value
const params = {}
// 位置参数:值
const position = []
for (let i = 1; i < argv.length; i++ ) {
if ( argv[i].startsWith("--") ) {
if ( i + 1 == argv.length || argv[ i + 1 ].startsWith("-") ) {
throw "flag 格式错误" + argv[i];
}
params[ argv[i].substring(2) ] = argv[++i];
} else if ( argv[i].startsWith("-") ) {
flags.add( argv[i].substring(1) );
} else {
position.push( argv[i] );
}
}
let line = this.cmdLines[ argv[0] ];
// 整理
// line 是藐视
// flags, params, position 是值
return new CliCmdLine( this, line, flags, params, position);
}
const CmdLine = require("./CmdLine")
const CliCmdLine = require("./CliCmdLine")
const Config = require("./Config")
const gConfig = new Config();
module.exports = class CliCmdParser {
constructor(name) {
// 软件名称
this.name = name
this.cmdLines = {}
this.funcs = {}
}
/**
* 声明一条命令
* @param cmdLine 命令模板
* @param info 命令说明
* @param func 响应函数
*/
addCmdLine(cmdLine, info, func) {
let cl = CmdLine.parse(cmdLine, info);
if( this.cmdLines[ cl.cmd ] ) {
throw "重复注册的命令 " + cl.cmd
}
this.cmdLines[ cl.cmd ] = cl;
if ( func ) {
this.funcs[ cl.cmd ] = func;
}
return cl;
}
/**
* 打印命令帮助
*/
help(){
let bs = ""
if( this.name ) {
bs += this.name + gConfig.crlf
}
for ( let cmd in this.cmdLines ) {
bs = bs.concat("\t")
.concat( this.cmdLines[ cmd ].line )
.concat("\t")
.concat( this.cmdLines[ cmd ].info )
.concat( gConfig.crlf );
}
return bs;
}
canEval( cliCmdLine ){
return !! this.funcs[ cliCmdLine.cmdline.cmd ];
}
async eval( cliCmdLine ){
let func = this.funcs[ cliCmdLine.cmdline.cmd ];
if ( null == func ) {
throw "没找到命令相应函数";
}
return await func( cliCmdLine );
}
parse(argv){
if ( ! argv || argv.length < 1 ) {
return null;
}
if ( argv[0].startsWith("-") ) {
throw "入参命令格式错误 : 没有入参 cmd"
}
if ( ! this.cmdLines[ argv[0] ] ) {
return null;
}
// 标记: 去除 - 之后的值
const flags = new Set([])
// 命名参数: 去除 -- 之后的值为 key之后值为 value
const params = {}
// 位置参数:值
const position = []
for (let i = 1; i < argv.length; i++ ) {
if ( argv[i].startsWith("--") ) {
if ( i + 1 == argv.length || argv[ i + 1 ].startsWith("-") ) {
throw "flag 格式错误" + argv[i];
}
params[ argv[i].substring(2) ] = argv[++i];
} else if ( argv[i].startsWith("-") ) {
flags.add( argv[i].substring(1) );
} else {
position.push( argv[i] );
}
}
let line = this.cmdLines[ argv[0] ];
// 整理
// line 是藐视
// flags, params, position 是值
return new CliCmdLine( this, line, flags, params, position);
}
}