Skip to content

Commit 895b99b

Browse files
committed
1.4.1
1 parent 367024d commit 895b99b

5 files changed

Lines changed: 75 additions & 43 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@grandlinex/easy-cli",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Cli lib to perform common tasks",
55
"main": "dist/index.js",
66
"module": "dist/index.js",

src/class/ShellComand.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Path from 'path';
22
import fs from 'fs';
33
import { CoreLogChannel, CoreLogger } from '@grandlinex/core';
4-
import { CmdProperty, IArgs, IHandler, ParamTypeRaw } from '../lib/types.js';
4+
import {
5+
CmdProperty,
6+
IArgs,
7+
IHandler,
8+
ParamTypeRaw,
9+
StricktOption,
10+
} from '../lib/types.js';
511
import ArgUtil from '../utils/ArgUtil.js';
612

713
export type ShellCommandParentProps = {
@@ -52,7 +58,11 @@ export abstract class ShellCommand<T = any> extends CoreLogChannel {
5258
if (required && ArgUtil.checkParameterType(args, key) === null) {
5359
throw this.lError(`Parameter --${key} is not set but required`);
5460
}
55-
if (options && p !== undefined && !options.find((o) => o.key === p)) {
61+
if (
62+
options &&
63+
p !== undefined &&
64+
!(await StricktOption(options, this.handler)).find((o) => o.key === p)
65+
) {
5666
throw this.lError(`Parameter --${key} has invalid option ${p}`);
5767
}
5868
}

src/commands/base/HelpAction.ts

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
ShellCommandParentProps,
44
} from '../../class/ShellComand.js';
55
import getVersion from '../../utils/Version.js';
6+
import { StricktOption } from '../../lib/types.js';
67

78
export default class HelpAction extends ShellCommand {
89
constructor(props: ShellCommandParentProps) {
@@ -24,41 +25,45 @@ export default class HelpAction extends ShellCommand {
2425
return ' '.repeat(n);
2526
}
2627

27-
private inspectCmd(c: ShellCommand): void {
28+
private async inspectCmd(c: ShellCommand) {
2829
this.info(`${this.space(c.level + 1)}${c.name}: ${c.description}`);
29-
c.properties.forEach(({ key, type, required, description, options }) => {
30-
let t;
31-
switch (type) {
32-
case 'string':
33-
if (!options) {
34-
t = `<${type}>`;
35-
} else {
36-
t = `<option>`;
30+
await Promise.all(
31+
c.properties.map(
32+
async ({ key, type, required, description, options }) => {
33+
let t;
34+
switch (type) {
35+
case 'string':
36+
if (!options) {
37+
t = `<${type}>`;
38+
} else {
39+
t = `<option>`;
40+
}
41+
break;
42+
case 'null':
43+
t = '';
44+
break;
45+
default:
46+
t = `<${type}>`;
3747
}
38-
break;
39-
case 'null':
40-
t = '';
41-
break;
42-
default:
43-
t = `<${type}>`;
44-
}
45-
this.info(
46-
`${this.space(c.level + 2)}--${key} ${t} ${
47-
required ? '(required)' : '(optional)'
48-
} ${description ? `: ${description}` : ''}`,
49-
);
50-
if (options) {
51-
this.info(`${this.space(c.level + 3)} Options:`);
52-
options.forEach((o) => {
5348
this.info(
54-
`${this.space(c.level + 4)}${o.key}${
55-
o.description ? ` : ${o.description}` : ''
56-
}`,
49+
`${this.space(c.level + 2)}--${key} ${t} ${
50+
required ? '(required)' : '(optional)'
51+
} ${description ? `: ${description}` : ''}`,
5752
);
58-
});
59-
}
60-
});
61-
c.subCommands.forEach((cmd) => this.inspectCmd(cmd));
53+
if (options) {
54+
this.info(`${this.space(c.level + 3)} Options:`);
55+
(await StricktOption(options, this.handler)).forEach((o) => {
56+
this.info(
57+
`${this.space(c.level + 4)}${o.key}${
58+
o.description ? ` : ${o.description}` : ''
59+
}`,
60+
);
61+
});
62+
}
63+
},
64+
),
65+
);
66+
await Promise.all(c.subCommands.map((cmd) => this.inspectCmd(cmd)));
6267
if (c.level === 0) this.info('---------------------------------');
6368
}
6469

@@ -69,7 +74,9 @@ export default class HelpAction extends ShellCommand {
6974
`Usage: ${this.handler.getCmdName()} <command> [...<sub-command>] [options]`,
7075
);
7176
this.info('---------------------------------');
72-
this.handler.getCmds(true).forEach((c) => this.inspectCmd(c));
77+
await Promise.all(
78+
this.handler.getCmds(true).map((c) => this.inspectCmd(c)),
79+
);
7380
return true;
7481
}
7582
}

src/commands/base/InteractiveAction.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
getStringOrUndefined,
1414
ParamTypeRaw,
1515
PathCMDProp,
16+
StricktOption,
1617
} from '../../lib/types.js';
1718

1819
export default class InteractiveAction extends ShellCommand {
@@ -195,18 +196,22 @@ export default class InteractiveAction extends ShellCommand {
195196
}
196197
} else {
197198
options[prop.key] = await validation({
198-
promise: () =>
199-
select({
199+
promise: async () => {
200+
const option = prop.options
201+
? await StricktOption(prop.options, this.handler)
202+
: undefined;
203+
return select({
200204
message: prop.description ?? prop.key,
201205
default: getStringOrUndefined(prop.default),
202206
choices:
203-
prop.options?.map((o) => ({
207+
option?.map((o) => ({
204208
name: `${o.key} - ${o.description}`,
205209
value: o.key,
206210
disabled: false,
207211
})) ?? [],
208212
pageSize: this.handler.getPageSize(),
209-
}),
213+
});
214+
},
210215
validate: prop.validate,
211216
});
212217
}

src/lib/types.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ export interface IArgs {
7474
getParameterNull(key: string): boolean;
7575
}
7676

77+
export type BaseCMDOptionEl = {
78+
key: ParamTypeRaw;
79+
description?: string;
80+
};
81+
export type BaseCMDOption =
82+
| ((handler: IHandler) => Promise<BaseCMDOptionEl[]>)
83+
| BaseCMDOptionEl[];
84+
export async function StricktOption(option: BaseCMDOption, handler: IHandler) {
85+
if (typeof option === 'function') {
86+
return option(handler);
87+
}
88+
return option;
89+
}
7790
export type BaseCMDProperty = {
7891
/**
7992
* The key of the parameter
@@ -99,10 +112,7 @@ export type BaseCMDProperty = {
99112
/**
100113
* The options of the parameter
101114
*/
102-
options?: {
103-
key: ParamTypeRaw;
104-
description?: string;
105-
}[];
115+
options?: BaseCMDOption;
106116
};
107117
export type SimpleCMDProp = BaseCMDProperty & {
108118
type: 'boolean' | 'null';

0 commit comments

Comments
 (0)