Skip to main content

HelpFormatter

Trait HelpFormatter 

Source
pub trait HelpFormatter {
    // Required methods
    fn format_app(&self, config: &CommandsConfig) -> String;
    fn format_command(&self, config: &CommandsConfig, command: &str) -> String;

    // Provided method
    fn format_directives(&self, directives: &[DirectiveDefinition]) -> String { ... }
}
Expand description

Generates help text from a runtime configuration.

format_app and format_command receive the full CommandsConfig so implementations have access to metadata, commands, and global options. format_directives receives the REPL directive table and has a default implementation, so a formatter only needs the first two methods.

§Object safety

This trait has no generic methods and no Self return types, so it is fully dyn-compatible. It can be used as Box<dyn HelpFormatter>.

§Example

use dynamic_cli::help::{HelpFormatter, DefaultHelpFormatter};
use dynamic_cli::config::schema::{CommandsConfig, Metadata};

let config = CommandsConfig {
    metadata: Metadata {
        version: "1.0.0".to_string(),
        prompt: "myapp".to_string(),
        prompt_suffix: " > ".to_string(),
    },
    commands: vec![],
    global_options: vec![],
    directives: vec![],
};

let formatter = DefaultHelpFormatter::new();
let help = formatter.format_app(&config);
assert!(help.contains("myapp"));
assert!(help.contains("1.0.0"));

Required Methods§

Source

fn format_app(&self, config: &CommandsConfig) -> String

Generate help text for the whole application.

Lists all commands with their descriptions, and prints usage.

Source

fn format_command(&self, config: &CommandsConfig, command: &str) -> String

Generate help text for a single command.

Looks up command by name or alias and prints its arguments, options, and aliases. If the command is not found, returns an informative error string (never panics).

Provided Methods§

Source

fn format_directives(&self, directives: &[DirectiveDefinition]) -> String

Generate the list of REPL directives.

directives is the effective table, with the names, aliases and descriptions set by the directives: configuration section. The REPL shows the result after the application help.

Names and aliases are stored without their leading :; the output is expected to show it, since that is what the user types.

The default implementation renders plain text, without colours:

DIRECTIVES:
    :help [command]      Show the application help, or the help of a command [aliases: :h, :?]
    :load <path>         Run every line of a script file
    :quit                Save the session history and leave [aliases: :q]
    :exit                Leave without saving the session history

It returns an empty string for an empty table.

§Example
use dynamic_cli::config::{effective_directives, DirectiveDefinition, ReplDirective};
use dynamic_cli::config::schema::CommandsConfig;
use dynamic_cli::help::HelpFormatter;

struct MyFormatter;

impl HelpFormatter for MyFormatter {
    fn format_app(&self, _config: &CommandsConfig) -> String {
        String::new()
    }
    fn format_command(&self, _config: &CommandsConfig, command: &str) -> String {
        command.to_string()
    }
}

let overrides = vec![DirectiveDefinition {
    implementation: ReplDirective::Help,
    name: "aide".to_string(),
    aliases: vec![],
    description: "Afficher l'aide".to_string(),
}];
let list = MyFormatter.format_directives(&effective_directives(&overrides));
assert!(list.contains(":aide [command]"));
assert!(list.contains("Afficher l'aide"));

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§