feat:add filter/sort/limit to | shell completion | download#793
Open
michaelj094 wants to merge 5 commits intoopentensor:stagingfrom
Open
feat:add filter/sort/limit to | shell completion | download#793michaelj094 wants to merge 5 commits intoopentensor:stagingfrom
michaelj094 wants to merge 5 commits intoopentensor:stagingfrom
Conversation
Author
|
Hi @ibraheem-abe, I hope you’re doing well! I’ve submitted a PR for adding filter and output to subnets & metagraph and shell completion: #793 When you have a chance, I’d really appreciate your review and any feedback. Thank you so much! Best regards, Michael |
Comment on lines
+1963
to
+2024
| # Note, only BASH version 4.4 and later have the nosort option. | ||
| COMPLETION_SCRIPT_BASH = """ | ||
| %(complete_func)s() { | ||
| local IFS=$'\n' | ||
| COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\ | ||
| COMP_CWORD=$COMP_CWORD \\ | ||
| %(autocomplete_var)s=complete $1 ) ) | ||
| return 0 | ||
| } | ||
| %(complete_func)setup() { | ||
| local COMPLETION_OPTIONS="" | ||
| local BASH_VERSION_ARR=(${BASH_VERSION//./ }) | ||
| # Only BASH version 4.4 and later have the nosort option. | ||
| if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then | ||
| COMPLETION_OPTIONS="-o nosort" | ||
| fi | ||
| complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s | ||
| } | ||
| %(complete_func)setup | ||
| """ | ||
|
|
||
| COMPLETION_SCRIPT_ZSH = """ | ||
| %(complete_func)s() { | ||
| local -a completions | ||
| local -a completions_with_descriptions | ||
| local -a response | ||
| response=("${(@f)$( env COMP_WORDS=\"${words[*]}\" \\ | ||
| COMP_CWORD=$((CURRENT-1)) \\ | ||
| %(autocomplete_var)s=\"complete_zsh\" \\ | ||
| %(script_names)s )}") | ||
| for key descr in ${(kv)response}; do | ||
| if [[ "$descr" == "_" ]]; then | ||
| completions+=("$key") | ||
| else | ||
| completions_with_descriptions+=("$key":"$descr") | ||
| fi | ||
| done | ||
| if [ -n "$completions_with_descriptions" ]; then | ||
| _describe -V unsorted completions_with_descriptions -U -Q | ||
| fi | ||
| if [ -n "$completions" ]; then | ||
| compadd -U -V unsorted -Q -a completions | ||
| fi | ||
| compstate[insert]="automenu" | ||
| } | ||
| compdef %(complete_func)s %(script_names)s | ||
| """ | ||
|
|
||
| _invalid_ident_char_re = re.compile(r"[^a-zA-Z0-9_]") | ||
|
|
||
|
|
||
| def get_completion_script(prog_name, complete_var, shell): | ||
| cf_name = _invalid_ident_char_re.sub("", prog_name.replace("-", "_")) | ||
| script = COMPLETION_SCRIPT_ZSH if shell == "zsh" else COMPLETION_SCRIPT_BASH | ||
| return ( | ||
| script | ||
| % { | ||
| "complete_func": "_%s_completion" % cf_name, | ||
| "script_names": prog_name, | ||
| "autocomplete_var": complete_var, | ||
| } | ||
| ).strip() + ";" |
Contributor
There was a problem hiding this comment.
What's the benefit of this over the currently existing btcli --install-completion?
thewhaleking
requested changes
Jan 22, 2026
Contributor
thewhaleking
left a comment
There was a problem hiding this comment.
I think this is a neat and novel approach. Two things I will recommend, however:
- Remove the install completion stuff. The standard
btcli --install-completionworks great, and doesn't require maintenance on our part. - Your new reused options should live in the Options class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: Add filter and output to subnets & metagraph,shell completion
Summary
This PR adds advanced filtering, sorting, column-selection, limiting, and structured output options to the
subnets listcommand and extends the same usability flags to thesubnets metagraphcommand. It also introduces a top-levelbtcli completioncommand for easy shell completion installation and a set of improvements to output handling and documentation.Description of Changes
btcli completioncommand to generate and optionally install shell completion scripts for Bash, Zsh, Fish, and PowerShell. README updated with instructions.subnets listcommand with many usability flags (filter, sort, limit, columns, output format, pager, header control, wide mode).subnets metagraphcommand so users can filter/sort/limit neurons, select columns, and export JSON/YAML.subnets listandsubnets metagraph.BTCLI_PAGERenvironment variable to page large table outputs.Motivation and Context
Working with large subnet lists or full metagraphs can be cumbersome. These changes give users granular control over list-like outputs (filtering, sorting, limiting, and column selection), allow structured export (JSON/YAML), and improve developer experience via modular helpers and tests. Shell completion increases productivity for interactive use.
Files Changed (high level)
README.md— shell completion docs and output tipsbittensor_cli/cli.py— added flags and validation,completioncommand, pass-through of new optionsbittensor_cli/src/commands/subnets/subnets.py—filter_sort_limit_subnets(existing commit) andfilter_sort_limit_metagraph_rowshelperssubnets_listandmetagraph_cmdto accept and honor new options (output, columns, no-header, wide, uids/hotkey-contains, sort-by, sort-order, limit)BTCLI_PAGERis settests/unit_tests/test_subnets_list_sort_filter.py— unit tests for subnets list helpertests/unit_tests/test_metagraph_list_sort_filter.py— unit tests for metagraph helper (new)(See commit for exact diffs and line-level changes.)
New CLI Flags (examples)
--netuids/-n--uids/-u--name-contains/--name--hotkey-contains/--hotkey--sort-by--sort-orderasc) or descending (desc).--limit/--top--output/-otable,json,yaml. Overrides legacy--json-output.--columns--no-header--wide/-wUsage Examples
Filter + sort + limit (subnets):
btcli subnets list \ --netuids 0,2 \ --name-contains "AI" \ --sort-by market_cap \ --sort-order desc \ --limit 5 \ --output json \ --columns netuid,name,market_cap \ --no-header \ --wideMetagraph: list top 10 neurons by global stake for a subnet, output as YAML:
Metagraph: filter by hotkey substring and show columns in a wide table:
Shell completion installation (examples):
Testing Performed
btcli completionfor supported shells (script output and--install).subnets listandsubnets metagraphwith combinations of filters,--columns,--output,--limit, and--wide.tests/unit_tests/test_subnets_list_sort_filter.py(existing)tests/unit_tests/test_metagraph_list_sort_filter.py(new)To run the new tests locally:
Backwards compatibility & notes
--outputflag supersedes--json-outputwhen provided; legacy--json-outputbehavior is preserved unless overridden by--output.--html(metagraph) is mutually exclusive with structuredjson/yamloutputs.--columnsoverrides the per-user configmetagraph_colsfor that invocation only.Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=191372963