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