Skip to content

refactor: replace nested conditionals in get_var_attrs with declarative token registry - #178

Open
bhagathkrishnacdac wants to merge 1 commit into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-commands
Open

refactor: replace nested conditionals in get_var_attrs with declarative token registry#178
bhagathkrishnacdac wants to merge 1 commit into
omec-project:mainfrom
bhagathkrishnacdac:bess-refactor-commands

Conversation

@bhagathkrishnacdac

Copy link
Copy Markdown

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

  • Declarative Token Map (TOKEN_REGISTRY): Established a centralized map of autocomplete tokens to their respective types, descriptors, and candidate-provider lists.
  • Unified RPC Wrapper (_fetch_candidates): Introduced a standardized helper to wrap BESS RPC data queries, ensuring safe execution and uniform error-handling during active tab completion.
  • Dynamic Fetchers: Extracted dynamic data-gathering logic (e.g., worker status, port lists, module classes, active traffic classes) into isolated, single-responsibility helper functions.
  • Streamlined Dispatcher (get_var_attrs): Simplified the entry point to a lightweight lookup that uses inspect.signature to dynamically resolve parameter requirements for different candidate providers.

…ve token registry

Signed-off-by: bhagathkrishnacdac <bhagath.krishna@cdac.in>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_REGISTRY mapping tokens to (type, description, provider) to eliminate the large if/elif chain.
  • 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.

Comment thread bessctl/commands.py
Comment on lines +300 to +301
import inspect
sig = inspect.signature(provider)
Comment thread bessctl/commands.py
Comment on lines +277 to +280
'[TCPDUMP_OPTS...]': ('opts', 'tcpdump(1) command-line options', []),
'[TSHARK_OPTS...]': ('opts', 'tshark(1) command-line options', []),
'[GRAPHEASY_OPTS...]': ('opts', 'graph-easy(1p) command-line options', []),
'[BESSD_OPTS...]': ('opts', 'bess daemon command-line options', []),
Comment thread bessctl/commands.py
Comment on lines +200 to +204
try:
return processor(func())
except (AttributeError, Exception):
# We ignore errors here as this is only for CLI auto-completion
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants