src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/edit/complete_getopt_test.elvts (about)

     1  //each:complete-getopt-in-global
     2  
     3  ///////////////////
     4  # complete-getopt #
     5  ///////////////////
     6  
     7  ~> fn complete {|@args|
     8       var opt-specs = [ [&short=a &long=all &desc="Show all"]
     9                         [&short=n &long=name &desc="Set name"
    10                          &arg-required=$true &arg-desc='new-name'
    11                          &completer= {|_| put name1 name2 }] ]
    12       var arg-handlers = [ {|_| put first1 first2 }
    13                            {|_| put second1 second2 } ... ]
    14       complete-getopt $args $opt-specs $arg-handlers
    15     }
    16  // complete argument
    17  ~> complete ''
    18  ▶ first1
    19  ▶ first2
    20  ~> complete '' >&-
    21  Exception: port does not support value output
    22    [tty]:6:29-46:   var arg-handlers = [ {|_| put first1 first2 }
    23    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    24    [tty]:1:1-15: complete '' >&-
    25  // complete option
    26  ~> complete -
    27  ▶ (edit:complex-candidate -a &code-suffix='' &display=[^styled '-a (Show all)'])
    28  ▶ (edit:complex-candidate --all &code-suffix='' &display=[^styled '--all (Show all)'])
    29  ▶ (edit:complex-candidate -n &code-suffix='' &display=[^styled '-n new-name (Set name)'])
    30  ▶ (edit:complex-candidate --name &code-suffix='' &display=[^styled '--name new-name (Set name)'])
    31  ~> complete - >&-
    32  Exception: port does not support value output
    33    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    34    [tty]:1:1-14: complete - >&-
    35  // complete long option
    36  ~> complete --
    37  ▶ (edit:complex-candidate --all &code-suffix='' &display=[^styled '--all (Show all)'])
    38  ▶ (edit:complex-candidate --name &code-suffix='' &display=[^styled '--name new-name (Set name)'])
    39  ~> complete --a
    40  ▶ (edit:complex-candidate --all &code-suffix='' &display=[^styled '--all (Show all)'])
    41  ~> complete -- >&-
    42  Exception: port does not support value output
    43    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    44    [tty]:1:1-15: complete -- >&-
    45  // complete argument of short option
    46  ~> complete -n ''
    47  ▶ name1
    48  ▶ name2
    49  ~> complete -n '' >&-
    50  Exception: port does not support value output
    51    [tty]:5:39-54:                      &completer= {|_| put name1 name2 }] ]
    52    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    53    [tty]:1:1-18: complete -n '' >&-
    54  // complete argument of long option
    55  ~> complete --name ''
    56  ▶ name1
    57  ▶ name2
    58  ~> complete --name '' >&-
    59  Exception: port does not support value output
    60    [tty]:5:39-54:                      &completer= {|_| put name1 name2 }] ]
    61    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    62    [tty]:1:1-22: complete --name '' >&-
    63  // complete (normal) argument after option that doesn't take an argument
    64  ~> complete -a ''
    65  ▶ first1
    66  ▶ first2
    67  ~> complete -a '' >&-
    68  Exception: port does not support value output
    69    [tty]:6:29-46:   var arg-handlers = [ {|_| put first1 first2 }
    70    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    71    [tty]:1:1-18: complete -a '' >&-
    72  // complete second argument
    73  ~> complete arg1 ''
    74  ▶ second1
    75  ▶ second2
    76  ~> complete arg1 '' >&-
    77  Exception: port does not support value output
    78    [tty]:7:29-48:                        {|_| put second1 second2 } ... ]
    79    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    80    [tty]:1:1-20: complete arg1 '' >&-
    81  // complete variadic argument
    82  ~> complete arg1 arg2 ''
    83  ▶ second1
    84  ▶ second2
    85  ~> complete arg1 arg2 '' >&-
    86  Exception: port does not support value output
    87    [tty]:7:29-48:                        {|_| put second1 second2 } ... ]
    88    [tty]:8:3-48:   complete-getopt $args $opt-specs $arg-handlers
    89    [tty]:1:1-25: complete arg1 arg2 '' >&-
    90  
    91  # typechecks #
    92  
    93  ~> complete-getopt [foo []] [] []
    94  Exception: arg should be string, got list
    95    [tty]:1:1-30: complete-getopt [foo []] [] []
    96  ~> complete-getopt [] [foo] []
    97  Exception: opt should be map, got string
    98    [tty]:1:1-27: complete-getopt [] [foo] []
    99  ~> complete-getopt [] [[&short=[]]] []
   100  Exception: short should be string, got list
   101    [tty]:1:1-35: complete-getopt [] [[&short=[]]] []
   102  ~> complete-getopt [] [[&short=foo]] []
   103  Exception: short should be exactly one rune, got foo
   104    [tty]:1:1-36: complete-getopt [] [[&short=foo]] []
   105  ~> complete-getopt [] [[&long=[]]] []
   106  Exception: long should be string, got list
   107    [tty]:1:1-34: complete-getopt [] [[&long=[]]] []
   108  ~> complete-getopt [] [[&]] []
   109  Exception: opt should have at least one of short and long forms
   110    [tty]:1:1-27: complete-getopt [] [[&]] []
   111  ~> complete-getopt [] [[&short=a &arg-required=foo]] []
   112  Exception: arg-required should be bool, got string
   113    [tty]:1:1-52: complete-getopt [] [[&short=a &arg-required=foo]] []
   114  ~> complete-getopt [] [[&short=a &arg-optional=foo]] []
   115  Exception: arg-optional should be bool, got string
   116    [tty]:1:1-52: complete-getopt [] [[&short=a &arg-optional=foo]] []
   117  ~> complete-getopt [] [[&short=a &arg-required=$true &arg-optional=$true]] []
   118  Exception: opt cannot have both arg-required and arg-optional
   119    [tty]:1:1-74: complete-getopt [] [[&short=a &arg-required=$true &arg-optional=$true]] []
   120  ~> complete-getopt [] [[&short=a &desc=[]]] []
   121  Exception: desc should be string, got list
   122    [tty]:1:1-43: complete-getopt [] [[&short=a &desc=[]]] []
   123  ~> complete-getopt [] [[&short=a &arg-desc=[]]] []
   124  Exception: arg-desc should be string, got list
   125    [tty]:1:1-47: complete-getopt [] [[&short=a &arg-desc=[]]] []
   126  ~> complete-getopt [] [[&short=a &completer=[]]] []
   127  Exception: completer should be fn, got list
   128    [tty]:1:1-48: complete-getopt [] [[&short=a &completer=[]]] []
   129  ~> complete-getopt [] [] [foo]
   130  Exception: string except for ... not allowed as argument handler, got foo
   131    [tty]:1:1-27: complete-getopt [] [] [foo]
   132  ~> complete-getopt [] [] [[]]
   133  Exception: argument handler should be fn, got list
   134    [tty]:1:1-26: complete-getopt [] [] [[]]