Skip to content

Commit

Permalink
feat: custom builtin command help
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Mar 27, 2023
1 parent 30deba0 commit ff5ec6e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
13 changes: 9 additions & 4 deletions packages/breadc/src/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import { makeHelpCommand, makeVersionCommand } from './builtin';
export function breadc(name: string, config: AppOption = {}) {
let defaultCommand: Command | undefined = undefined;
const allCommands: Command[] = [];
const globalOptions: Option[] = [
makeHelpCommand(name, config, allCommands),
makeVersionCommand(name, config)
];
const globalOptions: Option[] = [];

if (config.builtin?.version !== false) {
globalOptions.push(makeVersionCommand(name, config));
}
if (config.builtin?.help !== false) {
globalOptions.push(makeHelpCommand(name, config, allCommands));
}

const container = makePluginContainer(config.plugins);

const root = makeTreeNode({
Expand Down
24 changes: 21 additions & 3 deletions packages/breadc/src/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,24 @@ import { makeTreeNode, TreeNode } from './parser';
import { AppOption, Command, Option } from './types';

export function makeVersionCommand(name: string, config: AppOption): Option {
let description = 'Print version';
if (typeof config.builtin?.version === 'object') {
if (config.builtin.version.description) {
description = config.builtin.version.description;
}
}

const node = makeTreeNode({
next() {
return false;
},
finish() {
return () => {
const text = `${name}/${config.version ? config.version : 'unknown'}`;
const text =
typeof config.builtin?.version === 'object' &&
config.builtin.version.content
? config.builtin.version.content
: `${name}/${config.version ? config.version : 'unknown'}`;
console.log(text);
return text;
};
Expand All @@ -25,7 +36,7 @@ export function makeVersionCommand(name: string, config: AppOption): Option {
type: 'boolean',
initial: undefined,
order: 999999999 + 1,
description: 'Print version',
description,
parse() {
return node;
}
Expand Down Expand Up @@ -93,6 +104,13 @@ export function makeHelpCommand(
return [...alias.values()];
}

let description = 'Print help';
if (typeof config.builtin?.help === 'object') {
if (config.builtin.help.description) {
description = config.builtin.help.description;
}
}

const node = makeTreeNode({
next() {
return false;
Expand Down Expand Up @@ -162,7 +180,7 @@ export function makeHelpCommand(
short: 'h',
type: 'boolean',
initial: undefined,
description: 'Print help',
description,
order: 999999999,
parse(cursor, _token, context) {
context.meta.__cursor__ = cursor;
Expand Down
6 changes: 5 additions & 1 deletion packages/breadc/src/types/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export interface AppOption {

plugins?: Partial<Plugin>[];

// logger?: Partial<Logger> | LoggerFn;
builtin?: {
version?: false | Partial<{ description: string; content: string }>;

help?: false | Partial<{ description: string }>;
};
}

export interface Breadc<GlobalOption extends object = {}> {
Expand Down
36 changes: 36 additions & 0 deletions packages/breadc/test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ describe('Version Command', () => {
'"cli/1.0.0"'
);
});

it('should print custom version', async () => {
const cli = breadc('cli', {
builtin: {
version: {
content: 'Hello, this is version'
}
}
});

expect(await cli.run(['-v'])).toMatchInlineSnapshot(
'"Hello, this is version"'
);
expect(await cli.run(['--version'])).toMatchInlineSnapshot(
'"Hello, this is version"'
);
});
});

describe('Help command', () => {
Expand Down Expand Up @@ -358,4 +375,23 @@ describe('Help command', () => {
"
`);
});

it('should print custom help', async () => {
const cli = breadc('cli', {
builtin: {
version: { description: '输出版本' },
help: { description: '输出帮助' }
}
});
expect(await cli.run(['-h'])).toMatchInlineSnapshot(`
"cli/unknown
Usage: cli [OPTIONS]
Options:
-h, --help 输出帮助
-v, --version 输出版本
"
`);
});
});

0 comments on commit ff5ec6e

Please sign in to comment.