@gunshi/bone 1.0.0-beta.0 → 1.0.0-beta.2

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/lib/index.d.ts CHANGED
@@ -55,9 +55,6 @@ interface ArgToken {
55
55
  */
56
56
  inlineValue?: boolean | undefined;
57
57
  }
58
- /**
59
- * Parser Options.
60
- */
61
58
  //#endregion
62
59
  //#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver.d.ts
63
60
  //#region src/resolver.d.ts
@@ -638,7 +635,7 @@ interface Args {
638
635
  *
639
636
  * @typeParam T - {@link Args | Arguments} which is an object that defines the command line arguments.
640
637
  */
641
- type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
638
+ type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]>; }> : {
642
639
  [option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
643
640
  };
644
641
  /**
@@ -670,7 +667,7 @@ type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T
670
667
  *
671
668
  * @internal
672
669
  */
673
- type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: IsPresentArg<A[Arg]> extends true ? unknown : V[Arg] } & { -readonly [Arg in keyof A as IsPresentArg<A[Arg]> extends true ? Arg : never]: V[Arg] } extends (infer P) ? { [K in keyof P]: P[K] } : never;
670
+ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: IsPresentArg<A[Arg]> extends true ? unknown : V[Arg]; } & { -readonly [Arg in keyof A as IsPresentArg<A[Arg]> extends true ? Arg : never]: V[Arg]; } extends (infer P) ? { [K in keyof P]: P[K]; } : never;
674
671
  /**
675
672
  * Whether an argument with the schema `S` has a value: with a default, with `required: true` or as
676
673
  * a required positional argument. For a union of schemas, only when each schema of the union has
@@ -678,16 +675,6 @@ type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -r
678
675
  * value, as {@link FilterArgs} does.
679
676
  */
680
677
  type IsPresentArg<S extends ArgSchema> = S extends ArgSchema ? true extends (S['default'] extends {} ? true : S['required'] extends true ? true : false) ? true : IsRequiredPositionalArg<S> : never;
681
- /**
682
- * Filters the arguments based on their default values.
683
- *
684
- * @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
685
- * @typeParam V - Resolvable argument values.
686
- * @typeParam K - Key of the {@link ArgSchema | argument schema} to filter by.
687
- *
688
- * @internal
689
- */
690
-
691
678
  type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positional' ? A['multiple'] extends true ? A['required'] extends true ? true : false : MayBeUnrequired<A> extends true ? A['default'] extends {} ? true : false : true : false;
692
679
  /**
693
680
  * Whether the `required` of `A` may be `false`. An optional `required` of type `boolean`, as in
@@ -698,10 +685,6 @@ type IsRequiredPositionalArg<A extends ArgSchema> = A['type'] extends 'positiona
698
685
  type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] extends [{
699
686
  required: unknown;
700
687
  }] ? true : boolean | undefined extends A['required'] ? false : true : false;
701
- /**
702
- * An arguments for {@link resolveArgs | resolve arguments}.
703
- */
704
-
705
688
  /**
706
689
  * Tracks which arguments were explicitly provided by the user.
707
690
  *
@@ -714,7 +697,7 @@ type MayBeUnrequired<A extends ArgSchema> = false extends A['required'] ? [A] ex
714
697
  *
715
698
  * @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
716
699
  */
717
- type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
700
+ type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean; };
718
701
  //#endregion
719
702
  //#region ../gunshi/src/plugin/context.d.ts
720
703
  /**
@@ -848,7 +831,7 @@ type Awaitable<T> = T | Promise<T>;
848
831
  *
849
832
  * @typeParam T - The type to be prettified.
850
833
  */
851
- type Prettify<T> = { [K in keyof T]: T[K] } & {};
834
+ type Prettify<T> = { [K in keyof T]: T[K]; } & {};
852
835
  /**
853
836
  * Extend command context type. This type is used to extend the command context with additional properties at {@linkcode CommandContext.extensions}.
854
837
  *
@@ -1649,11 +1632,4 @@ declare function cli<E extends ExtendContext = ExtendContext, G extends GunshiPa
1649
1632
  */
1650
1633
  declare function cli<G extends GunshiParams = DefaultGunshiParams>(args: string[], entry: Command<G> | CommandRunner<G> | LazyCommand<G>, options?: CliOptions<G>): Promise<string | undefined>;
1651
1634
  //#endregion
1652
- //#region src/index.d.ts
1653
- /**
1654
- * @author kazuya kawaguchi (a.k.a. kazupon)
1655
- * @license MIT
1656
- */
1657
-
1658
- //#endregion
1659
- export { type ArgSchema, type ArgToken, type ArgValues, type Args, Awaitable, CliOptions, Command, CommandCallMode, CommandContext, CommandContextCore, CommandContextExtension, CommandDecorator, CommandEnvironment, CommandExamplesFetcher, CommandLoader, CommandRunner, Commandable, DefaultGunshiParams, ExtendContext, ExtractArgExplicitlyProvided, ExtractArgs, ExtractExtensions, GunshiParams, GunshiParamsConstraint, LazyCommand, MergeGunshiExtensions, NormalizeToGunshiParams, Prettify, RendererDecorator, RenderingOptions, SubCommandable, ValidationErrorsDecorator, cli };
1635
+ export { type ArgSchema, type ArgToken, type ArgValues, type Args, type Awaitable, type CliOptions, type Command, type CommandCallMode, type CommandContext, type CommandContextCore, type CommandContextExtension, type CommandDecorator, type CommandEnvironment, type CommandExamplesFetcher, type CommandLoader, type CommandRunner, type Commandable, type DefaultGunshiParams, type ExtendContext, type ExtractArgExplicitlyProvided, type ExtractArgs, type ExtractExtensions, type GunshiParams, type GunshiParamsConstraint, type LazyCommand, type MergeGunshiExtensions, type NormalizeToGunshiParams, type Prettify, type RendererDecorator, type RenderingOptions, type SubCommandable, type ValidationErrorsDecorator, cli };
package/lib/index.js CHANGED
@@ -1254,37 +1254,6 @@ function create(obj = null) {
1254
1254
  return Object.create(obj);
1255
1255
  }
1256
1256
  /**
1257
- * Resolve the arguments of a command, the way gunshi parses and renders them.
1258
- *
1259
- * The global options belong to no command definition, so they are merged into the arguments of the
1260
- * command that runs. An argument of the command shadows the global option of the same name, and it
1261
- * shadows the same way by short name: a global option gives up its short name to an argument of the
1262
- * command that claims the same letter, and keeps its long name.
1263
- *
1264
- * @param globalOptions - The global options that plugins registered with `addGlobalOption`
1265
- * @param args - The arguments that the command declares
1266
- * @returns The merged arguments
1267
- */
1268
- function resolveCommandArgs(globalOptions, args) {
1269
- return Object.assign(create(), resolveGlobalOptions(globalOptions, args), args);
1270
- }
1271
- function resolveGlobalOptions(globalOptions, args) {
1272
- if (!globalOptions) return;
1273
- const shortNames = /* @__PURE__ */ new Set();
1274
- for (const schema of Object.values(args || {})) if (schema.type !== "positional" && schema.short) shortNames.add(schema.short);
1275
- const resolved = create();
1276
- for (const [name, schema] of globalOptions)
1277
- /**
1278
- * NOTE(kazupon): a copy, because the schema is the one that the plugin registered, which every
1279
- * command of the CLI shares. Only this command gives up the short name.
1280
- */
1281
- resolved[name] = schema.short && shortNames.has(schema.short) ? {
1282
- ...schema,
1283
- short: void 0
1284
- } : schema;
1285
- return resolved;
1286
- }
1287
- /**
1288
1257
  * Log a message to the console.
1289
1258
  *
1290
1259
  * @param args - Arguments to log
@@ -1796,6 +1765,47 @@ function resolveDependencies(plugins) {
1796
1765
  return sorted;
1797
1766
  }
1798
1767
  //#endregion
1768
+ //#region ../gunshi/src/cli/args.ts
1769
+ /**
1770
+ * @author kazuya kawaguchi (a.k.a. kazupon)
1771
+ * @license MIT
1772
+ */
1773
+ /**
1774
+ * Resolve the arguments of a command, the way gunshi parses and renders them.
1775
+ *
1776
+ * The global options belong to no command definition, so they are merged into the arguments of the
1777
+ * command that runs. An argument of the command shadows the global option of the same name, and it
1778
+ * shadows the same way by short name: a global option gives up its short name to an argument of the
1779
+ * command that claims the same letter, and keeps its long name.
1780
+ *
1781
+ * NOTE(kazupon): this is not exported from the entry points. `@gunshi/plugin-completion` completes
1782
+ * the arguments the same way with the copy in `@gunshi/shared`, and the tests of that copy pin it
1783
+ * to the results of this one, so a change here has to be made there too.
1784
+ *
1785
+ * @param globalOptions - The global options that plugins registered with `addGlobalOption`
1786
+ * @param args - The arguments that the command declares
1787
+ * @returns The merged arguments
1788
+ */
1789
+ function resolveCommandArgs(globalOptions, args) {
1790
+ return Object.assign(create(), resolveGlobalOptions(globalOptions, args), args);
1791
+ }
1792
+ function resolveGlobalOptions(globalOptions, args) {
1793
+ if (!globalOptions) return;
1794
+ const shortNames = /* @__PURE__ */ new Set();
1795
+ for (const schema of Object.values(args || {})) if (schema.type !== "positional" && schema.short) shortNames.add(schema.short);
1796
+ const resolved = create();
1797
+ for (const [name, schema] of globalOptions)
1798
+ /**
1799
+ * NOTE(kazupon): a copy, because the schema is the one that the plugin registered, which every
1800
+ * command of the CLI shares. Only this command gives up the short name.
1801
+ */
1802
+ resolved[name] = schema.short && shortNames.has(schema.short) ? {
1803
+ ...schema,
1804
+ short: void 0
1805
+ } : schema;
1806
+ return resolved;
1807
+ }
1808
+ //#endregion
1799
1809
  //#region ../gunshi/src/cli/core.ts
1800
1810
  /**
1801
1811
  * @author kazuya kawaguchi (a.k.a. kazupon)
@@ -1868,7 +1878,7 @@ async function cliCore(argv, entry, options, plugins, usageOnly = false) {
1868
1878
  * the merge, so the core says which ones are in effect for this command (#745).
1869
1879
  */
1870
1880
  cliOptions.globalOptions = resolveEffectiveGlobalOptions(pluginContext.globalOptions, commandArgs, args);
1871
- const skipPositional = targetDepth > 0 ? targetDepth - 1 : -1;
1881
+ const skipPositional = resolveSkipPositional(tokens, pluginContext.globalOptions, resolvedCommand, targetDepth);
1872
1882
  const diagnostic = selection.diagnostic === true;
1873
1883
  const { explicit, values, positionals, rest, error } = diagnostic ? {
1874
1884
  explicit: create(),
@@ -2289,6 +2299,25 @@ function getEntryRoutingCommand(entry, options) {
2289
2299
  return entry;
2290
2300
  }
2291
2301
  function getRoutingPositionals(tokens, globalOptions, command) {
2302
+ /**
2303
+ * NOTE(kazupon): an empty argument is never a command name. `getPositionalTokens()` skips it when it
2304
+ * collects the candidates, and the routing of each candidate must skip it too; otherwise the routing
2305
+ * looks for a sub-command named `''`, and fails with `inconsistent-options` (#781).
2306
+ */
2307
+ return readPositionals(tokens, globalOptions, command).filter((value) => value !== "");
2308
+ }
2309
+ /**
2310
+ * Read the positional arguments as `resolveArgs()` reads them for a command, empty ones included.
2311
+ *
2312
+ * The schemas are projected down to the metadata that classifies the tokens, so no code of the user,
2313
+ * such as `parse`, runs.
2314
+ *
2315
+ * @param tokens - Tokenized command-line arguments
2316
+ * @param globalOptions - Global option schemas
2317
+ * @param command - The command whose arguments classify the tokens
2318
+ * @returns The positional arguments, in the order that `skipPositional` of args-tokens counts them
2319
+ */
2320
+ function readPositionals(tokens, globalOptions, command) {
2292
2321
  const effective = resolveCommandArgs(globalOptions, getCommandArgs(command));
2293
2322
  const routingArgs = create();
2294
2323
  for (const [name, schema] of Object.entries(effective)) {
@@ -2300,15 +2329,36 @@ function getRoutingPositionals(tokens, globalOptions, command) {
2300
2329
  toKebab: schema.toKebab
2301
2330
  };
2302
2331
  }
2303
- /**
2304
- * NOTE(kazupon): an empty argument is never a command name. `getPositionalTokens()` skips it when it
2305
- * collects the candidates, and the routing of each candidate must skip it too; otherwise the routing
2306
- * looks for a sub-command named `''`, and fails with `inconsistent-options` (#781).
2307
- */
2308
2332
  return resolveArgs(routingArgs, tokens, {
2309
2333
  shortGrouping: true,
2310
2334
  toKebab: command.toKebab
2311
- }).positionals.filter((value) => value !== "");
2335
+ }).positionals;
2336
+ }
2337
+ /**
2338
+ * Resolve `skipPositional` of args-tokens: the index of the last positional argument to skip.
2339
+ *
2340
+ * NOTE(kazupon): the command names are the first positional arguments that are not empty, and the
2341
+ * last one is at `depth - 1` only when no empty argument comes before it. `skipPositional` counts every
2342
+ * positional argument, the empty ones too, so `app '' build x` gave `build` to the first positional
2343
+ * argument of `build` (#793). The index is found in the reading of `resolveArgs()`, in which the values
2344
+ * of the options are already taken out, so that an empty value of an option is not counted.
2345
+ *
2346
+ * @param tokens - Tokenized command-line arguments
2347
+ * @param globalOptions - Global option schemas
2348
+ * @param command - The command whose arguments are resolved
2349
+ * @param depth - The number of command names
2350
+ * @returns The index of the last command name among the positional arguments, or `-1` for the entry
2351
+ */
2352
+ function resolveSkipPositional(tokens, globalOptions, command, depth) {
2353
+ if (depth === 0) return -1;
2354
+ if (!tokens.some((token) => token.kind === "positional" && token.value === "")) return depth - 1;
2355
+ let names = 0;
2356
+ for (const [index, value] of readPositionals(tokens, globalOptions, command).entries()) {
2357
+ if (value === "") continue;
2358
+ names++;
2359
+ if (names === depth) return index;
2360
+ }
2361
+ return depth - 1;
2312
2362
  }
2313
2363
  function sameCommandPath(left, right) {
2314
2364
  return left.length === right.length && left.every((name, index) => name === right[index]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/bone",
3
3
  "description": "gunshi minimum",
4
- "version": "1.0.0-beta.0",
4
+ "version": "1.0.0-beta.2",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -51,15 +51,15 @@
51
51
  }
52
52
  },
53
53
  "devDependencies": {
54
+ "@gunshi/definition": "1.0.0-beta.2",
55
+ "@gunshi/plugin-global": "1.0.0-beta.2",
56
+ "@gunshi/plugin-renderer": "1.0.0-beta.2",
54
57
  "deno": "^2.7.14",
58
+ "gunshi": "1.0.0-beta.2",
55
59
  "jsr": "^0.14.3",
56
60
  "jsr-exports-lint": "^0.4.2",
57
61
  "publint": "^0.3.20",
58
- "tsdown": "0.21.0",
59
- "@gunshi/definition": "1.0.0-beta.0",
60
- "@gunshi/plugin-global": "1.0.0-beta.0",
61
- "@gunshi/plugin-renderer": "1.0.0-beta.0",
62
- "gunshi": "1.0.0-beta.0"
62
+ "tsdown": "0.22.14"
63
63
  },
64
64
  "scripts": {
65
65
  "build": "tsdown",