Skip to content

RAW output flag

We need to implement in each function which prints output, to handle the raw flag (-r/--raw) to print outputs in raw json format.

The work to done is to pass a variable raw into the command, get the raw value and pass it to the function. For instance:

@click.command(help='Get a list of experiments.')
@click.pass_context
def list(ctx):
    """
    Get a list of experiments.

    Retrieves a list of experiments from a specified URL and displays them in a formatted table.

    Args:
        None

    Returns:
        None
        
    Example:
        sdlctl experiment list
    """
    raw = ctx.obj.get('RAW', False)

    exp.get_all_experiments(raw)

then the function needs to handle it:

def get_all_experiments(self, raw: bool):
        """
            Retrieves all experiments from the server and displays them in a table.

            Returns:
                None
            """
        url = f"{self.endpoints.Experiments}"

        table = self.table.experiment_table()
        self.table.set_title(table, "Experiments")
        response = self.rest.get_call(url)

        if response and response.status_code == 200:
            experiments = response.json()
            if not raw:
                self._populate_experiments_table(table, experiments)
                self._print_table(table)
            else:
                print(json.dumps(experiments, indent=4))
        else:
            self.error_handlers.handle_error("Failed to get experiments.", response)