breadc 0.3.0 → 0.4.0

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/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  Yet another Command Line Application Framework powered by [minimist](https://www.npmjs.com/package/minimist), but with fully strong [TypeScript](https://www.typescriptlang.org/) support.
6
6
 
7
+ ## Features
8
+
9
+ + ⚡️ **Light-weight**: Only 61 kB.
10
+ + 📖 **East to Learn**: Breadc is basically compatible with [cac](https://github.com/cacjs/cac) and there are only 4 APIs for building a CLI application: `command`, `option`, `action`, `run`.
11
+ + 💻 **TypeScript Infer**: IDE will automatically infer the type of your command action function.
12
+
7
13
  ## Installation
8
14
 
9
15
  ```bash
@@ -18,11 +24,11 @@ Try [./examples/echo.ts](./examples/echo.ts).
18
24
  import Breadc from 'breadc'
19
25
 
20
26
  const cli = Breadc('echo', { version: '1.0.0' })
21
- .option('--host <host>')
22
- .option('--port <port>')
27
+ .option('--host <host>', { default: 'localhost' })
28
+ .option('--port <port>', { construct: (port) => (port ? +port : 3000) });
23
29
 
24
30
  cli
25
- .command('[message]')
31
+ .command('[message]', 'Say something!')
26
32
  .action((message, option) => {
27
33
  const host = option.host;
28
34
  const port = option.port;
package/dist/index.cjs CHANGED
@@ -303,7 +303,7 @@ const _Option = class {
303
303
  this.description = config.description ?? "";
304
304
  this.required = format.indexOf("<") !== -1;
305
305
  this.default = config.default;
306
- this.construct = config.construct ?? ((text) => text ?? config.default ?? void 0);
306
+ this.construct = config.construct;
307
307
  }
308
308
  };
309
309
  let Option = _Option;
@@ -407,6 +407,13 @@ const _Command = class {
407
407
  options[name] = void 0;
408
408
  }
409
409
  }
410
+ if (rawOption.construct) {
411
+ options[name] = rawOption.construct(options[name]);
412
+ } else if (rawOption.default) {
413
+ if (!options[name]) {
414
+ options[name] = rawOption.default;
415
+ }
416
+ }
410
417
  }
411
418
  return {
412
419
  command: this,
@@ -580,17 +587,10 @@ class Breadc {
580
587
  }
581
588
  return map;
582
589
  }, {});
583
- const defaults = allowOptions.reduce((map, o) => {
584
- if (o.default) {
585
- map[o.name] = o.default;
586
- }
587
- return map;
588
- }, {});
589
590
  const argv = minimist(args, {
590
591
  string: allowOptions.filter((o) => o.type === "string").map((o) => o.name),
591
592
  boolean: allowOptions.filter((o) => o.type === "boolean").map((o) => o.name),
592
593
  alias,
593
- default: defaults,
594
594
  unknown: (t) => {
595
595
  if (t[0] !== "-")
596
596
  return true;
package/dist/index.d.ts CHANGED
@@ -250,10 +250,10 @@ function isConstructorOrProto (obj, key) {
250
250
  return key === 'constructor' && typeof obj[key] === 'function' || key === '__proto__';
251
251
  }
252
252
 
253
- interface OptionConfig<T = string> {
253
+ interface OptionConfig<F extends string, T = never> {
254
254
  description?: string;
255
255
  default?: T;
256
- construct?: (rawText?: string) => T;
256
+ construct?: (rawText: ExtractOptionType<F>) => T;
257
257
  }
258
258
  /**
259
259
  * Option
@@ -273,8 +273,8 @@ declare class Option<T extends string = string, F = string> {
273
273
  readonly description: string;
274
274
  readonly type: 'string' | 'boolean';
275
275
  readonly required: boolean;
276
- readonly construct: (rawText: string | undefined) => any;
277
- constructor(format: T, config?: OptionConfig<F>);
276
+ readonly construct?: (rawText: ExtractOptionType<T>) => F;
277
+ constructor(format: T, config?: OptionConfig<T, F>);
278
278
  }
279
279
 
280
280
  declare type ConditionFn = (args: minimist.ParsedArgs) => boolean;
@@ -294,8 +294,8 @@ declare class Command<F extends string = string, CommandOption extends object =
294
294
  condition?: ConditionFn;
295
295
  logger: Logger;
296
296
  });
297
- option<OF extends string, T = string>(format: OF, description: string, config?: Omit<OptionConfig<T>, 'description'>): Command<F, CommandOption & ExtractOption<OF>>;
298
- option<OF extends string, T = string>(format: OF, config?: OptionConfig<T>): Command<F, CommandOption & ExtractOption<OF>>;
297
+ option<OF extends string, T = undefined>(format: OF, description: string, config?: Omit<OptionConfig<OF, T>, 'description'>): Command<F, CommandOption & ExtractOption<OF, T>>;
298
+ option<OF extends string, T = undefined>(format: OF, config?: OptionConfig<OF, T>): Command<F, CommandOption & ExtractOption<OF, T>>;
299
299
  get hasConditionFn(): boolean;
300
300
  shouldRun(args: minimist.ParsedArgs): boolean;
301
301
  parseArgs(args: minimist.ParsedArgs, globalOptions: Option[]): ParseResult;
@@ -322,8 +322,8 @@ interface ParseResult {
322
322
  arguments: any[];
323
323
  options: Record<string, string>;
324
324
  }
325
- declare type ExtractOption<T extends string> = {
326
- [k in ExtractOptionName<T>]: ExtractOptionType<T>;
325
+ declare type ExtractOption<T extends string, D = undefined> = {
326
+ [k in ExtractOptionName<T>]: D extends undefined ? ExtractOptionType<T> : D;
327
327
  };
328
328
  /**
329
329
  * Extract option name type
@@ -357,8 +357,8 @@ declare class Breadc<GlobalOption extends object = {}> {
357
357
  constructor(name: string, option: AppOption);
358
358
  version(): string;
359
359
  help(command?: Command): string[];
360
- option<F extends string, T = string>(format: F, description: string, config?: Omit<OptionConfig<T>, 'description'>): Breadc<GlobalOption & ExtractOption<F>>;
361
- option<F extends string, T = string>(format: F, config?: OptionConfig<T>): Breadc<GlobalOption & ExtractOption<F>>;
360
+ option<F extends string, T = undefined>(format: F, description: string, config?: Omit<OptionConfig<F, T>, 'description'>): Breadc<GlobalOption & ExtractOption<F, T>>;
361
+ option<F extends string, T = undefined>(format: F, config?: OptionConfig<F, T>): Breadc<GlobalOption & ExtractOption<F, T>>;
362
362
  command<F extends string>(format: F, description: string, config?: Omit<CommandConfig, 'description'>): Command<F, GlobalOption>;
363
363
  command<F extends string>(format: F, config?: CommandConfig): Command<F, GlobalOption>;
364
364
  parse(args: string[]): ParseResult;
package/dist/index.mjs CHANGED
@@ -296,7 +296,7 @@ const _Option = class {
296
296
  this.description = config.description ?? "";
297
297
  this.required = format.indexOf("<") !== -1;
298
298
  this.default = config.default;
299
- this.construct = config.construct ?? ((text) => text ?? config.default ?? void 0);
299
+ this.construct = config.construct;
300
300
  }
301
301
  };
302
302
  let Option = _Option;
@@ -400,6 +400,13 @@ const _Command = class {
400
400
  options[name] = void 0;
401
401
  }
402
402
  }
403
+ if (rawOption.construct) {
404
+ options[name] = rawOption.construct(options[name]);
405
+ } else if (rawOption.default) {
406
+ if (!options[name]) {
407
+ options[name] = rawOption.default;
408
+ }
409
+ }
403
410
  }
404
411
  return {
405
412
  command: this,
@@ -573,17 +580,10 @@ class Breadc {
573
580
  }
574
581
  return map;
575
582
  }, {});
576
- const defaults = allowOptions.reduce((map, o) => {
577
- if (o.default) {
578
- map[o.name] = o.default;
579
- }
580
- return map;
581
- }, {});
582
583
  const argv = minimist(args, {
583
584
  string: allowOptions.filter((o) => o.type === "string").map((o) => o.name),
584
585
  boolean: allowOptions.filter((o) => o.type === "boolean").map((o) => o.name),
585
586
  alias,
586
- default: defaults,
587
587
  unknown: (t) => {
588
588
  if (t[0] !== "-")
589
589
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "breadc",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Yet another Command Line Application Framework with fully strong TypeScript support",
5
5
  "keywords": [
6
6
  "cli",