Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Define entrypoint cli using clap
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Jan 28, 2024
1 parent 18764ef commit ee80ee7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions iceoryx2-cli/iox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 24 additions & 0 deletions iceoryx2-cli/iox2/src/cli.rs
Original file line number Diff line number Diff line change
@@ -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] <COMMAND>\n\n\
OPTIONS:\n{options}\n\n\
COMMANDS:\n{subcommands}\n\
\u{00A0}\u{00A0}... See all installed commands with --list"
}
11 changes: 10 additions & 1 deletion iceoryx2-cli/iox2/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use colored::*;
use std::env;
use std::fs;
use std::path::PathBuf;

pub fn find() -> Vec<String> {
pub fn list() {
println!("Installed Commands:");
let installed_commands = find();
for command in installed_commands {
println!(" {}", command.bold());
}
}

fn find() -> Vec<String> {
let mut commands = find_command_binaries_in_development_dirs();
if commands.is_empty() {
commands = find_command_binaries_in_system_path();
Expand Down
10 changes: 6 additions & 4 deletions iceoryx2-cli/iox2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
{
Expand All @@ -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();
}
}

0 comments on commit ee80ee7

Please sign in to comment.