Select Page

Simplify Your Bash Scripting with Argbash

by | May 3, 2024 | Miscellaneous | 0 comments

Bash scripting is a powerful skill for any Linux or Unix administrator or developer. Bash scripts can automate repetitive tasks, perform system maintenance, and deploy applications. However, parsing command-line arguments can be a daunting task, especially for complex scripts. Fortunately, there’s a tool called Argbash that simplifies the process of parsing command-line arguments.

In this article, we will introduce you to Argbash and show you how to use it to simplify your bash scripting.

What is Argbash?

Argbash is a code generator that can generate bash scripts that parse command-line arguments. It simplifies the process of handling command-line arguments by generating bash code that handles the parsing for you. Argbash can generate code in various formats, such as shell functions, standalone scripts, and even Makefiles.

How to Use Argbash

The easiest way to use Argbash is through the Argbash website (https://argbash.io/). The website provides a graphical user interface that allows you to define your command-line arguments and generates the corresponding bash code.

Here’s how to use the Argbash website:

 

  1. Go to https://argbash.dev/.
  2. Click on the “Start Creating!” button to create a new template.
  3. Define your command-line arguments in the “Command-line arguments” section.
  4. Choose the code format you want Argbash to generate (e.g., shell function, standalone script, Makefile).
  5. Click on the “Generate!” button to generate the bash code.
  6. Copy the generated code and paste it into your bash script.

Here’s an example of a template file that defines three command-line arguments: -f, which specifies a file name, -d, which specifies a directory, and –verbose, which enables verbose mode:

#!/usr/bin/env argbash
# argbash template
#   --mode=generate
#   --name=my-script
#   --description="My awesome script"
#   --author="Jane Doe <jane.doe@example.com>"

# Required arguments
arg 'filename' -f --filename help='the file name' type=file
arg 'directory' -d --directory help='the directory' type=dir

# Optional arguments
arg '--verbose' help='enable verbose mode' action='store_true'

# Post-arg-parsing code
# Your code here
echo "filename = ${ARGS_FILENAME}"
echo "directory = ${ARGS_DIRECTORY}"
if [ "${ARGS_VERBOSE}" = true ]; then
    echo "Verbose mode enabled"
fi

The template file defines two required arguments, -f or –filename and -d or –directory, which are of type file and directory, respectively. It also defines an optional argument, –verbose, which enables verbose mode.

The arg command is used to define the arguments. The type parameter specifies the argument type (file, dir, string, etc.), and the action parameter specifies the action to take when the argument is specified (store the value, enable a flag, etc.).

The generated code is a standalone script that handles the parsing of the command-line arguments. You can integrate it into your script by sourcing it or by copying the relevant code into your script.

Conclusion

Argbash is a powerful tool that simplifies the process of parsing command-line arguments in bash scripts. With Argbash, you can define your command-line arguments in a template file, generate the parsing code, and integrate it into your script. This saves you time and makes your scripts more maintainable and readable. Try out Argbash on the Argbash website today and see how it can simplify your bash scripting!