{"id":24600142,"url":"https://github.com/integralist/zsh-cli-json-parser","last_synced_at":"2025-03-18T06:41:43.161Z","repository":{"id":273359923,"uuid":"919433046","full_name":"Integralist/zsh-cli-json-parser","owner":"Integralist","description":"Zsh script that uses jq to parse a JSON structure of a CLI","archived":false,"fork":false,"pushed_at":"2025-01-20T12:27:17.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-20T13:24:08.936Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Integralist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2025-01-20T11:34:15.000Z","updated_at":"2025-01-20T12:27:18.000Z","dependencies_parsed_at":"2025-01-20T13:24:14.010Z","dependency_job_id":"4a741cb2-2f74-4df7-87e6-6aedf6e34566","html_url":"https://github.com/Integralist/zsh-cli-json-parser","commit_stats":null,"previous_names":["integralist/zsh-cli-json-parser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Integralist%2Fzsh-cli-json-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Integralist%2Fzsh-cli-json-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Integralist%2Fzsh-cli-json-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Integralist%2Fzsh-cli-json-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Integralist","download_url":"https://codeload.github.com/Integralist/zsh-cli-json-parser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244173495,"owners_count":20410295,"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":[],"created_at":"2025-01-24T13:19:04.079Z","updated_at":"2025-03-18T06:41:43.134Z","avatar_url":"https://github.com/Integralist.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zsh-cli-json-parser\n\n## `get_cli_options`\n\nThe file [get_cli_options.zsh](get_cli_options.zsh) parses a JSON structure and\nreturns the relevant subcommands and flag options:\n\n\u003e \\[!NOTE\\]\n\u003e This isn't a recursive implementation.\\\n\u003e We stop at 4 levels deep.\n\n## Example JSON structure\n\nThe [example.json](example.json) file demonstrates what structure is required to\nbe passed to the `get_cli_options` shell function.\n\n## Example Output\n\nUsing `example.json` as our example structure, we should see the following\noutput:\n\n```zsh\n# show the top-level commands\n$ get_cli_options \"example.json\" \"\"\nservice\nacl\n\n# show the top-level/global flag options\n$ get_cli_options \"example.json\" \"--\"\n--help\n--quiet\n--verbose\n\n# show subcommands and flags\n$ get_cli_options \"example.json\" \"acl\"\n--help-acl\ncreate\nlist\n\n# show command flags only\n$ get_cli_options \"example.json\" \"acl --\"\n--help-acl\n\n# show subcommands and flags under a subcommand\n$ get_cli_options \"example.json\" \"acl list\"\n--json\nthird-level\n\n# show subcommand flags only\n$ get_cli_options \"example.json\" \"acl list --\"\n--json\n\n# show subcommands and flags under a third-level subcommand\n$ get_cli_options \"example.json\" \"acl list third-level\"\n--foo\n--bar\nfourth-level\n\n# show third-level subcommand flags only\n$ get_cli_options \"example.json\" \"acl list third-level --\"\n--foo\n--bar\n```\n\n## Zsh Autocomplete\n\nYou can plug this script into your zsh shell completion setup like so:\n\n\u003e \\[!NOTE\\]\n\u003e The following example is for setting up autocomplete for a binary called\n\u003e `example` (which uses the `example.json` from this repo as its structure). I\n\u003e typically store these files in `$HOME/.zsh/`, so in this case it would be\n\u003e stored as `$HOME/.zsh/_example`.\n\n```zsh\n#compdef fastly\nautoload -U compinit \u0026\u0026 compinit\nautoload -U bashcompinit \u0026\u0026 bashcompinit\n\n_example_bash_autocomplete() {\n    local cur opts input_str\n    COMPREPLY=()\n\n    # Current word being completed\n    cur=\"${COMP_WORDS[COMP_CWORD]}\"\n\n    # Reconstruct the input arguments excluding the binary name\n    local input=(\"${COMP_WORDS[@]:1}\")\n\n\t# Join the array into a single string, trimming excess whitespace\n    input_str=$(echo \"${input[*]}\" | sed 's/ *$//')\n\n    # Debugging: Log the exact call to get_cli_options\n    # echo \"Calling get_cli_options with input_str: '$input_str'\" \u003e\u003e /tmp/autocomplete-debug.log\n\n    # Pass the reconstructed input string to get_cli_options\n    opts=$(get_cli_options \"$HOME/.zsh/example.json\" \"$input_str\")\n\n    # Log the result of get_cli_options\n    # echo \"opts: $opts\" \u003e\u003e /tmp/autocomplete-debug.log\n\n    # Generate completions based on opts\n    COMPREPLY=( $(compgen -W \"${opts}\" -- \"${cur}\") )\n\n    # Fall back to file completion if no matches are found\n    [[ $COMPREPLY ]] \u0026\u0026 return\n    compgen -f\n    return 0\n}\ncomplete -F _example_bash_autocomplete example\n```\n\nYou'll then need to source the above `_example` script:\n\n```zsh\ndir_zsh=\"$HOME/.zsh\"\npath_example_completion=\"$dir_zsh/_example\"\nchmod +x $path_example_completion\nsource \"$path_example_completion\"\n```\n\nI'm not sure how you have your Zsh autocomplete setup, but I need to add this\ncustom directory to my `fpath`:\n\n```zsh\nfpath=($dir_zsh $fpath)\n```\n\n\u003e \\[!TIP\\]\n\u003e If you're looking for an example autocomplete setup, then view my [dot\n\u003e files][1].\n\n[1]: https://github.com/Integralist/dotfiles/blob/main/.config/zsh/autocomplete.zsh\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintegralist%2Fzsh-cli-json-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintegralist%2Fzsh-cli-json-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintegralist%2Fzsh-cli-json-parser/lists"}