Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • N nes
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Jobs
  • Commits
Collapse sidebar
  • Earth SciencesEarth Sciences
  • nes
  • Wiki
  • HowTo_Console_Line_Interface

HowTo_Console_Line_Interface · Changes

Page history
WIP authored Jul 05, 2025 by Carles Tena's avatar Carles Tena
Show whitespace changes
Inline Side-by-side
HowTo_Console_Line_Interface.md
View page @ bf7a5c47
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
To add a new command to the NES CLI, follow these steps: To add a new command to the NES CLI, follow these steps:
1. **Create the script with the desired functionality** ### 1. Create the script with the desired functionality
Create a new Python script in the `nes/entrypoints/` directory, for example, `my_function.py`. Create a new Python script in the `nes/entrypoints/` directory, for example, `my_function.py`.
This script should define the function that implements your command's logic. This script should define the function that implements your command's logic.
...@@ -19,7 +19,7 @@ To add a new command to the NES CLI, follow these steps: ...@@ -19,7 +19,7 @@ To add a new command to the NES CLI, follow these steps:
print("Executing my_function with arguments:", args) print("Executing my_function with arguments:", args)
``` ```
2. **Expose the function in `__init__.py`** ### 2. Expose the function in `__init__.py`
Import and expose your function in the `nes/entrypoints/__init__.py` file to make it available for CLI registration. Import and expose your function in the `nes/entrypoints/__init__.py` file to make it available for CLI registration.
...@@ -31,7 +31,7 @@ To add a new command to the NES CLI, follow these steps: ...@@ -31,7 +31,7 @@ To add a new command to the NES CLI, follow these steps:
__all__ = [..., 'my_function'] __all__ = [..., 'my_function']
``` ```
3. **Register the command in `cli.py`** ### 3. Register the command in `cli.py`
Add a helper function to register your command's subparser in `nes/entrypoints/cli.py`. Add a helper function to register your command's subparser in `nes/entrypoints/cli.py`.
This function should add the subparser and specify the handler function. This function should add the subparser and specify the handler function.
...@@ -39,16 +39,16 @@ To add a new command to the NES CLI, follow these steps: ...@@ -39,16 +39,16 @@ To add a new command to the NES CLI, follow these steps:
```python ```python
# nes/entrypoints/cli.py # nes/entrypoints/cli.py
def _add_my_function_subparser(subparsers):
from nes.entrypoints import my_function from nes.entrypoints import my_function
def _add_my_function(subparsers):
parser = subparsers.add_parser('my_function', help='Description of my_function command') parser = subparsers.add_parser('my_function', help='Description of my_function command')
# Add any command-line arguments here # Add any command-line arguments here
parser.add_argument('--option', type=str, help='An example option') parser.add_argument('--option', type=str, help='An example option')
parser.set_defaults(func=my_function) parser.set_defaults(func=my_function)
``` ```
4. **Call the subparser registration in `main()`** ### 4. Call the subparser registration in `main()`
Inside the `main()` function of `cli.py`, call `_add_my_function()` to register your command with the CLI. Inside the `main()` function of `cli.py`, call `_add_my_function()` to register your command with the CLI.
...@@ -56,24 +56,25 @@ To add a new command to the NES CLI, follow these steps: ...@@ -56,24 +56,25 @@ To add a new command to the NES CLI, follow these steps:
# nes/entrypoints/cli.py # nes/entrypoints/cli.py
def main(): def main():
import argparse ...
parser = argparse.ArgumentParser(description='NES CLI') # Add subcommands
subparsers = parser.add_subparsers(title='commands', dest='command') _add_check_subparser(subparsers)
_add_reorder_subparser(subparsers)
# Register your command here # Register your command here
_add_my_function(subparsers) _add_my_function_subparser(subparsers)
...
args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
else:
parser.print_help()
``` ```
**Usage Example:** ## Usage Example
After completing these steps, you can run your new command from the terminal: After completing these steps, you can run your new command from the terminal:
```bash
nes my_function --option example_value
```
or
```bash ```bash
python -m nes.entrypoints.cli my_function --option example_value python -m nes.entrypoints.cli my_function --option example_value
``` ```
......
Clone repository
  • Home
  • Tutorials
  • Trainings
  • Development
  • Contribute
  • FAQ