|
| 1 | +# Validations Example |
| 2 | + |
| 3 | +Demonstrates how to add validation functions to arguments and flag arguments. |
| 4 | + |
| 5 | +This example was generated with: |
| 6 | + |
| 7 | +```bash |
| 8 | +$ bashly init |
| 9 | +# ... now edit src/bashly.yml to match the example ... |
| 10 | +$ bashly add validations |
| 11 | +$ bashly generate |
| 12 | +``` |
| 13 | + |
| 14 | +Running the `bashly add validations` command simply adds the |
| 15 | +[src/lib/validations](src/lib/validations) folder, which includes some built in |
| 16 | +validation functions. You can add custom function by adding a function that |
| 17 | +starts with `validate_`. Thee functions must return a string on validation |
| 18 | +failure, or an empty string on success. |
| 19 | + |
| 20 | +----- |
| 21 | + |
| 22 | +## `bashly.yml` |
| 23 | + |
| 24 | +```yaml |
| 25 | +name: calc |
| 26 | +help: Sample application demonstrating validations |
| 27 | +version: 0.1.0 |
| 28 | + |
| 29 | +commands: |
| 30 | +- name: add |
| 31 | + short: a |
| 32 | + help: Add two numbers |
| 33 | + |
| 34 | + args: |
| 35 | + - name: first |
| 36 | + help: First number |
| 37 | + required: true |
| 38 | + |
| 39 | + # Specify one or more validation types (as string or array) |
| 40 | + # This validation will look for a function named `validate_integer` in your |
| 41 | + # script. |
| 42 | + validate: integer |
| 43 | + - name: second |
| 44 | + help: Second number |
| 45 | + |
| 46 | + # Using the array syntax, you can specify more than one validations |
| 47 | + validate: |
| 48 | + - integer |
| 49 | + |
| 50 | + flags: |
| 51 | + - long: --multiply |
| 52 | + short: -m |
| 53 | + arg: factor |
| 54 | + help: Multiply the result |
| 55 | + |
| 56 | + # Validations also work on flags (when they have arguments) |
| 57 | + validate: integer |
| 58 | +``` |
| 59 | +
|
| 60 | +
|
| 61 | +
|
| 62 | +## Generated script output |
| 63 | +
|
| 64 | +### `$ ./calc -h` |
| 65 | + |
| 66 | +```shell |
| 67 | +calc - Sample application demonstrating validations |
| 68 | +
|
| 69 | +Usage: |
| 70 | + calc [command] |
| 71 | + calc [command] --help | -h |
| 72 | + calc --version | -v |
| 73 | +
|
| 74 | +Commands: |
| 75 | + add Add two numbers |
| 76 | +
|
| 77 | +Options: |
| 78 | + --help, -h |
| 79 | + Show this help |
| 80 | +
|
| 81 | + --version, -v |
| 82 | + Show version number |
| 83 | +
|
| 84 | +
|
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +### `$ ./calc add 1 2 --multiply 3` |
| 89 | + |
| 90 | +```shell |
| 91 | +# this file is located in 'src/add_command.sh' |
| 92 | +# code for 'calc add' goes here |
| 93 | +# you can edit it freely and regenerate (it will not be overwritten) |
| 94 | +args: |
| 95 | +- ${args[first]} = 1 |
| 96 | +- ${args[--multiply]} = 3 |
| 97 | +- ${args[second]} = 2 |
| 98 | +
|
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | +### `$ ./calc add A` |
| 103 | + |
| 104 | +```shell |
| 105 | +validation error: FIRST must be an integer |
| 106 | +
|
| 107 | +
|
| 108 | +``` |
| 109 | + |
| 110 | +### `$ ./calc add 1 B` |
| 111 | + |
| 112 | +```shell |
| 113 | +validation error: SECOND must be an integer |
| 114 | +
|
| 115 | +
|
| 116 | +``` |
| 117 | + |
| 118 | +### `$ ./calc add 1 2 --multiply C` |
| 119 | + |
| 120 | +```shell |
| 121 | +validation error: --multiply, -m FACTOR must be an integer |
| 122 | +
|
| 123 | +
|
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | + |
0 commit comments