@clerc/plugin-help 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -39,34 +39,39 @@ const helpPlugin = (_options) => clerc.definePlugin({
39
39
  const { command, ...rest } = { ...defaultOptions, ..._options };
40
40
  if (command) {
41
41
  cli = cli.command("help", "Show help").on("help", (ctx) => {
42
- showHelp(cli, ctx, rest);
42
+ showHelp(ctx, rest);
43
43
  });
44
44
  }
45
45
  cli = cli.inspector((_ctx, next) => {
46
46
  const ctx = _ctx;
47
- if (ctx.flags.h || ctx.flags.help) {
47
+ if (_ctx.flags.h || _ctx.flags.help) {
48
48
  if (ctx.name === "help") {
49
- showSubcommandHelp(cli, {
49
+ showSubcommandHelp({
50
50
  ...ctx,
51
51
  name: "help"
52
52
  });
53
53
  return;
54
54
  }
55
55
  if (ctx.resolved) {
56
- showSubcommandHelp(cli, ctx);
56
+ showSubcommandHelp(ctx);
57
57
  } else {
58
- showHelp(cli, ctx, rest);
58
+ showHelp(ctx, rest);
59
59
  }
60
60
  return;
61
61
  }
62
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
63
+ showHelp(ctx, rest);
64
+ return;
65
+ }
62
66
  next();
63
67
  });
64
68
  return cli;
65
69
  }
66
70
  });
67
- function showHelp(cli, ctx, { examples, notes }) {
71
+ function showHelp(ctx, { examples, notes }) {
72
+ const { cli } = ctx;
68
73
  if (ctx.parameters.length > 0) {
69
- showSubcommandHelp(cli, ctx);
74
+ showSubcommandHelp(ctx);
70
75
  return;
71
76
  }
72
77
  cli._name && console.log(`${pc__default["default"].green(cli._name)} ${cli._version}`);
@@ -75,7 +80,7 @@ function showHelp(cli, ctx, { examples, notes }) {
75
80
  newline();
76
81
  }
77
82
  console.log(pc__default["default"].yellow("USAGE:"));
78
- console.log(` ${cli._name} <SUBCOMMAND> [OPTIONS]`);
83
+ console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
79
84
  newline();
80
85
  console.log(pc__default["default"].yellow("COMMANDS:"));
81
86
  const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
@@ -99,28 +104,18 @@ function showHelp(cli, ctx, { examples, notes }) {
99
104
  }
100
105
  }
101
106
  }
102
- function showSubcommandHelp(cli, ctx) {
107
+ function showSubcommandHelp(ctx) {
108
+ const { cli } = ctx;
103
109
  const commandName = String(ctx.name || ctx.parameters[0]);
104
110
  const commandToShowHelp = clerc.resolveCommand(cli._commands, commandName);
105
111
  if (!commandToShowHelp) {
106
- throw new clerc.NoSuchCommandsError(`No such command: ${commandName}`);
112
+ throw new clerc.NoSuchCommandError(`No such command: ${commandName}`);
107
113
  }
108
- console.log(`${pc__default["default"].green(`${cli._name} ${commandToShowHelp.name}`)} ${cli._version}`);
114
+ console.log(`${pc__default["default"].green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
109
115
  commandToShowHelp.description && console.log(commandToShowHelp.description);
110
116
  newline();
111
117
  console.log(pc__default["default"].yellow("USAGE:"));
112
118
  console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
113
- const parameters = commandToShowHelp.parameters || {};
114
- const parameterKeys = Object.keys(parameters);
115
- if (parameterKeys.length > 0) {
116
- newline();
117
- console.log(pc__default["default"].yellow("PARAMETERS:"));
118
- const parametersPadLength = getPadLength(parameterKeys);
119
- for (const [name, param] of Object.entries(parameters)) {
120
- const resuired = param.required ? pc__default["default"].red(" (required)") : "";
121
- console.log(` ${pc__default["default"].green(name.padEnd(parametersPadLength))}${param.description}${resuired}`);
122
- }
123
- }
124
119
  const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
125
120
  if (Object.keys(flagNameAndAlias).length > 0) {
126
121
  newline();
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import * as clerc from 'clerc';
2
- import { Clerc } from 'clerc';
3
2
 
4
3
  interface Options {
5
4
  /**
@@ -15,6 +14,6 @@ interface Options {
15
14
  */
16
15
  notes?: string[];
17
16
  }
18
- declare const helpPlugin: (_options?: Options) => clerc.Plugin<Clerc<{}>, Clerc<{}>>;
17
+ declare const helpPlugin: (_options?: Options) => clerc.Plugin<clerc.Clerc<{}>, clerc.Clerc<{}>>;
19
18
 
20
19
  export { helpPlugin };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { definePlugin, resolveCommand, NoSuchCommandsError } from 'clerc';
1
+ import { definePlugin, resolveCommand, NoSuchCommandError } from 'clerc';
2
2
  import pc from 'picocolors';
3
3
 
4
4
  const mustArray = (a) => Array.isArray(a) ? a : [a];
@@ -31,34 +31,39 @@ const helpPlugin = (_options) => definePlugin({
31
31
  const { command, ...rest } = { ...defaultOptions, ..._options };
32
32
  if (command) {
33
33
  cli = cli.command("help", "Show help").on("help", (ctx) => {
34
- showHelp(cli, ctx, rest);
34
+ showHelp(ctx, rest);
35
35
  });
36
36
  }
37
37
  cli = cli.inspector((_ctx, next) => {
38
38
  const ctx = _ctx;
39
- if (ctx.flags.h || ctx.flags.help) {
39
+ if (_ctx.flags.h || _ctx.flags.help) {
40
40
  if (ctx.name === "help") {
41
- showSubcommandHelp(cli, {
41
+ showSubcommandHelp({
42
42
  ...ctx,
43
43
  name: "help"
44
44
  });
45
45
  return;
46
46
  }
47
47
  if (ctx.resolved) {
48
- showSubcommandHelp(cli, ctx);
48
+ showSubcommandHelp(ctx);
49
49
  } else {
50
- showHelp(cli, ctx, rest);
50
+ showHelp(ctx, rest);
51
51
  }
52
52
  return;
53
53
  }
54
+ if (!ctx.resolved && ctx.parameters.length === 0 && Object.keys(ctx.flags).length === 0) {
55
+ showHelp(ctx, rest);
56
+ return;
57
+ }
54
58
  next();
55
59
  });
56
60
  return cli;
57
61
  }
58
62
  });
59
- function showHelp(cli, ctx, { examples, notes }) {
63
+ function showHelp(ctx, { examples, notes }) {
64
+ const { cli } = ctx;
60
65
  if (ctx.parameters.length > 0) {
61
- showSubcommandHelp(cli, ctx);
66
+ showSubcommandHelp(ctx);
62
67
  return;
63
68
  }
64
69
  cli._name && console.log(`${pc.green(cli._name)} ${cli._version}`);
@@ -67,7 +72,7 @@ function showHelp(cli, ctx, { examples, notes }) {
67
72
  newline();
68
73
  }
69
74
  console.log(pc.yellow("USAGE:"));
70
- console.log(` ${cli._name} <SUBCOMMAND> [OPTIONS]`);
75
+ console.log(` ${cli._name || "<CLI NAME>"} <SUBCOMMAND> [OPTIONS]`);
71
76
  newline();
72
77
  console.log(pc.yellow("COMMANDS:"));
73
78
  const commandNameAndAlias = generateNameAndAliasFromCommands(cli._commands);
@@ -91,28 +96,18 @@ function showHelp(cli, ctx, { examples, notes }) {
91
96
  }
92
97
  }
93
98
  }
94
- function showSubcommandHelp(cli, ctx) {
99
+ function showSubcommandHelp(ctx) {
100
+ const { cli } = ctx;
95
101
  const commandName = String(ctx.name || ctx.parameters[0]);
96
102
  const commandToShowHelp = resolveCommand(cli._commands, commandName);
97
103
  if (!commandToShowHelp) {
98
- throw new NoSuchCommandsError(`No such command: ${commandName}`);
104
+ throw new NoSuchCommandError(`No such command: ${commandName}`);
99
105
  }
100
- console.log(`${pc.green(`${cli._name} ${commandToShowHelp.name}`)} ${cli._version}`);
106
+ console.log(`${pc.green(`${cli._name}.${commandToShowHelp.name}`)} ${cli._version}`);
101
107
  commandToShowHelp.description && console.log(commandToShowHelp.description);
102
108
  newline();
103
109
  console.log(pc.yellow("USAGE:"));
104
110
  console.log(` ${cli._name} ${commandToShowHelp.name} [PARAMETERS] [FLAGS]`);
105
- const parameters = commandToShowHelp.parameters || {};
106
- const parameterKeys = Object.keys(parameters);
107
- if (parameterKeys.length > 0) {
108
- newline();
109
- console.log(pc.yellow("PARAMETERS:"));
110
- const parametersPadLength = getPadLength(parameterKeys);
111
- for (const [name, param] of Object.entries(parameters)) {
112
- const resuired = param.required ? pc.red(" (required)") : "";
113
- console.log(` ${pc.green(name.padEnd(parametersPadLength))}${param.description}${resuired}`);
114
- }
115
- }
116
111
  const flagNameAndAlias = generateFlagNameAndAliasFromCommand(commandToShowHelp);
117
112
  if (Object.keys(flagNameAndAlias).length > 0) {
118
113
  newline();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-help",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin help",
6
6
  "keywords": [
@@ -43,7 +43,7 @@
43
43
  "clerc": "*"
44
44
  },
45
45
  "dependencies": {
46
- "clerc": "0.2.0",
46
+ "clerc": "0.3.1",
47
47
  "picocolors": "^1.0.0"
48
48
  },
49
49
  "scripts": {