1
1
#![ cfg( feature = "unstable-dynamic" ) ]
2
2
3
+ use std:: fs;
3
4
use std:: path:: Path ;
4
5
5
6
use clap:: { builder:: PossibleValue , Command } ;
@@ -9,7 +10,7 @@ macro_rules! complete {
9
10
( $cmd: expr, $input: expr$( , current_dir = $current_dir: expr) ? $( , ) ?) => {
10
11
{
11
12
#[ allow( unused) ]
12
- let current_dir = None ;
13
+ let current_dir: Option < & Path > = None ;
13
14
$( let current_dir = $current_dir; ) ?
14
15
complete( & mut $cmd, $input, current_dir)
15
16
}
@@ -288,6 +289,156 @@ goodbye-world
288
289
) ;
289
290
}
290
291
292
+ #[ test]
293
+ fn suggest_argument_value ( ) {
294
+ let mut cmd = Command :: new ( "dynamic" )
295
+ . arg (
296
+ clap:: Arg :: new ( "input" )
297
+ . long ( "input" )
298
+ . short ( 'i' )
299
+ . value_hint ( clap:: ValueHint :: FilePath ) ,
300
+ )
301
+ . arg (
302
+ clap:: Arg :: new ( "format" )
303
+ . long ( "format" )
304
+ . short ( 'F' )
305
+ . value_parser ( [ "json" , "yaml" , "toml" ] ) ,
306
+ )
307
+ . arg (
308
+ clap:: Arg :: new ( "count" )
309
+ . long ( "count" )
310
+ . short ( 'c' )
311
+ . action ( clap:: ArgAction :: Count ) ,
312
+ )
313
+ . arg ( clap:: Arg :: new ( "positional" ) . value_parser ( [ "pos_a" , "pos_b" , "pos_c" ] ) )
314
+ . args_conflicts_with_subcommands ( true ) ;
315
+
316
+ let testdir = snapbox:: dir:: DirRoot :: mutable_temp ( ) . unwrap ( ) ;
317
+ let testdir_path = testdir. path ( ) . unwrap ( ) ;
318
+
319
+ fs:: write ( testdir_path. join ( "a_file" ) , "" ) . unwrap ( ) ;
320
+ fs:: write ( testdir_path. join ( "b_file" ) , "" ) . unwrap ( ) ;
321
+ fs:: create_dir_all ( testdir_path. join ( "c_dir" ) ) . unwrap ( ) ;
322
+ fs:: create_dir_all ( testdir_path. join ( "d_dir" ) ) . unwrap ( ) ;
323
+
324
+ assert_data_eq ! (
325
+ complete!( cmd, "--input [TAB]" , current_dir = Some ( testdir_path) ) ,
326
+ snapbox:: str ![
327
+ "--input
328
+ --format
329
+ --count
330
+ --help Print help
331
+ -i
332
+ -F
333
+ -c
334
+ -h Print help
335
+ pos_a
336
+ pos_b
337
+ pos_c"
338
+ ] ,
339
+ ) ;
340
+
341
+ assert_data_eq ! (
342
+ complete!( cmd, "-i [TAB]" , current_dir = Some ( testdir_path) ) ,
343
+ snapbox:: str ![
344
+ "--input
345
+ --format
346
+ --count
347
+ --help Print help
348
+ -i
349
+ -F
350
+ -c
351
+ -h Print help
352
+ pos_a
353
+ pos_b
354
+ pos_c"
355
+ ] ,
356
+ ) ;
357
+
358
+ assert_data_eq ! (
359
+ complete!( cmd, "--input a[TAB]" , current_dir = Some ( testdir_path) ) ,
360
+ snapbox:: str ![ "" ] ,
361
+ ) ;
362
+
363
+ assert_data_eq ! (
364
+ complete!( cmd, "-i b[TAB]" , current_dir = Some ( testdir_path) ) ,
365
+ snapbox:: str ![ "" ] ,
366
+ ) ;
367
+
368
+ assert_data_eq ! (
369
+ complete!( cmd, "--format [TAB]" ) ,
370
+ snapbox:: str ![
371
+ "--input
372
+ --format
373
+ --count
374
+ --help Print help
375
+ -i
376
+ -F
377
+ -c
378
+ -h Print help
379
+ pos_a
380
+ pos_b
381
+ pos_c"
382
+ ] ,
383
+ ) ;
384
+
385
+ assert_data_eq ! (
386
+ complete!( cmd, "-F [TAB]" ) ,
387
+ snapbox:: str ![
388
+ "--input
389
+ --format
390
+ --count
391
+ --help Print help
392
+ -i
393
+ -F
394
+ -c
395
+ -h Print help
396
+ pos_a
397
+ pos_b
398
+ pos_c"
399
+ ] ,
400
+ ) ;
401
+
402
+ assert_data_eq ! ( complete!( cmd, "--format j[TAB]" ) , snapbox:: str ![ "" ] , ) ;
403
+
404
+ assert_data_eq ! ( complete!( cmd, "-F j[TAB]" ) , snapbox:: str ![ "" ] , ) ;
405
+
406
+ assert_data_eq ! ( complete!( cmd, "--format t[TAB]" ) , snapbox:: str ![ "" ] , ) ;
407
+
408
+ assert_data_eq ! ( complete!( cmd, "-F t[TAB]" ) , snapbox:: str ![ "" ] , ) ;
409
+
410
+ assert_data_eq ! (
411
+ complete!( cmd, "-cccF [TAB]" ) ,
412
+ snapbox:: str ![
413
+ "--input
414
+ --format
415
+ --count
416
+ --help\t Print help
417
+ -i
418
+ -F
419
+ -c
420
+ -h\t Print help
421
+ pos_a
422
+ pos_b
423
+ pos_c"
424
+ ]
425
+ ) ;
426
+
427
+ assert_data_eq ! (
428
+ complete!( cmd, "--input a_file [TAB]" ) ,
429
+ snapbox:: str ![
430
+ "--input
431
+ --format
432
+ --count
433
+ --help\t Print help
434
+ -i
435
+ -F
436
+ -c
437
+ -h\t Print help"
438
+ ]
439
+ ) ;
440
+ }
441
+
291
442
fn complete ( cmd : & mut Command , args : impl AsRef < str > , current_dir : Option < & Path > ) -> String {
292
443
let input = args. as_ref ( ) ;
293
444
let mut args = vec ! [ std:: ffi:: OsString :: from( cmd. get_name( ) ) ] ;
0 commit comments