Skip to content

HowTo_Console_Line_Interface

Carles Tena edited this page Oct 6, 2026 · 1 revision

How to Add a Custom Command-Line Interface Command

Follow the steps below to add a new command to the NES CLI:

1. Create a Script for Your Command

Create a new Python script in the nes/cli/ directory, such as my_function.py. This script should define the function that implements the logic for your command.

# nes/cli/my_function.py

def my_function(args):
    """
    Implement the logic for your command here.
    Args:
        args: Parsed command-line arguments.
    """
    print("Executing my_function with arguments:", args)

2. Expose the Function in __init__.py

To make your function available for CLI registration, import and expose it in nes/cli/__init__.py.

# nes/cli/__init__.py

from .my_function import my_function

__all__ = [..., 'my_function']

3. Register the Command in cli.py

Add a helper function in nes/cli/cli.py to register your command's subparser. This function should add the subparser and specify the handler function.

# nes/cli/cli.py

def _add_my_function_subparser(subparsers):
    from nes.cli import my_function

    parser = subparsers.add_parser('my_function', help='Description of the my_function command')
    # Add any command-line arguments here
    parser.add_argument('--option', type=str, help='An example option')
    parser.set_defaults(func=my_function)

4. Register the Subparser in main()

Within the main() function in cli.py, call _add_my_function_subparser() to register your command with the CLI.

# nes/cli/cli.py

def main():
    ...
    # Add subcommands
    _add_check_subparser(subparsers)
    _add_reorder_subparser(subparsers)
    # Register your custom command here
    _add_my_function_subparser(subparsers)
    ...

5. Update Documentation

  1. Update the wiki User Guide with the new command and its link.
  2. Create a new wiki page for your command, following the format of existing pages.

6. Release

After testing and documenting your command, submit a merge request to the master branch.

Usage Example

After completing the steps above, you can run your new command from the terminal:

nes my_function --option example_value

or

python -m nes.cli.cli my_function --option example_value

This will execute the my_function command with the specified option.

Clone this wiki locally