flag.d.elv 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #elvdoc:fn call
  2. #
  3. # ```elvish
  4. # flag:call $fn $args
  5. # ```
  6. #
  7. # Parses flags from `$args` according to the signature of the
  8. # `$fn`, using the [Go convention](#go-convention), and calls `$fn`.
  9. #
  10. # The `$fn` must be a user-defined function (i.e. not a builtin
  11. # function or external command). Each option corresponds to a flag; see
  12. # [`flag:parse`](#flag:parse) for how the default value affects the behavior of
  13. # flags. After parsing, the non-flag arguments are used as function arguments.
  14. #
  15. # Example:
  16. #
  17. # ```elvish-transcript
  18. # ~> use flag
  19. # ~> fn f {|&verbose=$false &port=(num 8000) name| put $verbose $port $name }
  20. # ~> flag:call $f [-verbose -port 80 a.c]
  21. # ▶ $true
  22. # ▶ (num 80)
  23. # ▶ a.c
  24. # ```
  25. #
  26. # @cf flag:parse
  27. #elvdoc:fn parse
  28. #
  29. # ```elvish
  30. # flag:parse $args $specs
  31. # ```
  32. #
  33. # Parses flags from `$args` according to the `$specs`, using the [Go
  34. # convention](#go-convention).
  35. #
  36. # The `$args` must be a list of strings containing the command-line arguments
  37. # to parse.
  38. #
  39. # The `$specs` must be a list of flag specs:
  40. #
  41. # ```elvish
  42. # [
  43. # [flag default-value 'description of the flag']
  44. # ...
  45. # ]
  46. # ```
  47. #
  48. # Each flag spec consists of the name of the flag (without the leading `-`),
  49. # its default value, and a description. The default value influences the how
  50. # the flag gets converted from string:
  51. #
  52. # - If it is boolean, the flag is a boolean flag (see [Go
  53. # convention](#go-convention) for implications). Flag values `0`, `f`, `F`,
  54. # `false`, `False` and `FALSE` are converted to `$false`, and `1`, `t`,
  55. # `T`, `true`, `True` and `TRUE` to `$true`. Other values are invalid.
  56. #
  57. # - If it is a string, no conversion is done.
  58. #
  59. # - If it is a [typed number](language.html#number), the flag value is
  60. # converted using [`num`](builtin.html#num).
  61. #
  62. # - If it is a list, the flag value is split at `,` (equivalent to `{|s| put
  63. # [(str:split , $s)] }`).
  64. #
  65. # - If it is none of the above, an exception is thrown.
  66. #
  67. # On success, this command outputs two values: a map containing the value of
  68. # flags defined in `$specs` (whether they appear in `$args` or not), and a list
  69. # containing non-flag arguments.
  70. #
  71. # Example:
  72. #
  73. # ```elvish-transcript
  74. # ~> flag:parse [-v -times 10 foo] [
  75. # [v $false 'Verbose']
  76. # [times (num 1) 'How many times']
  77. # ]
  78. # ▶ [&v=$true &times=(num 10)]
  79. # ▶ [foo]
  80. # ~> flag:parse [] [
  81. # [v $false 'Verbose']
  82. # [times (num 1) 'How many times']
  83. # ]
  84. # ▶ [&v=$false &times=(num 1)]
  85. # ▶ []
  86. # ```
  87. #
  88. # @cf flag:call flag:parse-getopt
  89. #elvdoc:fn parse-getopt
  90. #
  91. # ```elvish
  92. # flag:parse-getopt $args $specs ^
  93. # &stop-after-double-dash=$true &stop-before-non-flag=$false &long-only=$false
  94. # ```
  95. #
  96. # Parses flags from `$args` according to the `$specs`, using the [getopt
  97. # convention](#getopt-convention) (see there for the semantics of the options),
  98. # and outputs the result.
  99. #
  100. # The `$args` must be a list of strings containing the command-line arguments
  101. # to parse.
  102. #
  103. # The `$specs` must be a list of flag specs:
  104. #
  105. # ```elvish
  106. # [
  107. # [&short=f &long=flag &arg-optional=$false &arg-required=$false]
  108. # ...
  109. # ]
  110. # ```
  111. #
  112. # Each flag spec can contain the following:
  113. #
  114. # - The short and long form of the flag, without the leading `-` or `--`. The
  115. # short form, if non-empty, must be one character. At least one of `&short`
  116. # and `&long` must be non-empty.
  117. #
  118. # - Whether the flag takes an optional argument or a required argument. At
  119. # most one of `&arg-optional` and `&arg-required` may be true.
  120. #
  121. # It is not an error for a flag spec to contain more keys.
  122. #
  123. # On success, this command outputs two values: a list describing all flags
  124. # parsed from `$args`, and a list containing non-flag arguments. The former
  125. # list looks like:
  126. #
  127. # ```elvish
  128. # [
  129. # [&spec=... &arg=value &long=$false]
  130. # ...
  131. # ]
  132. # ```
  133. #
  134. # Each entry contains the original spec for the flag, its argument, and whether
  135. # the flag appeared in its long form.
  136. #
  137. # Example (some output reformatted for readability):
  138. #
  139. # ```elvish-transcript
  140. # ~> var specs = [
  141. # [&short=v &long=verbose]
  142. # [&short=p &long=port &arg-required]
  143. # ]
  144. # ~> flag:parse-getopt [-v -p 80 foo] $specs
  145. # ▶ [[&spec=[&short=v &long=verbose] &long=$false &arg='']
  146. # [&spec=[&arg-required=$true &short=p &long=port] &long=$false &arg=80]]
  147. # ▶ [foo]
  148. # ~> flag:parse-getopt [--verbose] $specs
  149. # ▶ [[&spec=[&short=v &long=verbose] &long=$true &arg='']]
  150. # ▶ []
  151. # ~> flag:parse-getopt [-v] [[&short=v &extra-info=foo]] # extra key in spec
  152. # ▶ [[&spec=[&extra-info=foo &short=v] &long=$false &arg='']]
  153. # ▶ []
  154. # ```
  155. #
  156. # @cf flag:parse edit:complete-getopt