Created
March 21, 2022 22:34
-
-
Save apolopena/b6363ef904dd4304e42a89ff9338f0ac to your computer and use it in GitHub Desktop.
Bash function to validate a long option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# SPDX-License-Identifier: MIT | |
# Copyright © 2022 Apolo Pena | |
### is_long_option ### | |
# Description: | |
# Validates a long option using the below rules | |
# Returns 0 if $1: | |
# Starts with double dashes followed by any number of uppercase or lowercase letters or integers | |
# optionally followed by zero or more sets of a single dash that must be accompanied | |
# by any number uppercase or lowercase letters | |
# Returns 1 otherwise | |
# | |
# Usage: | |
# t() {if is_long_option "$1"; then echo "$1 is a long option"; else echo "$1 is NOT a long option"; fi;} | |
# t '-o' # Outputs: -o is NOT a long option | |
# t '--valid' # Outputs: --valid is a long option | |
# t '-not-valid' # Outputs: -not-valid is NOT a long option | |
# t '--save-file' # Output --save-file is a valid long option | |
is_long_option() { | |
[[ $1 =~ ^--[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$ ]] && return 0 || return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment