@@ -23,12 +23,12 @@ class Calculator extends Args {
2323 @argument ({ description: " first number" })
2424 @type (" number" )
2525 @required ()
26- a? : number ;
26+ a! : number ;
2727
2828 @argument ({ description: " second number" })
2929 @type (" number" )
3030 @required ()
31- b? : number ;
31+ b! : number ;
3232
3333 @description (" operation to perform" )
3434 operation = " add" ;
@@ -37,12 +37,8 @@ class Calculator extends Args {
3737// Parse command line arguments
3838const args = Calculator .parse ([" 10" , " 5" ]);
3939
40- // Handle potentially undefined values
41- if (args .a !== undefined && args .b !== undefined ) {
42- console .log (` ${args .a } ${args .operation } ${args .b } = ${args .a + args .b } ` );
43- } else {
44- console .error (" Both numbers are required" );
45- }
40+ // Use the required values directly
41+ console .log (` ${args .a } ${args .operation } ${args .b } = ${args .a + args .b } ` );
4642```
4743
4844Usage:
@@ -137,7 +133,7 @@ if (args.serve) {
137133``` typescript ignore
138134@type (" string" )
139135@required ()
140- apiKey ? : string ;
136+ apiKey ! : string ;
141137```
142138
143139** Optional properties** (with default values):
@@ -164,7 +160,7 @@ apiKey?: string;
164160// ✅ Fixed - add @type decorator
165161@type (" string" )
166162@required ()
167- apiKey ? : string ;
163+ apiKey ! : string ;
168164
169165// ✅ Or provide a default (no @type needed)
170166apiKey = " " ;
@@ -193,13 +189,13 @@ class DeployCommand extends Args {
193189 @argument ({ description: " Application name to deploy" })
194190 @type (" string" )
195191 @required ()
196- appName? : string ;
192+ appName! : string ;
197193
198194 @argument ({ description: " Target environment" })
199195 @type (" string" )
200196 @addValidator (oneOf ([" dev" , " staging" , " prod" ]))
201197 @required ()
202- environment? : string ;
198+ environment! : string ;
203199
204200 @argument ({ description: " Version to deploy (optional)" })
205201 @type (" string" )
@@ -218,33 +214,28 @@ class DeployCommand extends Args {
218214 @description (" API key for authentication" )
219215 @type (" string" )
220216 @required ()
221- apiKey? : string ;
217+ apiKey! : string ;
222218}
223219
224220// Parse arguments
225221const args = DeployCommand .parse ([" myapp" , " prod" , " --apiKey" , " secret123" ]);
226222
227- // Type-safe usage with proper undefined checking
228- if (args .appName && args .environment && args .apiKey ) {
229- console .log (` Deploying ${args .appName } to ${args .environment } ` );
230-
231- if (args .version ) {
232- console .log (` Version: ${args .version } ` );
233- } else {
234- console .log (" Version: latest" );
235- }
236-
237- if (args .verbose ) {
238- console .log (` Instances: ${args .instances } ` );
239- console .log (` Force mode: ${args .force } ` );
240- console .log (` API Key: ${args .apiKey .substring (0 , 4 )}... ` );
241- }
223+ // Type-safe usage - required fields are guaranteed
224+ console .log (` Deploying ${args .appName } to ${args .environment } ` );
242225
243- // Proceed with deployment...
226+ if (args .version ) {
227+ console .log (` Version: ${args .version } ` );
244228} else {
245- console .error (" Missing required arguments" );
246- Deno .exit (1 );
229+ console .log (" Version: latest" );
230+ }
231+
232+ if (args .verbose ) {
233+ console .log (` Instances: ${args .instances } ` );
234+ console .log (` Force mode: ${args .force } ` );
235+ console .log (` API Key: ${args .apiKey .substring (0 , 4 )}... ` );
247236}
237+
238+ // Proceed with deployment...
248239```
249240
250241Usage examples:
@@ -294,16 +285,12 @@ class Server extends Args {
294285 @description (" API key for authentication" )
295286 @type (" string" )
296287 @required ()
297- apiKey? : string ;
288+ apiKey! : string ;
298289}
299290
300291const config = Server .parse ([" --apiKey" , " secret123" ]);
301- if (config .apiKey ) {
302- console .log (` Starting server on ${config .host }:${config .port } ` );
303- console .log (` API Key: ${config .apiKey .substring (0 , 4 )}... ` );
304- } else {
305- console .error (" API key is required" );
306- }
292+ console .log (` Starting server on ${config .host }:${config .port } ` );
293+ console .log (` API Key: ${config .apiKey .substring (0 , 4 )}... ` );
307294```
308295
309296### Application with Subcommands
@@ -391,7 +378,7 @@ class FileProcessor extends Args {
391378 @argument ({ description: " Input file to process" })
392379 @type (" string" )
393380 @required ()
394- input? : string ;
381+ input! : string ;
395382
396383 @argument ({ description: " Output file" })
397384 @type (" string" )
@@ -415,13 +402,11 @@ const args = FileProcessor.parse([
415402 " transform" ,
416403]);
417404
418- // Handle potentially undefined values
419- if (args .input ) {
420- console .log (` Processing ${args .input } -> ${args .output || " stdout" } ` );
421- console .log (` Mode: ${args .mode } ` );
422- if (args .extras && args .extras .length > 0 ) {
423- console .log (` Extras: ${args .extras .join (" , " )} ` );
424- }
405+ // Required field is guaranteed to be present
406+ console .log (` Processing ${args .input } -> ${args .output || " stdout" } ` );
407+ console .log (` Mode: ${args .mode } ` );
408+ if (args .extras && args .extras .length > 0 ) {
409+ console .log (` Extras: ${args .extras .join (" , " )} ` );
425410}
426411```
427412
@@ -525,7 +510,7 @@ class User extends Args {
525510 @type (" string" )
526511 @required ()
527512 @email ()
528- email? : string ;
513+ email! : string ;
529514}
530515```
531516
0 commit comments