| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package App::Spec::Bash::Spec; | ||
| 4 | |||
| 5 | 1 | our $VERSION = '0.002'; # VERSION | |
| 6 | |||
| 7 | 1 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | our @EXPORT_OK = qw/ $SPEC /; | |
| 9 | |||
| 10 | 1 | our $SPEC; | |
| 11 | |||
| 12 | # START INLINE | ||
| 13 | 1 | $SPEC = { | |
| 14 | 'abstract' => 'Generate commandline parser, completion and man pages', | ||
| 15 | 'appspec' => { | ||
| 16 | 'version' => '0.001' | ||
| 17 | }, | ||
| 18 | 'class' => 'App::Spec::Bash', | ||
| 19 | 'description' => 'This script can generate a commandline parser for bash from a specification | ||
| 20 | file written in YAML. | ||
| 21 | |||
| 22 | Writing a parser for commandline arguments isn\'t rocket science, but it\'s | ||
| 23 | something you don\'t want to do manually for all of your scripts, especially | ||
| 24 | in bash. Some tools exist, but they often don\'t support subcommands or | ||
| 25 | validating of option values. | ||
| 26 | |||
| 27 | C<appspec>/C<appspec-bash> can generate pretty much everything for you: | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =item Commandline parser for subcommands, options and parameters | ||
| 32 | |||
| 33 | =item Usage output | ||
| 34 | |||
| 35 | =item Shell completion files | ||
| 36 | |||
| 37 | =item man pages | ||
| 38 | |||
| 39 | =back | ||
| 40 | |||
| 41 | =head2 EXAMPLES | ||
| 42 | |||
| 43 | Here is an example of a very simple script. | ||
| 44 | |||
| 45 | The following files are needed: | ||
| 46 | |||
| 47 | share/mytool.yaml | ||
| 48 | bin/mytool | ||
| 49 | lib/mytool | ||
| 50 | lib/appspec # generated | ||
| 51 | share/completion/zsh/_mytool # generated | ||
| 52 | share/completion/bash/mytool.bash # generated | ||
| 53 | pod/mytool.pod # generated | ||
| 54 | lib/help # generated | ||
| 55 | |||
| 56 | The spec: | ||
| 57 | |||
| 58 | # share/mytool.yaml | ||
| 59 | --- | ||
| 60 | name: mytool # commandname | ||
| 61 | appspec: { version: 0.001 } | ||
| 62 | plugins: [-Meta] # not supported in bash | ||
| 63 | title: My cool tool # Will be shown in help | ||
| 64 | class: MyTool # "Class name" (means function prefix here) | ||
| 65 | |||
| 66 | subcommands: | ||
| 67 | command1: | ||
| 68 | op: command1 # The function name (MyTool.command1) | ||
| 69 | summary: cmd one # Will be shown in help and completion | ||
| 70 | options: | ||
| 71 | - foo|f=s --Foo # --foo or -f; \'=s\' means string | ||
| 72 | - bar|b --Bar # --bar or -b; a flag | ||
| 73 | |||
| 74 | Your script C<mytool> would look like this: | ||
| 75 | |||
| 76 | # bin/mytool | ||
| 77 | #!/bin/bash | ||
| 78 | DIR="$( dirname $BASH_SOURCE )" | ||
| 79 | source "$DIR/../lib/appspec" | ||
| 80 | source "$DIR/../lib/mytool" | ||
| 81 | APPSPEC.run $@ | ||
| 82 | |||
| 83 | The actual app: | ||
| 84 | |||
| 85 | # lib/mytool | ||
| 86 | #!/bin/bash | ||
| 87 | MyTool.command1() { | ||
| 88 | echo "=== OPTION foo: $OPT_FOO" | ||
| 89 | echo "=== OPTION bar: $OPT_BAR" | ||
| 90 | } | ||
| 91 | |||
| 92 | =head3 Generating the parser | ||
| 93 | |||
| 94 | Then generate the parser like this: | ||
| 95 | |||
| 96 | $ appspec-bash generate parser share/mytool.yaml lib/appspec | ||
| 97 | |||
| 98 | |||
| 99 | C<APPSPEC.run> will parse the arguments and then call the function | ||
| 100 | C<MyTool.command1>. In this function you can use the options via | ||
| 101 | C<$OPT_FOO> and C<$OPT_BAR>. | ||
| 102 | |||
| 103 | $ ./bin/mytool command1 --foo x --bar | ||
| 104 | # or | ||
| 105 | $ mytool command1 -f x -b | ||
| 106 | === OPTION foo: x | ||
| 107 | === OPTION bar: true | ||
| 108 | |||
| 109 | =head3 Generate completion | ||
| 110 | |||
| 111 | $ appspec completion share/mytool.yaml --zsh >share/completion/zsh/_mytool | ||
| 112 | $ appspec completion share/mytool.yaml --bash >share/completion/bash/mytool.bash | ||
| 113 | |||
| 114 | =head3 Generate pod | ||
| 115 | |||
| 116 | $ appspec pod share/mytool.yaml > pod/mytool.pod | ||
| 117 | $ perldoc pod/mytool.pod | ||
| 118 | |||
| 119 | =head3 Generate help | ||
| 120 | |||
| 121 | $ appspec-bash generate help share/mydemo.yaml lib/help | ||
| 122 | |||
| 123 | =head2 Generate a new app skeleton | ||
| 124 | |||
| 125 | appspec-bash new --class MyTool --name mytool | ||
| 126 | |||
| 127 | This will create your app C<mytool> in the directory C<MyTool> and output | ||
| 128 | some usage information like this: | ||
| 129 | |||
| 130 | To generate the parser and help, do: | ||
| 131 | |||
| 132 | % cd MyTool | ||
| 133 | % appspec-bash generate parser share/mytool.yaml lib/appspec | ||
| 134 | % appspec-bash generate help share/mytool.yaml lib/help | ||
| 135 | |||
| 136 | Try it out: | ||
| 137 | |||
| 138 | % bin/mytool cmd1 -ab --opt-x x -yfoo --opt-y bar | ||
| 139 | % bin/mytool cmd2 -vvv | ||
| 140 | % bin/mytool cmd2 -vvv foo | ||
| 141 | |||
| 142 | =head2 OPTION AND PARAMETER PARSING | ||
| 143 | |||
| 144 | appspec-bash supports various types of options. | ||
| 145 | |||
| 146 | =head3 Flags | ||
| 147 | |||
| 148 | # YAML | ||
| 149 | options: | ||
| 150 | - verbose|v -- Verbose output | ||
| 151 | |||
| 152 | % mytool --verbose | ||
| 153 | % mytool -v | ||
| 154 | |||
| 155 | You can also stack several flags: | ||
| 156 | |||
| 157 | % mytool -abc | ||
| 158 | |||
| 159 | =head3 Options with values | ||
| 160 | |||
| 161 | # YAML | ||
| 162 | options: | ||
| 163 | - color|c=s --Specify color | ||
| 164 | |||
| 165 | % mytool --color red | ||
| 166 | % mytool -c red | ||
| 167 | % mytool -cred | ||
| 168 | # Not yet supported | ||
| 169 | # mytool --color=red | ||
| 170 | |||
| 171 | You can stack flags together with options. If you have the flags C<-a> and | ||
| 172 | C<-b> and the option C<-c>, then you can use this syntax: | ||
| 173 | |||
| 174 | % mytool -abc23 | ||
| 175 | |||
| 176 | =head3 Incremental flags | ||
| 177 | |||
| 178 | # YAML | ||
| 179 | options: | ||
| 180 | - verbose|v+ --Verbose output (incremental) | ||
| 181 | |||
| 182 | % mytool -vvv # like: declare -i OPT_VERBOSE=3 | ||
| 183 | % mytool --verbose --verbose # like: declare -i OPT_VERBOSE=2 | ||
| 184 | |||
| 185 | =head3 Options with multiple values | ||
| 186 | |||
| 187 | # YAML | ||
| 188 | options: | ||
| 189 | - server|s=s@ --List of servers | ||
| 190 | |||
| 191 | % mytool --server foo --server bar | ||
| 192 | % mytool -s foo -s bar | ||
| 193 | |||
| 194 | =head3 Parameters | ||
| 195 | |||
| 196 | # YAML | ||
| 197 | parameters: | ||
| 198 | - name: server | ||
| 199 | required: true | ||
| 200 | summary: Server name | ||
| 201 | |||
| 202 | =head3 Option types | ||
| 203 | |||
| 204 | By default, an option is a flag. To accept a value, add a C<=>. Then it will | ||
| 205 | be a string by default: | ||
| 206 | |||
| 207 | - foo= | ||
| 208 | # or | ||
| 209 | - foo=s | ||
| 210 | |||
| 211 | You can also create integer options: | ||
| 212 | |||
| 213 | - max=i --Specify maximum value | ||
| 214 | |||
| 215 | Then you can treat it as an integer and use C<OPT_MAX+=1> for example. | ||
| 216 | |||
| 217 | There are also the types C<file>, C<dir>, C<filename> and C<dirname>. | ||
| 218 | Currently they are only relevant for completion. | ||
| 219 | |||
| 220 | # YAML | ||
| 221 | - input= +file --Input filename | ||
| 222 | - source-dir= +dir --Source directory | ||
| 223 | - output= +filename --Output file | ||
| 224 | - out-dir= +dirname --Output directory | ||
| 225 | |||
| 226 | In the future, C<file> and C<dir> will check for an existing file or | ||
| 227 | directory. And C<filename> and C<dirname> can be used if the file | ||
| 228 | or directory doesn\'t exist yet, but the tab completion will still offer | ||
| 229 | file or directory names. | ||
| 230 | |||
| 231 | See the C<mydemo> app in the example directory for examples of options and | ||
| 232 | parameters. | ||
| 233 | ', | ||
| 234 | 'markup' => 'pod', | ||
| 235 | 'name' => 'appspec-bash', | ||
| 236 | 'options' => [], | ||
| 237 | 'subcommands' => { | ||
| 238 | 'generate' => { | ||
| 239 | 'subcommands' => { | ||
| 240 | 'help' => { | ||
| 241 | 'op' => 'genhelp', | ||
| 242 | 'parameters' => [ | ||
| 243 | '+spec= +file --YAML Specification file', | ||
| 244 | '+output= +filename --Output file, e.g. lib/help' | ||
| 245 | ], | ||
| 246 | 'summary' => 'Generate help functions' | ||
| 247 | }, | ||
| 248 | 'parser' => { | ||
| 249 | 'op' => 'parser', | ||
| 250 | 'parameters' => [ | ||
| 251 | '+spec= +file --YAML Specification file', | ||
| 252 | '+output= +filename --Output file, e.g. lib/appspec' | ||
| 253 | ], | ||
| 254 | 'summary' => 'Generate main commandline parser script' | ||
| 255 | } | ||
| 256 | }, | ||
| 257 | 'summary' => 'Generate parser, help' | ||
| 258 | }, | ||
| 259 | 'new' => { | ||
| 260 | 'description' => 'This command creates a skeleton for a new app. | ||
| 261 | It will create a directory for your app and write a skeleton | ||
| 262 | spec file. | ||
| 263 | |||
| 264 | Example: | ||
| 265 | |||
| 266 | appspec-bash new --name mytool --class MyTool MyTool | ||
| 267 | ', | ||
| 268 | 'op' => 'cmd_new', | ||
| 269 | 'options' => [ | ||
| 270 | '+name|n=s --The (file) name of the app', | ||
| 271 | '+class|c=s --The main "class" (function prefix) for your app implementation', | ||
| 272 | 'overwrite|o --Overwrite existing dist directory' | ||
| 273 | ], | ||
| 274 | 'parameters' => [ | ||
| 275 | 'path= +dirname --Path to the distribution directory (default is the classname in current directory)' | ||
| 276 | ], | ||
| 277 | 'summary' => 'Generate new app' | ||
| 278 | } | ||
| 279 | }, | ||
| 280 | 'title' => 'Command line framework generator for bash' | ||
| 281 | }; | ||
| 282 | # END INLINE | ||
| 283 | |||
| 284 | 1 | 1; | |