Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:35:38+00:00 2026-06-06T20:35:38+00:00

I am looking for a reusable code snippet that does command line argument validation

  • 0

I am looking for a reusable code snippet that does command line argument validation for bash.

Ideally something akin to the functionality offered by Apache Commons CLI:

Commons CLI supports different types of options:

  • POSIX like options (ie. tar -zxvf foo.tar.gz)
  • GNU like long options (ie. du –human-readable –max-depth=1)
  • Short options with value attached (ie. gcc -O2 foo.c)
  • long options with single hyphen (ie. ant -projecthelp)
  • …

and it generates a “usage” message for the program automatically, like this:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entried ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns

I would include this code snippet at the beginning of my Bash scripts and reuse it across scripts.

There must be something like this. I don’t believe we are all writing code to this effect or similar:

#!/bin/bash

NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS=3

number_of_supplied_command_line_arguments=$#

function show_command_usage() {
  echo usage:
  (...)
}

if (( number_of_supplied_command_line_arguments < NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS )); then
  show_command_usage
  exit
fi

...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:35:41+00:00Added an answer on June 6, 2026 at 8:35 pm

    This is the solution I use (found it on the net somewhere, probably here itself, don’t remember for sure). Please note that the GNU getopt (/usr/bin/getopt) does support single dash long options (ant -projecthelp style) using the option -a, however I haven’t used it so it is not shown in the example.

    This code parses for 3 options: --host value or -h value, --port value or -p value and --table value or -t value. In case the required parameter isn’t set, a test for it is

    # Get and parse options using /usr/bin/getopt
    OPTIONS=$(getopt -o h:p:t: --long host:,port:,table: -n "$0" -- "$@")
    # Note the quotes around `$OPTIONS': they are essential for handling spaces in 
    # option values!
    eval set -- "$OPTIONS"
    
    while true ; do
        case "$1" in
                -h|--host) HOST=$2 ; shift 2 ;;
                -t|--table)TABLE=$2 ; shift 2 ;;
                -p|--port)
                        case "$2" in
                                "") PORT=1313; shift 2 ;;
                                *)  PORT=$2; shift 2 ;;
                        esac;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
    done
    if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
        usage()
        exit
    if
    

    An alternative implementation using the getopts shell builtin(this only supports small options):

    while getopts ":h:p:t:" option; do
        case "$option" in
             h) HOST=$OPTARG ;;
             p) PORT=$OPTARG ;;
             t) TABLE=$OPTARG ;;
            *) usage(); exit 1 ;;
        esac
    done
    if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
        usage()
        exit
    if
    
    shift $((OPTIND - 1))
    

    Further reading for GNU getopt and getopts bash builtin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write reusable code that takes an HTML table from a JSP
I am looking for reusable open source components. The level of depth, breadth and
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for a control that allows to select one text value at a time
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Looking around at different code bases I see a variety of styles: Class interfaces
I am looking for a general, reusable UI pattern I can use for editing
I find myself writing a lot of reusable code because I couldn't find reusable
I'm looking for an efficient and reusable way to parse xml into an object
I'm looking for a good way to implement reusable buttons in CSS on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.