Add input type checks
This commit is contained in:
parent
b706526e99
commit
5be44a87bf
2 changed files with 48 additions and 0 deletions
18
bashutils.sh
18
bashutils.sh
|
@ -164,6 +164,23 @@ function is_command_file() { _bashutils_reset_environment; [[ "$(type -t "${1}")
|
|||
## => Arguments: <str:command name>
|
||||
## => Returns: 0 if it is, 1 otherwise
|
||||
function is_command_defined() { _bashutils_reset_environment; [[ "$(type -t "${1}")" == "" ]]; _bashutils_return_environment ${?}; }
|
||||
## Input type
|
||||
## => Checks whether the provided input is a bool.
|
||||
## => Arguments: <str:input>
|
||||
## => Returns: 0 if it is, 1 otherwise
|
||||
function is_input_bool() { _bashutils_reset_environment; [ "${1}" == "true" ] || [ "${1}" == "false" ]; _bashutils_return_environment "${?}"; }
|
||||
## => Checks whether the provided input is a byte.
|
||||
## => Arguments: <str:input>
|
||||
## => Returns: 0 if it is, 1 otherwise
|
||||
function is_input_byte() { _bashutils_reset_environment; [[ "${1}" =~ ^[0-9]+$ ]] && [[ "${1}" -gt -1 ]] && [["${1}" -lt 256 ]]; _bashutils_return_environment "${?}"; }
|
||||
## => Checks whether the provided input is a integer/number.
|
||||
## => Arguments: <str:input>
|
||||
## => Returns: 0 if it is, 1 otherwise
|
||||
function is_input_int() { _bashutils_reset_environment; [[ "${1}" =~ ^[0-9]+$ ]]; _bashutils_return_environment "${?}"; }
|
||||
## => Checks whether the provided input is a char.
|
||||
## => Arguments: <str:input>
|
||||
## => Returns: 0 if it is, 1 otherwise
|
||||
function is_input_char() { _bashutils_reset_environment; [[ "${1}" =~ ^.$ ]]; _bashutils_return_environment "${?}"; }
|
||||
|
||||
# Variable definition
|
||||
## => Sets the specified variable to the specified value if it is undefined.
|
||||
|
@ -185,3 +202,4 @@ set_undefined "BASHUTILS_LOGLEVEL" "3"
|
|||
################################
|
||||
### BASHUTILS CODE ENDS HERE ###
|
||||
################################
|
||||
|
||||
|
|
|
@ -57,3 +57,33 @@ These checks check the type of the passed command and return `0` if it matches o
|
|||
<td><code>is_command_defined <str:command name></code></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## Input type
|
||||
These checks check the type of the specified input and return `0` if the type matches or `1` if it isn't.
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>Checks for</td>
|
||||
<td>Usage</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>bool</code></td>
|
||||
<td><code>is_input_bool <?:input></code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>byte</code></td>
|
||||
<td><code>is_input_byte <?:input></code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>int</code></td>
|
||||
<td><code>is_input_int <?:input></code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>char</code></td>
|
||||
<td><code>is_input_char <?:input></code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>str</code></td>
|
||||
<td>Are you serious?</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
Loading…
Reference in a new issue