@@ -21,15 +21,30 @@ import type { HasType, Type } from "./type";
2121import { string } from "./types" ;
2222import type { AllOrNothing } from "./utils" ;
2323
24- type OptionConfig < Decoder extends Type < string , any > > = LongDoc &
24+ type OptionConfig <
25+ Decoder extends Type < string , any > ,
26+ ImplicitValue = never ,
27+ > = LongDoc &
2528 HasType < Decoder > &
26- Partial < Descriptive & EnvDoc & ShortDoc & OnMissing < OutputOf < Decoder > > > & {
29+ Partial <
30+ Descriptive &
31+ EnvDoc &
32+ ShortDoc &
33+ OnMissing < OutputOf < Decoder > | ImplicitValue >
34+ > & {
2735 completion ?: CompletionHandler ;
28- } & AllOrNothing < Default < OutputOf < Decoder > > > ;
36+ implicitValue ?: ImplicitValue ;
37+ } & AllOrNothing < Default < OutputOf < Decoder > | ImplicitValue > > ;
2938
30- function fullOption < Decoder extends Type < string , any > > (
31- config : OptionConfig < Decoder > ,
32- ) : ArgParser < OutputOf < Decoder > > & ProvidesHelp & Partial < Descriptive > {
39+ function fullOption < Decoder extends Type < string , any > , ImplicitValue = never > (
40+ config : OptionConfig < Decoder , ImplicitValue > ,
41+ ) : ArgParser < OutputOf < Decoder > | ImplicitValue > &
42+ ProvidesHelp &
43+ Partial < Descriptive > {
44+ const hasImplicitValue = Object . prototype . hasOwnProperty . call (
45+ config ,
46+ "implicitValue" ,
47+ ) ;
3348 const parser : ReturnType < typeof fullOption > = {
3449 description : config . description ?? config . type . description ,
3550 helpTopics ( ) {
@@ -38,7 +53,7 @@ function fullOption<Decoder extends Type<string, any>>(
3853 if ( config . short ) {
3954 usage += `, -${ config . short } ` ;
4055 }
41- usage += ` <${ displayName } >` ;
56+ usage += hasImplicitValue ? ` [< ${ displayName } >]` : ` <${ displayName } >` ;
4257
4358 const defaults : string [ ] = [ ] ;
4459
@@ -87,15 +102,24 @@ function fullOption<Decoder extends Type<string, any>>(
87102 ] ;
88103 } ,
89104 register ( opts ) {
90- opts . forceOptionLongNames . add ( config . long ) ;
91- if ( config . short ) {
92- opts . forceOptionShortNames . add ( config . short ) ;
105+ if ( hasImplicitValue ) {
106+ opts . forceOptionalValueLongNames . add ( config . long ) ;
107+ if ( config . short ) {
108+ opts . forceOptionalValueShortNames . add ( config . short ) ;
109+ }
110+ } else {
111+ opts . forceOptionLongNames . add ( config . long ) ;
112+ if ( config . short ) {
113+ opts . forceOptionShortNames . add ( config . short ) ;
114+ }
93115 }
94116 } ,
95117 async parse ( {
96118 nodes,
97119 visitedNodes,
98- } : ParseContext ) : Promise < ParsingResult < OutputOf < Decoder > > > {
120+ } : ParseContext ) : Promise <
121+ ParsingResult < OutputOf < Decoder > | ImplicitValue >
122+ > {
99123 const options = findOption ( nodes , {
100124 longNames : [ config . long ] ,
101125 shortNames : config . short ? [ config . short ] : [ ] ,
@@ -120,6 +144,8 @@ function fullOption<Decoder extends Type<string, any>>(
120144 let envPrefix = "" ;
121145 if ( option ?. value ) {
122146 rawValue = option . value . node . raw ;
147+ } else if ( option && hasImplicitValue ) {
148+ return Result . ok ( config . implicitValue as ImplicitValue ) ;
123149 } else if ( valueFromEnv !== undefined ) {
124150 rawValue = valueFromEnv ;
125151 envPrefix = `env[${ styleText ( "italic" , String ( config . env ) ) } ]: ` ;
@@ -191,6 +217,7 @@ function fullOption<Decoder extends Type<string, any>>(
191217}
192218
193219type StringType = Type < string , string > ;
220+ type NoInferValue < T > = [ T ] [ T extends any ? 0 : never ] ;
194221
195222/**
196223 * Decodes an argument which is in the form of a key and a value, and allows parsing the following ways:
@@ -199,13 +226,50 @@ type StringType = Type<string, string>;
199226 * - `--long value` where `long` is the provided `long`
200227 * - `-s=value` where `s` is the provided `short`
201228 * - `-s value` where `s` is the provided `short`
202- * @param config flag configurations
229+ *
230+ * Set `implicitValue` to also accept the option without a value. A bare
231+ * occurrence uses that value, while an absent option continues through the
232+ * normal environment, default, and `onMissing` fallbacks.
233+ * @param config option configuration
203234 */
235+ export function option < Decoder extends Type < string , any > , ImplicitValue > (
236+ config : LongDoc &
237+ HasType < Decoder > &
238+ Partial <
239+ Descriptive &
240+ EnvDoc &
241+ ShortDoc &
242+ OnMissing < OutputOf < Decoder > | NoInferValue < ImplicitValue > >
243+ > & {
244+ completion ?: CompletionHandler ;
245+ implicitValue : ImplicitValue ;
246+ } & AllOrNothing < Default < OutputOf < Decoder > | NoInferValue < ImplicitValue > > > ,
247+ ) : ArgParser < OutputOf < Decoder > | ImplicitValue > &
248+ ProvidesHelp &
249+ Partial < Descriptive > ;
250+ export function option < ImplicitValue > (
251+ config : LongDoc &
252+ Partial <
253+ HasType < never > &
254+ Descriptive &
255+ EnvDoc &
256+ ShortDoc &
257+ OnMissing < OutputOf < StringType > | NoInferValue < ImplicitValue > >
258+ > & {
259+ completion ?: CompletionHandler ;
260+ implicitValue : ImplicitValue ;
261+ } & AllOrNothing <
262+ Default < OutputOf < StringType > | NoInferValue < ImplicitValue > >
263+ > ,
264+ ) : ArgParser < OutputOf < StringType > | ImplicitValue > &
265+ ProvidesHelp &
266+ Partial < Descriptive > ;
204267export function option < Decoder extends Type < string , any > > (
205268 config : LongDoc &
206269 HasType < Decoder > &
207270 Partial < Descriptive & EnvDoc & ShortDoc & OnMissing < OutputOf < Decoder > > > & {
208271 completion ?: CompletionHandler ;
272+ implicitValue ?: never ;
209273 } & AllOrNothing < Default < OutputOf < Decoder > > > ,
210274) : ArgParser < OutputOf < Decoder > > & ProvidesHelp & Partial < Descriptive > ;
211275export function option (
@@ -218,13 +282,14 @@ export function option(
218282 OnMissing < OutputOf < StringType > >
219283 > & { completion ?: CompletionHandler } & AllOrNothing <
220284 Default < OutputOf < StringType > >
221- > ,
285+ > & { implicitValue ?: never } ,
222286) : ArgParser < OutputOf < StringType > > & ProvidesHelp & Partial < Descriptive > ;
223287export function option (
224288 config : LongDoc &
225289 Partial < HasType < any > > &
226290 Partial < Descriptive & EnvDoc & ShortDoc > & {
227291 completion ?: CompletionHandler ;
292+ implicitValue ?: unknown ;
228293 } ,
229294) : ArgParser < OutputOf < any > > & ProvidesHelp & Partial < Descriptive > {
230295 return fullOption ( {
0 commit comments