https://github.com/mohammadreza-alizadeh/argparser
a lightweight CLI argument parser with bash
https://github.com/mohammadreza-alizadeh/argparser
argparse bash bash-script lightweight
Last synced: 2 months ago
JSON representation
a lightweight CLI argument parser with bash
- Host: GitHub
- URL: https://github.com/mohammadreza-alizadeh/argparser
- Owner: Mohammadreza-Alizadeh
- Created: 2025-03-25T08:38:51.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-26T07:49:46.000Z (over 1 year ago)
- Last Synced: 2025-03-26T08:34:09.626Z (over 1 year ago)
- Topics: argparse, bash, bash-script, lightweight
- Language: Shell
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bash script ArgParser
A reusable, type-aware argument parser for Bash scripts to help you undrestand what a user say
i didn't find a minimal and no-dependency module to use when my scripts need `Argument Parsing`, so here we are.
with this script you can easily manage your script `argument parsing` logic, just define the option you want to recive from user and let the script handle its validation
for anyone wonders, this script is influenced from `python's argparse`
## ๐งฉ Features
- Short & long flags (e.g. `-f` and `--file`)
- Required and optional arguments
- Argument types: `dir`, `number`, `file`
- Flag safety: avoids consuming other flags as values
- Auto help generation
## ๐งฉ Usage
### ๐ Define Options
Use the `define_option` function:
```bash
define_option SHORT LONG HAS_ARG DESCRIPTION REQUIRED TYPE DEFAULT
```
| Parameter | Description |
|---------------|--------------------------------------------------|
| `SHORT` | Single-letter flag (e.g. `f` for `-f`) |
| `LONG` | Long-form flag (e.g. `file` for `--file`) |
| `HAS_ARG` | `"yes"` if flag takes an argument, otherwise `"no"` |
| `DESCRIPTION` | Help description shown to users |
| `REQUIRED` | `"required"` or `"optional"` |
| `TYPE` | `file`, `number`, `dir`, or leave empty for any |
| `DEFAULT` | Default value if not provided (optional only) |
### ๐งช Example
```bash
define_option "f" "file" "yes" "Input file" "required" "file" ""
define_option "n" "number" "yes" "Number of threads" "optional" "number" "21"
define_option "d" "directory" "yes" "Input directory" "optional" "dir" ""
define_option "s" "label" "yes" "Label name" "optional" "" ""
```
---
### ๐ Parsing and Accessing Values
```bash
parse_args "$@"
echo "${ARGS[file]}"
echo "${ARGS[number]}"
```
see the `examples` directory for a in action use of script