Skip to content

Commit fca7c4f

Browse files
committed
Allow options only in command mode.
1 parent efdd42f commit fca7c4f

13 files changed

+60
-34
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
[Dep:LRT.Core]: https://github.com/litert/core.js
44

5+
## v0.2.0
6+
7+
- Now allows that only options input without commands in command mode.
8+
59
## v0.1.1
610

711
- Update the dependencies.

docs/zh-CN/02-use-command.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Help command is found.
9797
Input: test.txt
9898

9999
$ node dist/samples/sample-07.js --input test.txt
100-
Exception { _errno: 7, _message: 'No command input.' }
100+
Input: test.txt
101101
```
102102

103103
使用命令解析器时,必须输入命令,否则会返回错误。

docs/zh-CN/interface.ICommandParser.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ICommandParser extends ISimpleParser {
1717
*
1818
* @param opts 主命令信息
1919
*/
20-
addCommand(opts: ICommandSettings): ICommandParser;
20+
addCommand(opts: ICommandSettings): this;
2121

2222
/**
2323
* 为一个主命令添加一个新的子命令。遇到如下情况时会抛出异常:
@@ -36,7 +36,7 @@ interface ICommandParser extends ISimpleParser {
3636
addSubCommand(
3737
main: string,
3838
opts: ICommandSettings
39-
): ICommandParser;
39+
): this;
4040

4141
/**
4242
* 开始分析

docs/zh-CN/interface.IParserSettings.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ interface IParserSettings {
3434
* 默认值: false
3535
*/
3636
"shortAttach"?: boolean;
37+
38+
/**
39+
* 允许只使用选项,而不使用命令。
40+
*
41+
* 默认值: true
42+
*/
43+
"allowOptionsOnly"?: boolean;
3744
}
3845
```
3946

docs/zh-CN/interface.ISimpleParser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface ISimpleParser extends ISimpleParser {
1717
*
1818
* @param opts 选项信息
1919
*/
20-
addOption(opts: IOptionSetting): ISimpleParser;
20+
addOption(opts: IOptionSetting): this;
2121

2222
/**
2323
* 开始分析。

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@litert/clap",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"description": "A simple library for parsing commandline arguments.",
55
"main": "dist/index.js",
66
"scripts": {

sources/class.AbstractCommand.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Exception from "./class.Exception";
1717
import * as Errors from "./errors";
1818
import { ICommandSettings } from "./interfaces";
1919

20-
abstract class AbstractCommand implements ICommandSettings {
20+
export abstract class AbstractCommand implements ICommandSettings {
2121

2222
protected _name: string;
2323

@@ -67,4 +67,4 @@ abstract class AbstractCommand implements ICommandSettings {
6767
}
6868
}
6969

70-
export = AbstractCommand;
70+
export default AbstractCommand;

sources/class.CommandParser.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { CommandParseResult } from "./class.ParseResult";
1919
import * as External from "./interfaces";
2020
import * as Errors from "./errors";
2121
import * as Internal from "./internal";
22-
import MainCommand = require("./class.MainCommand");
23-
import { SimpleParser } from "./class.SimpleParser";
22+
import MainCommand from "./class.MainCommand";
23+
import SimpleParser from "./class.SimpleParser";
2424

2525
export class CommandParser extends SimpleParser implements External.ICommandParser {
2626

@@ -37,7 +37,7 @@ export class CommandParser extends SimpleParser implements External.ICommandPars
3737
this._shortMainCommands = {};
3838
}
3939

40-
public addCommand(opts: External.ICommandSettings): External.ICommandParser {
40+
public addCommand(opts: External.ICommandSettings): this {
4141

4242
opts.name = opts.name.toLowerCase();
4343

@@ -70,7 +70,7 @@ export class CommandParser extends SimpleParser implements External.ICommandPars
7070
public addSubCommand(
7171
main: string,
7272
opts: External.ICommandSettings
73-
): External.ICommandParser {
73+
): this {
7474

7575
main = main.toLowerCase();
7676

@@ -106,20 +106,25 @@ export class CommandParser extends SimpleParser implements External.ICommandPars
106106
);
107107
}
108108

109-
if (!ret.mainCommand) {
109+
if (!this._settings.allowOptionsOnly) {
110110

111-
throw new Exception(
112-
Errors.E_LACK_MAIN_COMMAND,
113-
"No command input."
114-
);
115-
}
111+
if (!ret.mainCommand) {
112+
113+
throw new Exception(
114+
Errors.E_LACK_MAIN_COMMAND,
115+
"No command input."
116+
);
117+
}
116118

117-
if (!ret.subCommand && this._mainCommands[ret.mainCommand].enableSubCommand) {
119+
if (!ret.subCommand
120+
&& this._mainCommands[ret.mainCommand].enableSubCommand
121+
) {
118122

119-
throw new Exception(
120-
Errors.E_LACK_SUB_COMMAND,
121-
"No sub command input."
122-
);
123+
throw new Exception(
124+
Errors.E_LACK_SUB_COMMAND,
125+
"No sub command input."
126+
);
127+
}
123128
}
124129

125130
ret.setSuccess();

sources/class.MainCommand.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { IDictionary } from "@litert/core";
1818
import Exception from "./class.Exception";
1919
import { ICommandSettings } from "./interfaces";
2020
import { IMainCommandSettings } from "./internal";
21-
import AbstractCommand = require("./class.AbstractCommand");
21+
import AbstractCommand from "./class.AbstractCommand";
2222

23-
class MainCommand extends AbstractCommand implements IMainCommandSettings {
23+
export class MainCommand extends AbstractCommand implements IMainCommandSettings {
2424

2525
public enableSubCommand?: boolean;
2626

@@ -84,4 +84,4 @@ class MainCommand extends AbstractCommand implements IMainCommandSettings {
8484

8585
class SubCommandOption extends AbstractCommand {}
8686

87-
export = MainCommand;
87+
export default MainCommand;

0 commit comments

Comments
 (0)