How to write a command line tool using bash

bash

Solution

So far as I know, "long options", like `--help` and `--version` are not POSIX standard, but GNU standard. For command-line utilities the POSIX standard says:

The arguments that consist of hyphens and single letters or digits, such as 'a', are known as "options" (or, historically, "flags").

To support POSIX short options options it is worth getting to know `getopts` (there are tutorials on the web), but it does not support GNU long options.

For long options you have to roll your own:

filename=default
while (( $# > 0 ))
do
    opt="$1"
    shift

    case $opt in
    --help)
        helpfunc
        exit 0
        ;;
    --version)
        echo "$0 version $version"
        exit 0
        ;;
    --file)  # Example with an operand
        filename="$1"
        shift
        ;;
    --*)
        echo "Invalid option: '$opt'" >&2
        exit 1
        ;;
    *)
        # end of long options
        break;
        ;;
   esac

done

Problem

I want to write a command line tool like git which will follow the `POSIX` standards. It will take the options like `--help` or `-h` , `--version` ..etc. But i am not getting how to do it. Can anybody tell me how to do this using bash scripting. Please help me. This is something very new to me. Example : if the name of my tool is Check-code then i want to use the tool like ; ``` Check-code --help ``` or ``` Check-code --version ```

Original source