|
| 1 | +pre-code-text: In addition to detailed help screens and error messages out of the box, your ArgumentParser CLI tools can provide completion scripts and man pages, as well as extensibility through a JSON rendering of the interface. |
| 2 | +headline: ArgumentParser in use |
| 3 | +tabs: |
| 4 | + - label: CLI usage |
| 5 | + code: |- |
| 6 | + $ repeat yes --count 3 |
| 7 | + yes |
| 8 | + yes |
| 9 | + yes |
| 10 | +
|
| 11 | + $ repeat --count |
| 12 | + Error: Missing value for '--count <count>' |
| 13 | + Help: --count <count> The number of times to repeat 'phrase'. |
| 14 | + Usage: repeat <phrase> [--count <count>] |
| 15 | + See 'repeat --help' for more information. |
| 16 | +
|
| 17 | + $ repeat -h |
| 18 | + USAGE: repeat <phrase> [--count <count>] |
| 19 | +
|
| 20 | + ARGUMENTS: |
| 21 | + <phrase> The phrase to repeat. |
| 22 | +
|
| 23 | + OPTIONS: |
| 24 | + --count <count> The number of times to repeat 'phrase'. |
| 25 | + -h, --help Show help information.' |
| 26 | + - label: Man page |
| 27 | + code: |- |
| 28 | + .\" "Generated by swift-argument-parser" |
| 29 | + .Dd May 21, 2025 |
| 30 | + .Dt REPEAT 1 |
| 31 | + .Os |
| 32 | + .Sh NAME |
| 33 | + .Nm repeat |
| 34 | + .Sh SYNOPSIS |
| 35 | + .Nm |
| 36 | + .Ar subcommand |
| 37 | + .Ar phrase |
| 38 | + .Op Fl -count Ar count |
| 39 | + .Op Fl -help |
| 40 | + .Sh DESCRIPTION |
| 41 | + .Bl -tag -width 6n |
| 42 | + .It Ar phrase |
| 43 | + The phrase to repeat. |
| 44 | + .It Fl -count Ar count |
| 45 | + The number of times to repeat 'phrase'. |
| 46 | + .It Fl h , -help |
| 47 | + Show help information. |
| 48 | + .It Em help |
| 49 | + Show subcommand help information. |
| 50 | + .Bl -tag -width 6n |
| 51 | + .It Ar subcommands... |
| 52 | + .El |
| 53 | + .El |
| 54 | + .Sh "EXIT STATUS" |
| 55 | + .Ex -std |
| 56 | + - label: Completion script |
| 57 | + code: |- |
| 58 | + #compdef repeat |
| 59 | +
|
| 60 | + __repeat_complete() { |
| 61 | + local -ar non_empty_completions=("${@:#(|:*)}") |
| 62 | + local -ar empty_completions=("${(M)@:#(|:*)}") |
| 63 | + _describe -V '' non_empty_completions -- empty_completions -P $'\'\'' |
| 64 | + } |
| 65 | +
|
| 66 | + __repeat_custom_complete() { |
| 67 | + local -a completions |
| 68 | + completions=("${(@f)"$("${command_name}" "${@}" "${command_line[@]}")"}") |
| 69 | + if [[ "${#completions[@]}" -gt 1 ]]; then |
| 70 | + __repeat_complete "${completions[@]:0:-1}" |
| 71 | + fi |
| 72 | + } |
| 73 | +
|
| 74 | + __repeat_cursor_index_in_current_word() { |
| 75 | + if [[ -z "${QIPREFIX}${IPREFIX}${PREFIX}" ]]; then |
| 76 | + printf 0 |
| 77 | + else |
| 78 | + printf %s "${#${(z)LBUFFER}[-1]}" |
| 79 | + fi |
| 80 | + } |
| 81 | +
|
| 82 | + _repeat() { |
| 83 | + emulate -RL zsh -G |
| 84 | + setopt extendedglob nullglob numericglobsort |
| 85 | + unsetopt aliases banghist |
| 86 | +
|
| 87 | + local -xr SAP_SHELL=zsh |
| 88 | + local -x SAP_SHELL_VERSION |
| 89 | + SAP_SHELL_VERSION="$(builtin emulate zsh -c 'printf %s "${ZSH_VERSION}"')" |
| 90 | + local -r SAP_SHELL_VERSION |
| 91 | +
|
| 92 | + local context state state_descr line |
| 93 | + local -A opt_args |
| 94 | +
|
| 95 | + local -r command_name="${words[1]}" |
| 96 | + local -ar command_line=("${words[@]}") |
| 97 | + local -ir current_word_index="$((CURRENT - 1))" |
| 98 | +
|
| 99 | + local -i ret=1 |
| 100 | + local -ar arg_specs=( |
| 101 | + ':phrase:' |
| 102 | + '--count[The number of times to repeat '\''phrase'\''.]:count:' |
| 103 | + '(-h --help)'{-h,--help}'[Show help information.]' |
| 104 | + ) |
| 105 | + _arguments -w -s -S : "${arg_specs[@]}" && ret=0 |
| 106 | +
|
| 107 | + return "${ret}" |
| 108 | + } |
| 109 | +
|
| 110 | + _repeat |
| 111 | + - label: JSON output |
| 112 | + code: |- |
| 113 | + { |
| 114 | + "command" : { |
| 115 | + "arguments" : [ |
| 116 | + { |
| 117 | + "abstract" : "The phrase to repeat.", |
| 118 | + "isOptional" : false, |
| 119 | + "isRepeating" : false, |
| 120 | + "kind" : "positional", |
| 121 | + "shouldDisplay" : true, |
| 122 | + "valueName" : "phrase" |
| 123 | + }, |
| 124 | + { |
| 125 | + "abstract" : "The number of times to repeat 'phrase'.", |
| 126 | + "isOptional" : true, |
| 127 | + "isRepeating" : false, |
| 128 | + "kind" : "option", |
| 129 | + "names" : [ |
| 130 | + { |
| 131 | + "kind" : "long", |
| 132 | + "name" : "count" |
| 133 | + } |
| 134 | + ], |
| 135 | + "preferredName" : { |
| 136 | + "kind" : "long", |
| 137 | + "name" : "count" |
| 138 | + }, |
| 139 | + "shouldDisplay" : true, |
| 140 | + "valueName" : "count" |
| 141 | + }, |
| 142 | + { |
| 143 | + "abstract" : "Show help information.", |
| 144 | + "isOptional" : true, |
| 145 | + "isRepeating" : false, |
| 146 | + "kind" : "flag", |
| 147 | + "names" : [ |
| 148 | + { |
| 149 | + "kind" : "short", |
| 150 | + "name" : "h" |
| 151 | + }, |
| 152 | + { |
| 153 | + "kind" : "long", |
| 154 | + "name" : "help" |
| 155 | + } |
| 156 | + ], |
| 157 | + "preferredName" : { |
| 158 | + "kind" : "long", |
| 159 | + "name" : "help" |
| 160 | + }, |
| 161 | + "shouldDisplay" : true, |
| 162 | + "valueName" : "help" |
| 163 | + } |
| 164 | + ], |
| 165 | + "commandName" : "repeat", |
| 166 | + "shouldDisplay" : true, |
| 167 | + "subcommands" : [ |
| 168 | + { |
| 169 | + "abstract" : "Show subcommand help information.", |
| 170 | + "arguments" : [ |
| 171 | + { |
| 172 | + "isOptional" : true, |
| 173 | + "isRepeating" : true, |
| 174 | + "kind" : "positional", |
| 175 | + "shouldDisplay" : true, |
| 176 | + "valueName" : "subcommands" |
| 177 | + }, |
| 178 | + { |
| 179 | + "isOptional" : true, |
| 180 | + "isRepeating" : false, |
| 181 | + "kind" : "flag", |
| 182 | + "names" : [ |
| 183 | + { |
| 184 | + "kind" : "short", |
| 185 | + "name" : "h" |
| 186 | + }, |
| 187 | + { |
| 188 | + "kind" : "long", |
| 189 | + "name" : "help" |
| 190 | + }, |
| 191 | + { |
| 192 | + "kind" : "longWithSingleDash", |
| 193 | + "name" : "help" |
| 194 | + } |
| 195 | + ], |
| 196 | + "preferredName" : { |
| 197 | + "kind" : "long", |
| 198 | + "name" : "help" |
| 199 | + }, |
| 200 | + "shouldDisplay" : false, |
| 201 | + "valueName" : "help" |
| 202 | + } |
| 203 | + ], |
| 204 | + "commandName" : "help", |
| 205 | + "shouldDisplay" : true, |
| 206 | + "superCommands" : [ |
| 207 | + "repeat" |
| 208 | + ] |
| 209 | + } |
| 210 | + ] |
| 211 | + }, |
| 212 | + "serializationVersion" : 0 |
| 213 | + } |
0 commit comments