refactor: replace nested conditionals in get_var_attrs with declarative token registry - #178
Conversation
…ve token registry Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
There was a problem hiding this comment.
Pull request overview
Refactors BESS CLI auto-completion token handling in bessctl by replacing a large conditional chain in get_var_attrs() with a declarative token registry and helper candidate fetchers, aiming to simplify maintenance of completion behavior.
Changes:
- Introduced helper fetchers (
_fetch_candidates,_get_workers,_get_ports, etc.) to centralize RPC-backed candidate collection. - Added a
TOKEN_REGISTRYmapping tokens to(type, description, provider)to eliminate the largeif/elifchain. - Simplified
get_var_attrs()into a registry lookup + provider dispatch.
Suppressed comments (2)
bessctl/commands.py:285
[PORT_NUMBER]describes a port number but the text says "HTTP server address". This is user-facing help text shown during CLI guidance, so it should refer to the port.
'[PORT_NUMBER]': ('int', 'HTTP server address to listen on (default: 5000)', []),
bessctl/commands.py:283
[PAUSE_WORKERS]description dropped the default value information that existed previously, which is useful in the interactive help output. Consider restoring the default note.
'[PAUSE_WORKERS]': ('pause_workers', 'determines whether to pause workers',
['pause', 'no_pause']),
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>
|
Suggest wrapping all module lookups: 'MODULE': (..., lambda cli, word: _get_modules(cli)),
'MODULE...': (..., lambda cli, word: _get_modules(cli)),
'[MODULE]': (..., lambda cli, word: _get_modules(cli, True)), |
| 'MCLASS': ('name', 'name of a module class', _get_mclasses), | ||
| 'MCLASS...': ('name+', 'one or more module class names', _get_mclasses), | ||
| '[NEW_MODULE]': ('name', 'specify a name of the new module instance', []), | ||
| 'MODULE': ('name', 'name of an existing module instance', _get_modules), |
There was a problem hiding this comment.
get_var_attrs() currently uses the provider’s parameter count to decide whether to pass partial_word. That appears to break _get_modules(cli, include_star=False): it has two parameters, so MODULE and MODULE... are invoked as _get_modules(cli, partial_word). Any nonempty partial word is truthy and therefore enables include_star, adding * to completions which seems unintended.
Suggest wrapping all module lookups:
'MODULE': (..., lambda cli, word: _get_modules(cli)),
'MODULE...': (..., lambda cli, word: _get_modules(cli)),
'[MODULE]': (..., lambda cli, word: _get_modules(cli, True)),
Summary
This PR refactors commands.py for improved code quality. This PR focuses on refactoring the CLI auto-completion logic to eliminate a massive if/elif chain in get_var_attrs.
Key Changes