-
Notifications
You must be signed in to change notification settings - Fork 63
feat:add filter/sort/limit to | shell completion | download #793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
feat:add filter/sort/limit to | shell completion | download #793
Conversation
|
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 |
| # 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() + ";" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the benefit of this over the currently existing btcli --install-completion?
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.