{"id":16787351,"url":"https://github.com/yaacov/argparse-sh","last_synced_at":"2025-06-29T22:36:06.014Z","repository":{"id":213390071,"uuid":"733976246","full_name":"yaacov/argparse-sh","owner":"yaacov","description":"argparse.sh contains bash functions that streamlines the management of command-line arguments in Bash scripts","archived":false,"fork":false,"pushed_at":"2023-12-21T06:11:49.000Z","size":40,"stargazers_count":11,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-14T08:14:43.340Z","etag":null,"topics":["argparse","argparser","bash","bash-script","command-line","command-line-parser"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaacov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-20T15:09:50.000Z","updated_at":"2024-10-13T21:07:09.000Z","dependencies_parsed_at":"2024-01-08T12:14:54.015Z","dependency_job_id":null,"html_url":"https://github.com/yaacov/argparse-sh","commit_stats":{"total_commits":21,"total_committers":1,"mean_commits":21.0,"dds":0.0,"last_synced_commit":"e0eced6ef98f024a0055735304f64b1ae1c90f75"},"previous_names":["yaacov/argparse-sh"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaacov%2Fargparse-sh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaacov%2Fargparse-sh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaacov%2Fargparse-sh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaacov%2Fargparse-sh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaacov","download_url":"https://codeload.github.com/yaacov/argparse-sh/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225986002,"owners_count":17555557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argparse","argparser","bash","bash-script","command-line","command-line-parser"],"created_at":"2024-10-13T08:14:45.002Z","updated_at":"2025-06-29T22:36:05.983Z","avatar_url":"https://github.com/yaacov.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# argparse-sh\n\n## Introduction\n\n`argparse.sh` contains Bash functions that streamlines the management of command-line arguments in Bash scripts, enhancing the robustness and user-friendliness of your code. Its syntax is intentionally designed to be familiar to those who have experience with argument parsing libraries, providing a smooth and intuitive scripting experience.\n\n## Features\n\n- **Easy Integration**: Simply source `argparse.sh` in your script to start using.\n- **Supports Various Argument Types**: Handles required, optional, and boolean flag arguments.\n- **Default Values**: Define default values for optional arguments.\n- **Automatic Help Generation**: Generates a help message based on defined arguments.\n- **Error Handling**: Provides user-friendly error messages for missing or incorrect arguments.\n- **Bash v3+ compatibility**: Use `argparse3.sh` if your script needs to support Bash v3. `argparse.sh` supports Bash v4+. \n\n[![asciicast](https://asciinema.org/a/627909.svg)](https://asciinema.org/a/627909)\n\n## Installation\n\nClone the script repository.\n\n```bash\ngit clone https://github.com/yaacov/argparse-sh.git\n```\n\nYou can now source `argparse.sh` in your Bash scripts to leverage its argument parsing capabilities.\n\n\u003e If you need compatiblity with Bash v3+, source `argparse3.sh` instead.\n\n```bash\nsource /path/to/argparse.sh\n# source /path/to/argparse3.sh # Use 'argparse3.sh' if you need compatibility with Bash v3\n\n# Your code here\n```\n\n## Usage\n\n### Defining Arguments with `define_arg`\n\nThe `define_arg` function allows you to define a new command-line argument. The syntax is:\n\n```bash\ndefine_arg \"arg_name\" [\"default\"] [\"help text\"] [\"action\"] [\"required\"]\n```\n\n| Parameter | Description | Optional | Default |\n| --- | --- | --- | --- |\n| arg_name | Name of the argument | No | |\n| default | Default value for the argument | Yes | \"\" (*) |\n| help text | Description of the argument for the help message | Yes | \"\" |\n| type | Type of the argument (string or store_true for flags) | Yes | \"string\" |\n| required | Whether the argument is required (`required`' or `optional`) | Yes | \"optional\" |\n\n*= For compatibility reasons with `argparse3.sh`, you can also use a *placeholder* to set an \"empty value\" in `argparse.sh`. By default, the *placeholder* value is `\"null\"`, but it can be defined setting the `_NULL_VALUE_` variable in `argparse.sh`.\n\n#### Setting default empty values in `argparse3.sh`\n\nBash v3 does not support **associative** arrays, so we store the properties of a defined argument as a *list* (integer-indexed array).\nElements in the list are separated by spaces. That removes the ability to store the default value for an *string* argument as an empty string.\nStoring the value `\"\"` (or `\" \"`, or any number of blank spaces) is interpreted by Bash as \"space\" and thus, considered an item separator, and not a *value* in the list.\n\nFor that reason, you need to use a *placeholder* (`\"null\"`, by default) if you want to define an \"empty string\" (`\"\"`) as the default value of an argument of type *string* in `argparse3.sh`.\n\nBy default, `argparse3.sh` uses `\"null\"` as the *placeholder* for an empty default value.\nYou can define the \"empty\" value changing the `_NULL_VALUE_` variable in `argparse3.sh`.\n\n```bash\n# 'argparse.sh', Bash v4+\ndefine_arg \"username\" \"\" \"name of the user\" \"string\" \"optional\"\n# 'argparse3.sh', Bash v3+\ndefine_arg \"username\" \"null\" \"name of the user\" \"string\" \"optional\"\n```\n\n### Setting Script Description with `set_description`\n\nThe optional `set_description` function sets a description for your script, which appears at the top of the automatically generated help message. Usage is straightforward:\n\n```bash\nset_description \"Your script description here\"\n```\n\nWhen not set, the help text will show without a description text.\n\n### Parsing Arguments with `parse_args`\n\nAfter defining your arguments, use the `parse_args` function to parse the command-line inputs. Simply pass `\"$@\"` (the array of command-line arguments) to this function:\n\n```bash\nparse_args \"$@\"\n```\n\nThis function will process the inputs based on the defined arguments and handle errors or help message requests automatically.\n\n## Example\n\nHere's a simple example of a script using argparse.sh:\n\n```bash\n\n#!/bin/bash\n\n# Source the argparse script\nsource ./argparse.sh\n\n# [Optional] Set script description\nset_description \"This is a simple script that greets the user.\"\n\n# Define an argument\ndefine_arg \"name\" \"\" \"Name of the user\" \"string\" \"required\"\n\n# [Optional] Check for -h and --help\ncheck_for_help \"$@\"\n\n# Parse the arguments\nparse_args \"$@\"\n\n# Main script logic\necho \"Hello, $name!\"\n```\n\nRun this script using:\n\n```bash\n./yourscript.sh --name Alice\n```\n\n## Contributing\n\nWe warmly welcome contributions to `argparse.sh`. If you have an idea for an improvement or have spotted an issue, feel free to contribute. Start by forking the repository and cloning your fork to your local machine. Create a new branch for your feature or fix. Once you've made your changes, commit them with a clear and descriptive message. After committing your changes, push them to your fork on GitHub. Finally, submit a pull request from your branch to the main repository. We appreciate your efforts in enhancing `argparse.sh` and look forward to your valuable input!\n\n## License\n\nThis project is licensed under the [MIT License](https://github.com/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaacov%2Fargparse-sh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaacov%2Fargparse-sh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaacov%2Fargparse-sh/lists"}