DEV Community

Brandon Rozek
Brandon Rozek

Posted on • Originally published at brandonrozek.com on

Partial Argument Parse and Passing in Bash

Let’s say we want to augment an existing terminal command (like for example wget). We then want to be able to add or edit command line options. The rest of this post provides an example that hopefully you can use in your bash script.

#!/bin/bash

# Custom help function
show_help() {
  echo "Usage: custom_command [arguments]"
  echo " --name <name>"
  echo " --flag_example"
  echo " <additional arguments to be passed along>"
  exit 0
}

# Defaults for our custom flags or parameters.
name=""
flag_example=0

# Loop through and take out our custom parameters
# from the parameter list.
i=0
numargs=$#
while test $i -lt "$numargs"
do
  case "$1" in
    "--help")
      show_help
      ;;
    "--name")
      shift
      name=$1
      ;;
    "--flag_example")
      flag_example=1
      ;;
    *)
      set -- "$@" "$1"
      ;;
  esac
  shift
  i=$((i+1))
done

# Do something here using our custom parameters

# Pass our non-custom parameters to the application
wget "$@"

Enter fullscreen mode Exit fullscreen mode

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay