Skip to content
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

Implement arguments tab-completion in Controller CLI #47

Merged
merged 8 commits into from
Jun 19, 2020
242 changes: 242 additions & 0 deletions control_plane/controller/controller/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@

"""Implementation of a CLI for the SRv6 Controller"""

# This comment avoids the annoying warning "Too many lines in module"
# of pylint. Maybe we should split this module in the future.
#
# pylint: disable=too-many-lines

# General imports
import logging
import os
import readline
import sys
from argparse import ArgumentParser
from cmd import Cmd
Expand Down Expand Up @@ -59,6 +65,10 @@
DEFAULT_DEBUG = False


# Set line delimiters, required for the auto-completion feature
readline.set_completer_delims(' \t\n')


class CustomCmd(Cmd):
"""This class extends the python class Cmd and implements a handler
for CTRL+C and CTRL+D"""
Expand Down Expand Up @@ -189,6 +199,67 @@ def do_extract_and_load_on_arango(self, args):
)
return True

def complete_extract(self, text, line, start_idx, end_idx):
"""Auto-completion for extract command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return topo_cli.complete_topology_information_extraction_isis(
text, prev_text)

def complete_load_on_arango(self, text, line, start_idx, end_idx):
"""Auto-completion for load_on_arango command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return topo_cli.complete_load_topo_on_arango(text, prev_text)

def complete_extract_and_load_on_arango(self, text,
line, start_idx, end_idx):
"""Auto-completion for extract_and_load_on_arango command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return (topo_cli
.complete_extract_topo_from_isis_and_load_on_arango(
text, prev_text))

def help_extract(self):
"""Show help usage for extract command"""

Expand Down Expand Up @@ -271,6 +342,44 @@ def do_reset(self, args):
)
return True

def complete_set(self, text, line, start_idx, end_idx):
"""Auto-completion for set command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6pm_cli.complete_set_configuration(text, prev_text)

def complete_reset(self, text, line, start_idx, end_idx):
"""Auto-completion for reset command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6pm_cli.complete_reset_configuration(text, prev_text)

def help_set(self):
"""Show help usagte for set operation"""

Expand Down Expand Up @@ -386,6 +495,63 @@ def do_stop(self, args):
)
return True

def complete_start(self, text, line, start_idx, end_idx):
"""Auto-completion for start command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6pm_cli.complete_start_experiment(text, prev_text)

def complete_show(self, text, line, start_idx, end_idx):
"""Auto-completion for show command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6pm_cli.complete_get_experiment_results(text, prev_text)

def complete_stop(self, text, line, start_idx, end_idx):
"""Auto-completion for stop command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6pm_cli.complete_stop_experiment(text, prev_text)

def help_start(self):
"""Show help usage for start operation"""

Expand Down Expand Up @@ -548,6 +714,82 @@ def do_biditunnel(self, args):
)
return True

def complete_path(self, text, line, start_idx, end_idx):
"""Auto-completion for path command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6_cli.complete_srv6_path(text, prev_text)

def complete_behavior(self, text, line, start_idx, end_idx):
"""Auto-completion for behavior command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6_cli.complete_srv6_behavior(text, prev_text)

def complete_unitunnel(self, text, line, start_idx, end_idx):
"""Auto-completion for unitunnel command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6_cli.complete_srv6_unitunnel(text, prev_text)

def complete_biditunnel(self, text, line, start_idx, end_idx):
"""Auto-completion for biditunnel command"""

# pylint: disable=no-self-use, unused-argument

# Get the previous argument in the command
# Depending on the previous argument, it is possible to
# complete specific params, such as the paths
#
# Split args
args = line[:start_idx].split(' ')
# If this is not the first arg, get the previous one
prev_text = None
if len(args) > 1:
prev_text = args[-2] # [-2] because last element is always ''
# Call auto-completion function and return a list of
# possible arguments
return srv6_cli.complete_srv6_biditunnel(text, prev_text)

def help_path(self):
"""Show help usage for path command"""

Expand Down
Loading