Skip to content

Commit ea548ed

Browse files
committed
feat(cli): added command to generate shell completion scripts
1 parent a48e5ac commit ea548ed

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ base64 = "0.22.1"
254254
calamine = { version = "0.31" }
255255
chrono = { version = "0.4", features = ["serde"] }
256256
clap = { version = "4.5", features = ["derive"] }
257+
clap_complete_command.version = "0.6.1"
257258
clientele = { version = "0.3.8", default-features = false, features = ["dotenv", "subcommands", "std"] }
258259
crossterm = "0.27"
259260
colored = "3"

rudof_cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ name = "rudof"
1616
[dependencies]
1717
anyhow.workspace = true
1818
clap.workspace = true
19+
clap_complete_command.workspace = true
1920
clientele.workspace = true
2021
dctap.workspace = true
2122
iri_s.workspace = true

rudof_cli/src/cli/parser.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::cli::wrappers::{
88
SortByShaclValidationReportCli, SortByValidateCli, ValidationModeCli,
99
};
1010
use clap::{Args, Parser, Subcommand};
11+
use clap_complete_command::Shell;
1112
use rudof_lib::{InputSpec, IriS};
1213
use rudof_mcp::server::TransportType;
1314
use std::path::PathBuf;
@@ -19,6 +20,7 @@ use std::path::PathBuf;
1920
#[derive(Parser, Debug)]
2021
#[command(author, version, about)]
2122
#[command(
23+
name = "rudof",
2224
arg_required_else_help = true,
2325
long_about = None // Automatically uses the Doc Comment above
2426
)]
@@ -73,6 +75,8 @@ pub enum Command {
7375
Generate(GenerateArgs),
7476
/// Validate Property Graph data using PGSchema
7577
PgSchemaValidate(PgSchemaValidateArgs),
78+
/// Generates a shell completion script for the specified shell
79+
Completion(CompletionArgs),
7680
}
7781

7882
// ============================================================================
@@ -1372,3 +1376,13 @@ pub struct PgSchemaValidateArgs {
13721376
#[command(flatten)]
13731377
pub common: CommonArgsOutputForceOverWrite,
13741378
}
1379+
1380+
/// Arguments for the `completion` command
1381+
#[derive(Debug, Clone, Args)]
1382+
pub struct CompletionArgs {
1383+
#[clap(value_parser = clap::value_parser!(Shell))]
1384+
pub shell: Shell,
1385+
1386+
#[command(flatten)]
1387+
pub common: CommonArgsOutputForceOverWrite,
1388+
}

rudof_cli/src/commands/base.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::cli::parser::{Command as CliCommand, CommonArgs, CommonArgsAll, CommonArgsOutputForceOverWrite};
22
use crate::commands::{
3-
CompareCommand, ConvertCommand, DataCommand, DctapCommand, GenerateCommand, McpCommand, NodeCommand,
4-
PgSchemaValidateCommand, PgschemaCommand, QueryCommand, RdfConfigCommand, ServiceCommand, ShaclCommand,
5-
ShaclValidateCommand, ShapemapCommand, ShexCommand, ShexValidateCommand, ValidateCommand,
3+
CompareCommand, CompletionCommand, ConvertCommand, DataCommand, DctapCommand, GenerateCommand, McpCommand,
4+
NodeCommand, PgSchemaValidateCommand, PgschemaCommand, QueryCommand, RdfConfigCommand, ServiceCommand,
5+
ShaclCommand, ShaclValidateCommand, ShapemapCommand, ShexCommand, ShexValidateCommand, ValidateCommand,
66
};
77
use crate::output::{ColorSupport, get_writer};
88
use anyhow::Result;
@@ -118,6 +118,7 @@ impl CommandFactory {
118118
CliCommand::Query(args) => Ok(Box::new(QueryCommand::new(args))),
119119
CliCommand::Generate(args) => Ok(Box::new(GenerateCommand::new(args))),
120120
CliCommand::PgSchemaValidate(args) => Ok(Box::new(PgSchemaValidateCommand::new(args))),
121+
CliCommand::Completion(args) => Ok(Box::new(CompletionCommand::new(args))),
121122
}
122123
}
123124
}
@@ -213,5 +214,9 @@ fn extract_common(cmd: &CliCommand) -> CommonArgs {
213214
output: a.common.output.clone(),
214215
force_overwrite: a.common.force_overwrite,
215216
}),
217+
CliCommand::Completion(a) => CommonArgs::OutputForceOverWrite(CommonArgsOutputForceOverWrite {
218+
output: a.common.output.clone(),
219+
force_overwrite: a.common.force_overwrite,
220+
}),
216221
}
217222
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::cli::parser::{Cli, CompletionArgs};
2+
use crate::commands::CommandContext;
3+
use crate::commands::base::Command;
4+
use clap::CommandFactory;
5+
6+
/// Implementation of the `compare` command.
7+
///
8+
/// This struct holds the specific arguments parsed by `clap` and
9+
/// implements the [`Command`] trait to execute Compare command logic.
10+
pub struct CompletionCommand {
11+
/// Arguments specific to Completion command.
12+
args: CompletionArgs,
13+
}
14+
15+
impl CompletionCommand {
16+
pub fn new(args: CompletionArgs) -> Self {
17+
Self { args }
18+
}
19+
}
20+
21+
impl Command for CompletionCommand {
22+
/// Executes the Completion command logic.
23+
fn execute(&self, ctx: &mut CommandContext) -> anyhow::Result<()> {
24+
self.args.shell.generate(&mut Cli::command(), ctx.writer.as_mut());
25+
Ok(())
26+
}
27+
28+
/// Returns the unique identifier for this command.
29+
fn name(&self) -> &'static str {
30+
"completion"
31+
}
32+
}

rudof_cli/src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod base;
22
mod compare;
3+
mod completion;
34
mod convert;
45
mod data;
56
mod dctap;
@@ -20,6 +21,7 @@ mod validate;
2021

2122
pub use base::{CommandContext, CommandFactory};
2223
pub use compare::CompareCommand;
24+
pub use completion::CompletionCommand;
2325
pub use convert::ConvertCommand;
2426
pub use data::DataCommand;
2527
pub use dctap::DctapCommand;

0 commit comments

Comments
 (0)