#Modular structure commands using rust

5 messages · Page 1 of 1 (latest)

dusty dome
#

Extremely new to rust (but avid c# nerd). I'm implementing a console based application to control things on a little API based game. Here is my main.rs:

mod api;
mod modules;

use modules::*;
use std::io;
use std::io::{stdin, Write};
use std::process::exit;

fn main() {
    loop {
        let command = get_input();

        println!();
        do_command(command);
        println!();
    }
}

fn get_input() -> String {
    let mut input = String::new();
    print!("Enter command: ");
    io::stdout().flush().unwrap();
    stdin().read_line(&mut input).unwrap();

    input
}

fn do_command(command: String) {
    match command.as_str().trim() {
        "agent" => agent::print(),
        "agent all" => agent::print_all(),
        "agent count" => agent::print_count(),
        "ships" | "fleet" => fleet::print(),
        "exit" => {
            println!("Goodbye!");
            exit(0)
        },
        _ => println!("Command not found!")
    }
}

However, I'm finding this is extremely tightly coupled. I need to modify my main.rs every time the modules change. I also would like to attach descriptions to modules so I can dynamically generate a help command. As you can maybe see in the code above a module can have aliases and can have subcommands.

I know I can do this in c# using reflection and attributes. But I'm unsure how to implement this in rust and unfortunately google hasn't given me much, can anyone give me some pointers?

sour crown
#

sounds like you want to integrate clap to handle all the parsing and help docs etc

#

?crate clap

left galleonBOT
#

A simple to use, efficient, and full-featured Command Line Argument Parser

Version

4.5.0

Downloads

194 341 393

dusty dome