Skip to content

Commit

Permalink
feat: option config
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Feb 3, 2023
1 parent 1aa881e commit 3bbbb86
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 25 deletions.
11 changes: 6 additions & 5 deletions examples/echo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Breadc from '../src';
import breadc from '../packages/breadc/src';

const cli = Breadc('echo', { version: '1.0.0' })
.option('--host [host]', { default: 'localhost' })
.option('--port [port]', { construct: (port) => (port ? +port : 3000) });
const cli = breadc('echo', { version: '1.0.0' })
.option('--host <host>', { default: 'localhost' })
.option('--port <port>', { default: '3000' });

cli.command('[message]', 'Say something!').action((message, option) => {
console.log(message ?? 'You can say anything!');
Expand All @@ -12,4 +12,5 @@ cli.command('[message]', 'Say something!').action((message, option) => {
console.log(`Port: ${port}`);
});

cli.run(process.argv.slice(2)).catch((err) => cli.logger.error(err.message));
cli.run(process.argv.slice(2));
// .catch((err) => cli.logger.error(err.message));
14 changes: 5 additions & 9 deletions examples/genType.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import Breadc from '../src/';
import breadc from '../packages/breadc/src';

const cli = Breadc('genType');
const cli = breadc('genType');

cli
.command('<maxDep>')
.option('--commandDep [number]', {
default: '3',
construct(t) {
return +t!;
}
})
.action((_maxDep, { commandDep }) => {
.option('--command-dep <number>', { default: '3' })
.action((_maxDep, option) => {
const maxDep = +_maxDep;
const commandDep = +option.commandDep;
const ans: string[][] = [];

for (let dep = maxDep; dep >= 1; dep--) {
Expand Down
19 changes: 11 additions & 8 deletions examples/vite.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import Breadc from '../src';
import breadc from '../packages/breadc/src';

const vite = Breadc('vite', {
const vite = breadc('vite', {
version: '1.0.0',
description: 'Next generation frontend tooling.'
})
.option('-c, --config <file>', `[string] use specified config file`)
.option('--base <path>', `[string] public base path (default: /)`)
.option('--base <path>', `[string] public base path (default: /)`, {
default: '/'
})
.option('-l, --logLevel <level>', `[string] info | warn | error | silent`)
.option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
.option('-d, --debug <feat>', `[string | boolean] show debug logs`)
.option('-f, --filter <filter>', `[string] filter debug logs`)
.option('-m, --mode <mode>', `[string] set env mode`);

vite
.command('dev [root]', { description: 'start dev server' })
.option('--host [host]', `[string] specify hostname`)
.option('--host <host>', `[string] specify hostname`)
.option('--port <port>', `[number] specify port`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--https', `<boolean> use TLS + HTTP/2`)
.option('--open <path>', `[boolean | string] open browser on startup`)
.option('--cors', `[boolean] enable CORS`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.option(
Expand All @@ -30,4 +32,5 @@ vite
console.log(option);
});

vite.run(process.argv.slice(2)).catch((err) => vite.logger.error(err.message));
vite.run(process.argv.slice(2));
// .catch((err) => vite.logger.error(err.message));
8 changes: 6 additions & 2 deletions packages/breadc/src/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ export function breadc(name: string, config: AppOption = {}) {
});

const breadc: Breadc = {
option(format, config): Breadc {
option(format, _config, _config2: any = {}) {
const config =
typeof _config === 'string'
? { description: _config, ..._config2 }
: _config;
const option = makeOption(format, config);
globalOptions.push(option);
return breadc;
},
command(text, _config = {}): Command {
command(text, _config = {}) {
const config =
typeof _config === 'string' ? { description: _config } : _config;

Expand Down
6 changes: 5 additions & 1 deletion packages/breadc/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export function makeCommand<F extends string = string>(
description: config.description ?? '',
_arguments: args,
_options: options,
option(format, config) {
option(format, _config, _config2: any = {}) {
const config =
typeof _config === 'string'
? { description: _config, ..._config2 }
: _config;
const option = makeOption(format, config);
options.push(option);
return command;
Expand Down
16 changes: 16 additions & 0 deletions packages/breadc/src/types/breadc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export interface AppOption {
export type ActionFn = (...args: any[]) => any;

export interface Breadc {
option<
F extends string = string,
T extends string | boolean = ExtractOptionType<F>
>(
format: F,
description?: string,
option?: OptionOption<T>
): Breadc;
option<
F extends string = string,
T extends string | boolean = ExtractOptionType<F>
Expand Down Expand Up @@ -42,6 +50,14 @@ export interface Command<F extends string = string> {

_options: Option[];

option<
OF extends string = string,
OT extends string | boolean = ExtractOptionType<F>
>(
format: OF,
description?: string,
option?: OptionOption<OT>
): Command<F>;
option<
OF extends string = string,
OT extends string | boolean = ExtractOptionType<F>
Expand Down

0 comments on commit 3bbbb86

Please sign in to comment.