getopts-parser-generator
npx machina-cli add skill a5c-ai/babysitter/getopts-parser-generator --openclawFiles (1)
SKILL.md
2.8 KB
Getopts Parser Generator
Generate getopts-based argument parsing for shell scripts.
Capabilities
- Generate getopts parsing code
- Support short and long options
- Implement option validation
- Create help text generation
- Handle required arguments
- Generate usage documentation
Usage
Invoke this skill when you need to:
- Add argument parsing to shell scripts
- Support short and long options
- Validate argument values
- Generate help text
Generated Patterns
Getopts with Long Options
#!/usr/bin/env bash
# Default values
VERBOSE=false
OUTPUT=""
FORMAT="text"
FORCE=false
# Usage function
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS] <input>
Options:
-h, --help Show this help
-v, --verbose Enable verbose output
-o, --output FILE Output file (default: stdout)
-f, --format FORMAT Output format: text, json, csv (default: text)
-F, --force Force overwrite existing files
Arguments:
input Input file to process
EOF
}
# Parse options
parse_options() {
# Transform long options to short ones
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--verbose') set -- "$@" '-v' ;;
'--output') set -- "$@" '-o' ;;
'--format') set -- "$@" '-f' ;;
'--force') set -- "$@" '-F' ;;
*) set -- "$@" "$arg" ;;
esac
done
# Parse short options
local OPTIND opt
while getopts ":hvo:f:F" opt; do
case "$opt" in
h) usage; exit 0 ;;
v) VERBOSE=true ;;
o) OUTPUT="$OPTARG" ;;
f)
case "$OPTARG" in
text|json|csv) FORMAT="$OPTARG" ;;
*) die "Invalid format: $OPTARG" ;;
esac
;;
F) FORCE=true ;;
:) die "Option -$OPTARG requires an argument" ;;
\?) die "Unknown option: -$OPTARG" ;;
esac
done
shift $((OPTIND - 1))
# Remaining arguments
if [[ $# -lt 1 ]]; then
usage
die "Missing required argument: input"
fi
INPUT="$1"
shift
# Extra arguments
EXTRA_ARGS=("$@")
}
die() {
echo "Error: $*" >&2
exit 1
}
# Parse and validate
parse_options "$@"
# Validation
[[ -f "$INPUT" ]] || die "Input file not found: $INPUT"
[[ -n "$OUTPUT" && -f "$OUTPUT" && "$FORCE" != true ]] && \
die "Output file exists: $OUTPUT (use --force to overwrite)"
Target Processes
- shell-script-development
- shell-completion-scripts
- argument-parser-setup
Source
git clone https://github.com/a5c-ai/babysitter/blob/main/plugins/babysitter/skills/babysit/process/specializations/cli-mcp-development/skills/getopts-parser-generator/SKILL.mdView on GitHub Overview
This skill creates robust getopts parsing code for Bash scripts, supporting short and long options, validation, and helpful usage text. It automates option handling, defaults, required arguments, and documentation generation.
How This Skill Works
It builds a parsing scaffold that maps long options to short equivalents, then processes them with getopts, storing results in variables and applying defaults. It includes validation checks, error handling, and a reusable usage() function to guide users.
When to Use It
- Add argument parsing to a Bash script
- Support both short (-v) and long (--verbose) options
- Validate option values and enforce required arguments
- Generate a help/usage block automatically
- Guard against overwriting files without --force
Quick Start
- Step 1: Define defaults, usage(), and allowed options
- Step 2: Normalize long options to short ones and parse with getopts
- Step 3: Validate inputs, enforce required args, and handle errors
Best Practices
- Normalize long options to short ones for a single getopts pass
- Validate OPTARG against allowed values early and clearly
- Provide a comprehensive usage()/help block with defaults
- Document which arguments are required and which have defaults
- Include a die() helper for consistent error exits
Example Use Cases
- A file converter script that accepts -i/--input and -o/--output with a --format option
- A log generator that uses -v/--verbose and --force to control output
- A data validator that requires an input file and a --format option
- An automation script that displays usage when -h/--help is invoked
- A script that refuses to overwrite unless --force is provided
Frequently Asked Questions
Add this skill to your agents