1 2use builtin; 3use str; 4 5set edit:completion:arg-completer[my-app] = {|@words| 6 fn spaces {|n| 7 builtin:repeat $n ' ' | str:join '' 8 } 9 fn cand {|text desc| 10 edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc 11 } 12 var command = 'my-app' 13 for word $words[1..-1] { 14 if (str:has-prefix $word '-') { 15 break 16 } 17 set command = $command';'$word 18 } 19 var completions = [ 20 &'my-app'= { 21 cand -c 'some config file' 22 cand -C 'some config file' 23 cand --config 'some config file' 24 cand --conf 'some config file' 25 cand -h 'Print help' 26 cand --help 'Print help' 27 cand -V 'Print version' 28 cand --version 'Print version' 29 cand test 'tests things' 30 cand some_cmd 'top level subcommand' 31 cand help 'Print this message or the help of the given subcommand(s)' 32 } 33 &'my-app;test'= { 34 cand --case 'the case to test' 35 cand -h 'Print help' 36 cand --help 'Print help' 37 cand -V 'Print version' 38 cand --version 'Print version' 39 } 40 &'my-app;some_cmd'= { 41 cand -h 'Print help' 42 cand --help 'Print help' 43 cand -V 'Print version' 44 cand --version 'Print version' 45 cand sub_cmd 'sub-subcommand' 46 cand help 'Print this message or the help of the given subcommand(s)' 47 } 48 &'my-app;some_cmd;sub_cmd'= { 49 cand --config 'the other case to test' 50 cand -h 'Print help (see more with ''--help'')' 51 cand --help 'Print help (see more with ''--help'')' 52 cand -V 'Print version' 53 cand --version 'Print version' 54 } 55 &'my-app;some_cmd;help'= { 56 cand sub_cmd 'sub-subcommand' 57 cand help 'Print this message or the help of the given subcommand(s)' 58 } 59 &'my-app;some_cmd;help;sub_cmd'= { 60 } 61 &'my-app;some_cmd;help;help'= { 62 } 63 &'my-app;help'= { 64 cand test 'tests things' 65 cand some_cmd 'top level subcommand' 66 cand help 'Print this message or the help of the given subcommand(s)' 67 } 68 &'my-app;help;test'= { 69 } 70 &'my-app;help;some_cmd'= { 71 cand sub_cmd 'sub-subcommand' 72 } 73 &'my-app;help;some_cmd;sub_cmd'= { 74 } 75 &'my-app;help;help'= { 76 } 77 ] 78 $completions[$command] 79} 80