breadc 0.4.3 → 0.4.6

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
@@ -59,7 +59,7 @@ const _Option = class {
59
59
  }
60
60
  };
61
61
  let Option = _Option;
62
- Option.OptionRE = /^(-[a-zA-Z], )?--([a-zA-Z]+)( \[[a-zA-Z]+\]| <[a-zA-Z]+>)?$/;
62
+ Option.OptionRE = /^(-[a-zA-Z0-9], )?--([a-zA-Z0-9\-]+)( \[[a-zA-Z0-9]+\]| <[a-zA-Z0-9]+>)?$/;
63
63
 
64
64
  const _Command = class {
65
65
  constructor(format, config) {
@@ -167,6 +167,11 @@ const _Command = class {
167
167
  }
168
168
  }
169
169
  }
170
+ for (const key of Object.keys(options)) {
171
+ if (!fullOptions.has(key)) {
172
+ delete options[key];
173
+ }
174
+ }
170
175
  return {
171
176
  command: this,
172
177
  arguments: argumentss,
@@ -235,6 +240,10 @@ class Breadc {
235
240
  constructor(name, option) {
236
241
  this.options = [];
237
242
  this.commands = [];
243
+ this.callbacks = {
244
+ pre: [],
245
+ post: []
246
+ };
238
247
  this.name = name;
239
248
  this._version = option.version ?? "unknown";
240
249
  this.description = option.description;
@@ -376,10 +385,16 @@ class Breadc {
376
385
  options
377
386
  };
378
387
  }
388
+ on(event, fn) {
389
+ this.callbacks[event].push(fn);
390
+ }
379
391
  async run(args) {
380
392
  const parsed = this.parse(args);
381
393
  if (parsed.command) {
382
- return await parsed.command.run(...parsed.arguments, parsed.options);
394
+ await Promise.all(this.callbacks.pre.map((fn) => fn(parsed.options)));
395
+ const returnValue = await parsed.command.run(...parsed.arguments, parsed.options);
396
+ await Promise.all(this.callbacks.post.map((fn) => fn(parsed.options)));
397
+ return returnValue;
383
398
  }
384
399
  }
385
400
  }
package/dist/index.d.ts CHANGED
@@ -117,6 +117,8 @@ declare class Breadc<GlobalOption extends object = {}> {
117
117
  command<F extends string>(format: F, description: string, config?: Omit<CommandConfig, 'description'>): Command<F, GlobalOption>;
118
118
  command<F extends string>(format: F, config?: CommandConfig): Command<F, GlobalOption>;
119
119
  parse(args: string[]): ParseResult;
120
+ private readonly callbacks;
121
+ on(event: 'pre' | 'post', fn: (option: GlobalOption) => void | Promise<void>): void;
120
122
  run(args: string[]): Promise<any>;
121
123
  }
122
124
 
package/dist/index.mjs CHANGED
@@ -52,7 +52,7 @@ const _Option = class {
52
52
  }
53
53
  };
54
54
  let Option = _Option;
55
- Option.OptionRE = /^(-[a-zA-Z], )?--([a-zA-Z]+)( \[[a-zA-Z]+\]| <[a-zA-Z]+>)?$/;
55
+ Option.OptionRE = /^(-[a-zA-Z0-9], )?--([a-zA-Z0-9\-]+)( \[[a-zA-Z0-9]+\]| <[a-zA-Z0-9]+>)?$/;
56
56
 
57
57
  const _Command = class {
58
58
  constructor(format, config) {
@@ -160,6 +160,11 @@ const _Command = class {
160
160
  }
161
161
  }
162
162
  }
163
+ for (const key of Object.keys(options)) {
164
+ if (!fullOptions.has(key)) {
165
+ delete options[key];
166
+ }
167
+ }
163
168
  return {
164
169
  command: this,
165
170
  arguments: argumentss,
@@ -228,6 +233,10 @@ class Breadc {
228
233
  constructor(name, option) {
229
234
  this.options = [];
230
235
  this.commands = [];
236
+ this.callbacks = {
237
+ pre: [],
238
+ post: []
239
+ };
231
240
  this.name = name;
232
241
  this._version = option.version ?? "unknown";
233
242
  this.description = option.description;
@@ -369,10 +378,16 @@ class Breadc {
369
378
  options
370
379
  };
371
380
  }
381
+ on(event, fn) {
382
+ this.callbacks[event].push(fn);
383
+ }
372
384
  async run(args) {
373
385
  const parsed = this.parse(args);
374
386
  if (parsed.command) {
375
- return await parsed.command.run(...parsed.arguments, parsed.options);
387
+ await Promise.all(this.callbacks.pre.map((fn) => fn(parsed.options)));
388
+ const returnValue = await parsed.command.run(...parsed.arguments, parsed.options);
389
+ await Promise.all(this.callbacks.post.map((fn) => fn(parsed.options)));
390
+ return returnValue;
376
391
  }
377
392
  }
378
393
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "breadc",
3
- "version": "0.4.3",
3
+ "version": "0.4.6",
4
4
  "description": "Yet another Command Line Application Framework with fully strong TypeScript support",
5
5
  "keywords": [
6
6
  "cli",
@@ -45,7 +45,7 @@
45
45
  "typescript": "^4.7.4",
46
46
  "unbuild": "^0.7.4",
47
47
  "vite": "^2.9.12",
48
- "vitest": "^0.15.2"
48
+ "vitest": "^0.16.0"
49
49
  },
50
50
  "packageManager": "pnpm@7.3.0",
51
51
  "scripts": {