{"id":21986633,"url":"https://github.com/alvations/cranium","last_synced_at":"2025-10-10T14:05:15.253Z","repository":{"id":74469348,"uuid":"103336513","full_name":"alvations/cranium","owner":"alvations","description":"Bashing CLI arguments","archived":false,"fork":false,"pushed_at":"2017-09-13T05:16:08.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T02:22:27.365Z","etag":null,"topics":["argparse","bash","docopt","json","python"],"latest_commit_sha":null,"homepage":"","language":"Python","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/alvations.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-13T01:06:02.000Z","updated_at":"2024-09-10T06:05:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e7731bc-7dd1-49ed-b7f0-ce44929a56ad","html_url":"https://github.com/alvations/cranium","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alvations/cranium","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvations%2Fcranium","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvations%2Fcranium/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvations%2Fcranium/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvations%2Fcranium/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvations","download_url":"https://codeload.github.com/alvations/cranium/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvations%2Fcranium/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004184,"owners_count":26083688,"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","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","bash","docopt","json","python"],"created_at":"2024-11-29T18:20:09.513Z","updated_at":"2025-10-10T14:05:15.219Z","avatar_url":"https://github.com/alvations.png","language":"Python","readme":"# Cranium: Bashing CLI arguments\n\nBecause I'm sick of writing `argparse`.\n\nThe idea is to\n\n 1. Write argument parsing documentation using `docopt` (in Python)\n 2. Use Python `docopt` to parse the arguments and save them as JSON and\n 3. Read the JSON file in the shell script\n\n\nDependencies\n====\n\n```\napt-get install jq  # `brew install jq` for Mac OS\napt-get install pip # `brew install pip` for Mac OS\npip install docopt\n```\n\nUsage (**TL;DR**)\n====\n\nWrites a Python script with docopt to parse your arguments, e.g. see barathrum.py\n\n```\n$ python barathrum.py\nUsage:\n  barathrum.py FILE [-g BASHLVL] [-e HASTELVL] [-d] [-n]\n  barathrum.py (-h | --help)\n  barathrum.py --version\n```\n\nMake the Python script output a JSON file with the variable-value pairs, e.g.\n\n```\n$ python barathrum.py ancientscroll.txt -g 5 -n -d\n{\"dagger\": true, \"version\": false, \"nether_strike\": true, \"greater_bash\": \"5\", \"FILE\": \"ancientscroll.txt\", \"empower_haste\": \"1\", \"help\": false}\n\n$ python barathrum.py ancientscroll.txt -g 5 -n -d \u003e arguments.json\n\n$ cat arguments.json\n{\"dagger\": true, \"version\": false, \"nether_strike\": true, \"greater_bash\": \"5\", \"FILE\": \"ancientscroll.txt\", \"empower_haste\": \"1\", \"help\": false}\n```\n\nIn your shell script pass the `\"$@\"` arguments to the Python script to output a json.\n\n```\n#!/bin/bash\n\npython barathrum.py \"$@\" \u003e arguments.json\n\nwhile read -r name value; do\n   declare \"$name=$value\"\ndone \u003c \u003c(cat arguments.json | jq -r 'to_entries[] | \"\\(.key) \\(.value)\"')\n\necho $dagger\necho greater_bash\necho empower_haste\n```\n\n\nEt voila!\n\n```\n$ bash cranium.sh ancientscroll.txt -g 5 -n -d\ntrue\n5\n1\n```\n\n\nUsage (**In Long**)\n====\n\n**Step 1**:\n\nCreate a Python file with arguments options and usage definitions that\nfollows the Python `docopt` style guide, e.g. in `barathrum.py`:\n\n```shell\nUsage:\n  barathrum.py FILE [-g BASHLVL] [-e HASTELVL] [-d] [-n]\n  barathrum.py (-h | --help)\n  barathrum.py --version\n\nOptions:\n  -g --greater-bash=BASHLVL     Sets the level to knocking back enemies [default: 0].\n  -n --nether-strike            Shifts into elemental realm and reappearing up close to the enemies.\n  -e --empower-haste=HASTELVL   Increases speed and inspires nearby allies to keep up the pace [default: 1].\n  -d --dagger                   Use the dagger to teleport a short distant.\n  --version                     Show version.\n  -h --help                     Show this screen.\n```\n\n**Step 2:**\n\nCopy+paste the `main()` function below into the Python file as such:\n\n\n```python\nfrom __future__ import print_function\nimport json\n\nfrom docopt import docopt\n\nif __name__ == '__main__':\n    # Remeber to change the version measge and name =)\n    arguments = docopt(__doc__, version='Barathrum Example for cranium.sh - version 0.0.1')\n    argdict = {}\n    for k, v in arguments.items():\n        if k.startswith('--'):\n            k = k[2:].replace('-', '_')\n        argdict[k] = v\n    print(json.dumgitps(argdict))\n```\n\n**Step 3:**\n\nIn your shell script, use the `\"$@\"` operator to pass all the arguments to the\nPython script with `docopt` you've previously written:\n\n```shell\n# Input the arguments into a JSON file as specified by `barathrum.py`\npython barathrum.py \"$@\" \u003e arguments.json\n```\n\nThen, you can read the variables individually as such:\n\n```shell\n# Initialize the arguments according.\ndagger=$(cat arguments.json | jq -r '.[\"dagger\"]')\ngreater_bash=$(cat arguments.json | jq -r '.[\"greater_bash\"]')\n\necho $dagger\necho $greater_bash\n```\n\nOr initialize them all at one go ([Credit goes to @chepner](https://stackoverflow.com/q/46187807/610569)):\n\n```shell\nwhile read -r name value; do\n   declare \"$name=$value\"\ndone \u003c \u003c(cat arguments.json | jq -r 'to_entries[] | \"\\(.key) \\(.value)\"')\n\necho $empower_haste\necho $greater_bash\n```\n\n\nQnA\n====\n\n**Q: Why don't you use the [`docopt` for bash](https://github.com/docopt/docopts) directly?**\n\n**A:** Cos I don't want to retype what's in the docstring and initialize the arguments individually. Also, I find it really ugly to have `#?` and `##?` on every line of comment (my personal preference, no offense intended).\n\n----\n\n**Q: Why don't you use [`argparse-bash`](https://github.com/nhoffman/argparse-bash) that also uses Python?**\n\n**A:** Cos I can't write my arguments `--help` message in the docstring.\n\n----\n\n**Q: Is there really a need to ask `Bash` to call `Python` to use `docopt` just to convert the arguments to `JSON` and read it through `jq` in shell?**\n\n**A:** Nope, no need to but I just prefer to read JSON files as opposed to writing `argparse` in shell.\n\n\n----\n\n**Q: Don't we still have to write the `docopt` docstring in Python to read our customized arguments options?**\n\n**A:** Yes, doesn't that make your arguments parsing and the `--help` messages more beautiful?\n\n\nSimilar / Related to\n====\n\n - [argparse-bash](https://github.com/nhoffman/argparse-bash): Use python's argparse module in shell scripts\n - [docopt](http://docopt.org): Command-line interface description language\n - [jq](https://stedolan.github.io/jq): Lightweight and flexible command-line JSON processor\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvations%2Fcranium","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvations%2Fcranium","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvations%2Fcranium/lists"}