Skip to content

Commit

Permalink
feat(breadc): cli usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 4, 2023
1 parent e19cfa0 commit 0ad8218
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/breadc/src/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export function breadc(name: string, config: AppOption = {}) {
}

initContextOptions(
[makeHelpCommand(name, config), makeVersionCommand(name, config)],
[
makeHelpCommand(name, config, allCommands),
makeVersionCommand(name, config)
],
context
);
},
Expand Down
17 changes: 16 additions & 1 deletion packages/breadc/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ export function makeVersionCommand(name: string, config: AppOption): Option {
type HelpBlcok = string | Array<[string, string]>;
type HelpMessage = Array<HelpBlcok | (() => HelpBlcok[] | undefined)>;

export function makeHelpCommand(name: string, config: AppOption): Option {
export function makeHelpCommand(
name: string,
config: AppOption,
allCommands: Command[]
): Option {
function expandMessage(message: HelpMessage) {
const result: string[] = [];
for (const row of message) {
Expand Down Expand Up @@ -311,6 +315,15 @@ export function makeHelpCommand(name: string, config: AppOption): Option {
return commands;
}

const usage =
allCommands.length === 0
? `[OPTIONS]`
: allCommands.length === 1
? `[OPTIONS] ${allCommands[0].format}`
: allCommands.some((c) => c._default)
? `[OPTIONS] [COMMAND]`
: `[OPTIONS] <COMMAND>`;

const command: Command = {
async callback(parsed) {
// @ts-ignore
Expand All @@ -327,6 +340,8 @@ export function makeHelpCommand(name: string, config: AppOption): Option {
return undefined;
}
},
'',
`${bold(underline('Usage:'))} ${bold(name)} ${usage}`,
() => {
const cmds = expandCommands(cursor);
if (cmds.length > 0) {
Expand Down
18 changes: 18 additions & 0 deletions packages/breadc/test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] [COMMAND]
Commands:
cli [root] Start dev server
cli build [root] Build static site
Expand All @@ -97,6 +99,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] [COMMAND]
Commands:
cli [root] Start dev server
cli build [root] Build static site
Expand All @@ -121,6 +125,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] [COMMAND]
Commands:
cli build [root] Build static site
Expand All @@ -146,6 +152,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] <COMMAND>
Commands:
cli store ls [path] List path
cli store rm [path] Remove path
Expand All @@ -162,6 +170,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] <COMMAND>
Commands:
cli file info [path] Get file info
cli store ls [path] List path
Expand All @@ -179,6 +189,8 @@ describe('Help command', () => {
This is a cli app.
Usage: cli [OPTIONS] <COMMAND>
Commands:
cli store ls [path] List path
Expand All @@ -197,6 +209,8 @@ describe('Help command', () => {
expect(await cli.run(['-h'])).toMatchInlineSnapshot(`
"cli/unknown
Usage: cli [OPTIONS]
Options:
-h, --host <addr> Host address
--remote Enable remote
Expand All @@ -218,6 +232,8 @@ describe('Help command', () => {
expect(await cli.run(['-h'])).toMatchInlineSnapshot(`
"cli/unknown
Usage: cli [OPTIONS] build
Commands:
cli build Build static site
Expand All @@ -232,6 +248,8 @@ describe('Help command', () => {
expect(await cli.run(['build', '-h'])).toMatchInlineSnapshot(`
"cli/unknown
Usage: cli [OPTIONS] build
Commands:
cli build Build static site
Expand Down

0 comments on commit 0ad8218

Please sign in to comment.