@silvery/commander 0.8.1 → 0.8.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bjørn Stabell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -49,7 +49,7 @@ const { env, force, verbose } = program.opts()
49
49
  // └──────────────── "dev" | "staging" | "prod"
50
50
  ```
51
51
 
52
- With plain Commander, `opts()` returns `Record<string, any>` — every value is untyped. With `@silvery/commander`, each option's type is inferred from its schema: `z.port` produces `number`, `z.enum(...)` produces a union, `z.csv` produces `string[]`. Invalid values are rejected at parse time with clear error messages — not silently passed through as strings.
52
+ With plain Commander, `opts()` returns `Record<string, any>` — every value is untyped. With `@silvery/commander`, each `.option()` call narrows the return type of `.opts()` — no manual interfaces needed. Types are inferred from flag syntax (`--verbose` → `boolean`, `--port <n>` → `string`), parser functions (`port` → `number`, `csv` → `string[]`), schemas (`z.enum(...)` → union), and choices arrays. Accessing a nonexistent option is a compile error. Invalid values are rejected at parse time with clear error messages.
53
53
 
54
54
  [Zod](https://github.com/colinhacks/zod) is entirely optional — `z` is tree-shaken from your bundle if you don't import it. Without Zod, use the built-in types (`port`, `int`, `csv`) or plain Commander.
55
55
 
@@ -75,6 +75,80 @@ Options with [Zod](https://github.com/colinhacks/zod) schemas or built-in types
75
75
 
76
76
  Full reference, type table, and API details at **[silvery.dev/reference/commander](https://silvery.dev/reference/commander)**.
77
77
 
78
+ ## Typed `.action()` and `.actionMerged()`
79
+
80
+ `.action()` is Commander-native: positional arguments, then the options object, then the command instance. Every positional is fully typed from the `.argument()` chain, and `opts` is fully typed from the `.option()` chain:
81
+
82
+ ```ts
83
+ new Command("deploy")
84
+ .argument("<service>", "Service to deploy")
85
+ .argument("[env]", "Environment", ["dev", "staging", "prod"] as const)
86
+ .option("--verbose", "Verbose output")
87
+ .action((service, env, opts) => {
88
+ service // string
89
+ env // "dev" | "staging" | "prod" | undefined
90
+ opts.verbose // boolean | undefined
91
+ })
92
+ ```
93
+
94
+ For commands with several positional arguments, `.actionMerged()` provides a flat named-object form:
95
+
96
+ ```ts
97
+ new Command("deploy")
98
+ .argument("<service>", "Service to deploy")
99
+ .argument("[env]", "Environment", ["dev", "staging", "prod"] as const)
100
+ .option("-p, --port <n>", "Port", port)
101
+ .option("--verbose", "Verbose output")
102
+ .actionMerged(({ service, env, port, verbose }, cmd) => {
103
+ // Everything in one destructured object.
104
+ // `cmd` is the Command instance.
105
+ })
106
+ ```
107
+
108
+ `.actionMerged()` merges all arguments (by camelCased name) and options into a single `params` object. It's a convenience over `.action()` — use whichever reads better for your command.
109
+
110
+ **Picking between them:**
111
+
112
+ - `.action()` — better for commands with zero or one positional argument, or when you want access to the command instance as a trailing argument. Matches Commander.js muscle memory.
113
+ - `.actionMerged()` — better for commands with 2+ positional arguments, where a flat object is nicer than nested destructure.
114
+
115
+ Both forms are fully typed end-to-end. The only runtime difference is whether arguments are merged into the params object or passed positionally.
116
+
117
+ ### Two ways to declare positional arguments
118
+
119
+ Both forms are fully typed end-to-end and produce equivalent `Command<{}, Args, ArgsRecord>`:
120
+
121
+ ```ts
122
+ // Inline form — Commander.js native shorthand
123
+ .command("deploy <service> [env]")
124
+ .action((service, env, opts) => { /* service: string, env: string | undefined */ })
125
+
126
+ // Explicit form — chained .argument() calls
127
+ .command("deploy")
128
+ .argument("<service>", "Service to deploy")
129
+ .argument("[env]", "Environment", ["dev", "staging", "prod"] as const)
130
+ .action((service, env, opts) => { /* service: string, env: union | undefined */ })
131
+ ```
132
+
133
+ Use the inline form for plain string args (terse, matches Commander.js docs and tutorials).
134
+ Use `.argument()` when you need:
135
+
136
+ - A description for `--help` output
137
+ - A parser, schema, or `choices` array (the inline string syntax can't express these)
138
+ - A default value
139
+
140
+ The two forms compose: inline args declared in the command string come first in the
141
+ positional tuple, and any `.argument()` calls append. The merged form `.actionMerged()`
142
+ sees all of them on a single named-object parameter regardless of which form declared them.
143
+
144
+ ## Replaces `@commander-js/extra-typings`
145
+
146
+ [`@commander-js/extra-typings`](https://github.com/commander-js/extra-typings) pioneered chain-inferred option types for Commander.js — each `.option()` call narrows the type of `.opts()`. We use the same idea but with a much simpler implementation (~100 lines vs. 1536 lines) because we own the `Command` class rather than wrapping someone else's.
147
+
148
+ `extra-typings` re-declares Commander's entire API from the outside — every method, every overload, every generic parameter — because it's a type-only wrapper around a class it doesn't control. We use interface merging on our own class and only type the methods that matter (`.option()`, `.opts()`, `.action()`). Modern TS features (const type parameters, template literal types) keep the flag-parsing utilities clean.
149
+
150
+ If you're using `@silvery/commander`, you don't need `@commander-js/extra-typings`.
151
+
78
152
  ## Credits
79
153
 
80
154
  - **[Commander.js](https://github.com/tj/commander.js)** by TJ Holowaychuk and contributors