From ee80ee7405eaf326f270d0d7066a3201b2f026d6 Mon Sep 17 00:00:00 2001 From: Jeff Ithier Date: Sun, 28 Jan 2024 18:35:25 +0100 Subject: [PATCH] [#98] Define entrypoint cli using clap --- iceoryx2-cli/iox2/Cargo.toml | 2 ++ iceoryx2-cli/iox2/src/cli.rs | 24 ++++++++++++++++++++++++ iceoryx2-cli/iox2/src/commands.rs | 11 ++++++++++- iceoryx2-cli/iox2/src/main.rs | 10 ++++++---- 4 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 iceoryx2-cli/iox2/src/cli.rs diff --git a/iceoryx2-cli/iox2/Cargo.toml b/iceoryx2-cli/iox2/Cargo.toml index 6e8161eb1..7e630195c 100644 --- a/iceoryx2-cli/iox2/Cargo.toml +++ b/iceoryx2-cli/iox2/Cargo.toml @@ -13,3 +13,5 @@ version = { workspace = true } [dependencies] human-panic = "1.2.3" better-panic = "0.3.0" +clap = { version = "4.4.18", features = ["derive"] } +colored = "2.0" diff --git a/iceoryx2-cli/iox2/src/cli.rs b/iceoryx2-cli/iox2/src/cli.rs new file mode 100644 index 000000000..6de69fa84 --- /dev/null +++ b/iceoryx2-cli/iox2/src/cli.rs @@ -0,0 +1,24 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command( + name = "iox2", + about = "The command-line interface to iceoryx2", + long_about = None, + version = env!("CARGO_PKG_VERSION"), + disable_help_subcommand = true, + arg_required_else_help = true, + help_template = help_template(), +)] +pub struct Cli { + #[arg(short, long, help = "List all installed commands")] + pub list: bool, +} + +fn help_template() -> &'static str { + "\ + USAGE: iox2 [OPTIONS] \n\n\ + OPTIONS:\n{options}\n\n\ + COMMANDS:\n{subcommands}\n\ + \u{00A0}\u{00A0}... See all installed commands with --list" +} diff --git a/iceoryx2-cli/iox2/src/commands.rs b/iceoryx2-cli/iox2/src/commands.rs index 99749de9a..2106946b2 100644 --- a/iceoryx2-cli/iox2/src/commands.rs +++ b/iceoryx2-cli/iox2/src/commands.rs @@ -1,8 +1,17 @@ +use colored::*; use std::env; use std::fs; use std::path::PathBuf; -pub fn find() -> Vec { +pub fn list() { + println!("Installed Commands:"); + let installed_commands = find(); + for command in installed_commands { + println!(" {}", command.bold()); + } +} + +fn find() -> Vec { let mut commands = find_command_binaries_in_development_dirs(); if commands.is_empty() { commands = find_command_binaries_in_system_path(); diff --git a/iceoryx2-cli/iox2/src/main.rs b/iceoryx2-cli/iox2/src/main.rs index 49df03974..44cfcd1b2 100644 --- a/iceoryx2-cli/iox2/src/main.rs +++ b/iceoryx2-cli/iox2/src/main.rs @@ -4,8 +4,11 @@ use human_panic::setup_panic; #[cfg(debug_assertions)] extern crate better_panic; +mod cli; mod commands; +use clap::Parser; + fn main() { #[cfg(not(debug_assertions))] { @@ -20,10 +23,9 @@ fn main() { .install(); } - let commands = commands::find(); + let cli = cli::Cli::parse(); - println!("Available commands:"); - for command in commands { - println!("- {}", command); + if cli.list { + commands::list(); } }