just-bash-util 0.1.6 → 0.1.7

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
@@ -67,6 +67,7 @@ await serve.invoke({ port: 8080, entry: "app.ts" }, ctx);
67
67
  - `--` end-of-options separator (remaining tokens become positional args and are available via `meta.passthrough`)
68
68
  - Environment variable fallbacks for options
69
69
  - Levenshtein-based "did you mean?" suggestions for typos
70
+ - `transformArgs` callback to rewrite tokens before parsing — support non-standard shorthand syntax like `-5` → `-n 5`
70
71
  - Automatic error handling — thrown errors in handlers are caught and returned as clean `ExecResult` with `exitCode: 1`
71
72
 
72
73
  #### Options and flags
@@ -123,6 +124,27 @@ handler: (args, ctx, meta) => {
123
124
  }
124
125
  ```
125
126
 
127
+ #### Token rewriting with `transformArgs`
128
+
129
+ Commands can define a `transformArgs` callback to rewrite the raw token array before it reaches the parser. This is useful for supporting non-standard shorthand syntax that the parser wouldn't otherwise understand:
130
+
131
+ ```ts
132
+ cli.command("log", {
133
+ description: "Show commit log",
134
+ transformArgs: (tokens) =>
135
+ tokens.map((t) => (/^-(\d+)$/.test(t) ? `-n${t.slice(1)}` : t)),
136
+ options: {
137
+ maxCount: o.number().alias("n").describe("Limit output to n commits"),
138
+ },
139
+ handler: (args) => {
140
+ // git log -5 → tokens rewritten to -n5 → args.maxCount === 5
141
+ return { stdout: `showing ${args.maxCount} commits`, stderr: "", exitCode: 0 };
142
+ },
143
+ });
144
+ ```
145
+
146
+ The callback receives a mutable copy of the tokens and returns the rewritten array. It runs only in `execute()` (token-based invocation) — `invoke()` takes typed args directly, so there's nothing to transform.
147
+
126
148
  ### `just-bash-util/config` — Config file discovery
127
149
 
128
150
  Cosmiconfig-style config search that walks up the directory tree, trying conventional filenames at each level. Comments and trailing commas are supported out of the box.
@@ -550,10 +550,11 @@ var Command = class _Command {
550
550
  examples;
551
551
  omitInherited;
552
552
  handler;
553
+ transformArgs;
553
554
  children = /* @__PURE__ */ new Map();
554
555
  parent;
555
556
  /** @internal */
556
- constructor(name, description, options, args, examples, omitInherited, handler) {
557
+ constructor(name, description, options, args, examples, omitInherited, handler, transformArgs) {
557
558
  this.name = name;
558
559
  this.description = description;
559
560
  this.options = options;
@@ -561,6 +562,7 @@ var Command = class _Command {
561
562
  this.examples = examples;
562
563
  this.omitInherited = omitInherited;
563
564
  this.handler = handler;
565
+ this.transformArgs = transformArgs;
564
566
  }
565
567
  // --------------------------------------------------------------------------
566
568
  // Tree building
@@ -575,7 +577,8 @@ var Command = class _Command {
575
577
  resolveArgsInput(config.args),
576
578
  config.examples ?? [],
577
579
  omitSet,
578
- config.handler
580
+ config.handler,
581
+ config.transformArgs
579
582
  );
580
583
  child.parent = this;
581
584
  this.children.set(name, child);
@@ -763,7 +766,8 @@ var Command = class _Command {
763
766
  return { stdout: generateHelp(this), stderr: "", exitCode: 0 };
764
767
  }
765
768
  if (this.handler) {
766
- const parsed = parseArgs(this.allOptions, this.args, [...tokens], env);
769
+ const effective = this.transformArgs ? this.transformArgs([...tokens]) : [...tokens];
770
+ const parsed = parseArgs(this.allOptions, this.args, effective, env);
767
771
  if (!parsed.ok) {
768
772
  return { stdout: "", stderr: formatErrors(parsed.errors), exitCode: 1 };
769
773
  }
@@ -797,7 +801,8 @@ function command(name, config) {
797
801
  resolveArgsInput(config.args),
798
802
  config.examples ?? [],
799
803
  /* @__PURE__ */ new Set(),
800
- config.handler
804
+ config.handler,
805
+ config.transformArgs
801
806
  );
802
807
  }
803
808
  function hasHelpFlag(tokens) {
@@ -177,6 +177,7 @@ declare class Command<THandlerArgs extends object = {}, TInvokeArgs extends obje
177
177
  readonly examples: readonly string[];
178
178
  readonly omitInherited: ReadonlySet<string>;
179
179
  readonly handler?: Handler<any>;
180
+ readonly transformArgs?: (tokens: string[]) => string[];
180
181
  readonly children: Map<string, Command<any, any>>;
181
182
  parent?: Command<any, any>;
182
183
  /** @internal — phantom type carrying the resolved handler args */
@@ -184,7 +185,7 @@ declare class Command<THandlerArgs extends object = {}, TInvokeArgs extends obje
184
185
  /** @internal — phantom type carrying the resolved invoke args */
185
186
  readonly _invokeArgs: TInvokeArgs;
186
187
  /** @internal */
187
- constructor(name: string, description: string, options: OptionsSchema, args: ArgsSchema, examples: readonly string[], omitInherited: ReadonlySet<string>, handler: Handler<any> | undefined);
188
+ constructor(name: string, description: string, options: OptionsSchema, args: ArgsSchema, examples: readonly string[], omitInherited: ReadonlySet<string>, handler: Handler<any> | undefined, transformArgs?: (tokens: string[]) => string[]);
188
189
  /** Add a subcommand. Returns the child command for further nesting. */
189
190
  command<TOpts extends OptionsInput = {}, const TArgs extends ArgsInput = [], const TOmit extends string[] = []>(name: string, config: {
190
191
  readonly description: string;
@@ -192,6 +193,7 @@ declare class Command<THandlerArgs extends object = {}, TInvokeArgs extends obje
192
193
  readonly args?: TArgs;
193
194
  readonly examples?: readonly string[];
194
195
  readonly omitInherited?: TOmit;
196
+ readonly transformArgs?: (tokens: string[]) => string[];
195
197
  readonly handler?: Handler<Prettify<Omit<THandlerArgs, TOmit[number]> & InferOptionsFromInput<TOpts> & InferArgsFromInput<TArgs>>>;
196
198
  }): Command<Prettify<Omit<THandlerArgs, TOmit[number]> & InferOptionsFromInput<TOpts> & InferArgsFromInput<TArgs>>, Prettify<Omit<TInvokeArgs, TOmit[number]> & InferInvokeOptions<TOpts> & InferInvokeArgs<TArgs>>>;
197
199
  /** Full path from root (e.g. "mycli db migrate") */
@@ -258,6 +260,7 @@ declare function command<TOpts extends OptionsInput = {}, const TArgs extends Ar
258
260
  readonly options?: TOpts;
259
261
  readonly args?: TArgs;
260
262
  readonly examples?: readonly string[];
263
+ readonly transformArgs?: (tokens: string[]) => string[];
261
264
  readonly handler?: Handler<Prettify<InferOptionsFromInput<TOpts> & InferArgsFromInput<TArgs>>>;
262
265
  }): Command<Prettify<InferOptionsFromInput<TOpts> & InferArgsFromInput<TArgs>>, Prettify<InferInvokeOptions<TOpts> & InferInvokeArgs<TArgs>>>;
263
266
  /**
@@ -8,7 +8,7 @@ import {
8
8
  generateHelp,
9
9
  o,
10
10
  parseArgs
11
- } from "../chunk-4J5EECVQ.js";
11
+ } from "../chunk-B6JJTTPV.js";
12
12
  export {
13
13
  Command,
14
14
  a,
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  generateHelp,
9
9
  o,
10
10
  parseArgs
11
- } from "./chunk-4J5EECVQ.js";
11
+ } from "./chunk-B6JJTTPV.js";
12
12
  import {
13
13
  findUp,
14
14
  loadConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "just-bash-util",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "CLI command framework, config file discovery, and path utilities for just-bash",
5
5
  "type": "module",
6
6
  "license": "MIT",