complete_getopt.d.elv 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #elvdoc:fn complete-getopt
  2. #
  3. # ```elvish
  4. # edit:complete-getopt $args $opt-specs $arg-handlers
  5. # ```
  6. # Produces completions according to a specification of accepted command-line
  7. # options (both short and long options are handled), positional handler
  8. # functions for each command position, and the current arguments in the command
  9. # line. The arguments are as follows:
  10. #
  11. # * `$args` is an array containing the current arguments in the command line
  12. # (without the command itself). These are the arguments as passed to the
  13. # [Argument Completer](#argument-completer) function.
  14. #
  15. # * `$opt-specs` is an array of maps, each one containing the definition of
  16. # one possible command-line option. Matching options will be provided as
  17. # completions when the last element of `$args` starts with a dash, but not
  18. # otherwise. Each map can contain the following keys (at least one of `short`
  19. # or `long` needs to be specified):
  20. #
  21. # - `short` contains the one-letter short option, if any, without the dash.
  22. #
  23. # - `long` contains the long option name, if any, without the initial two
  24. # dashes.
  25. #
  26. # - `arg-optional`, if set to `$true`, specifies that the option receives an
  27. # optional argument.
  28. #
  29. # - `arg-required`, if set to `$true`, specifies that the option receives a
  30. # mandatory argument. Only one of `arg-optional` or `arg-required` can be
  31. # set to `$true`.
  32. #
  33. # - `desc` can be set to a human-readable description of the option which
  34. # will be displayed in the completion menu.
  35. #
  36. # - `completer` can be set to a function to generate possible completions for
  37. # the option argument. The function receives as argument the element at
  38. # that position and return zero or more candidates.
  39. #
  40. # * `$arg-handlers` is an array of functions, each one returning the possible
  41. # completions for that position in the arguments. Each function receives
  42. # as argument the last element of `$args`, and should return zero or more
  43. # possible values for the completions at that point. The returned values can
  44. # be plain strings or the output of `edit:complex-candidate`. If the last
  45. # element of the list is the string `...`, then the last handler is reused
  46. # for all following arguments.
  47. #
  48. # Example:
  49. #
  50. # ```elvish-transcript
  51. # ~> fn complete {|@args|
  52. # opt-specs = [ [&short=a &long=all &desc="Show all"]
  53. # [&short=n &desc="Set name" &arg-required=$true
  54. # &completer= {|_| put name1 name2 }] ]
  55. # arg-handlers = [ {|_| put first1 first2 }
  56. # {|_| put second1 second2 } ... ]
  57. # edit:complete-getopt $args $opt-specs $arg-handlers
  58. # }
  59. # ~> complete ''
  60. # ▶ first1
  61. # ▶ first2
  62. # ~> complete '-'
  63. # ▶ (edit:complex-candidate -a &display='-a (Show all)')
  64. # ▶ (edit:complex-candidate --all &display='--all (Show all)')
  65. # ▶ (edit:complex-candidate -n &display='-n (Set name)')
  66. # ~> complete -n ''
  67. # ▶ name1
  68. # ▶ name2
  69. # ~> complete -a ''
  70. # ▶ first1
  71. # ▶ first2
  72. # ~> complete arg1 ''
  73. # ▶ second1
  74. # ▶ second2
  75. # ~> complete arg1 arg2 ''
  76. # ▶ second1
  77. # ▶ second2
  78. # ```
  79. #
  80. # @cf flag:parse-getopt