use clap::{Parser, Subcommand, ValueEnum};
use clap_repl::reedline::{DefaultPrompt, DefaultPromptSegment, FileBackedHistory};
use clap_repl::ClapEditor;
use reedline::{default_emacs_keybindings, Emacs, KeyModifiers, ReedlineEvent};
#[derive(Debug, Parser)]
enum SimpleCommand {
Command1,
Command2 {
#[arg(short, long)]
input: String,
},
}
#[derive(Debug, Parser)]
#[command(name = "")] enum ComplexCommand<N = ComplexCommand<SimpleCommand>>
where
N: Subcommand,
{
NoInput,
PositionalInput {
input: ValueEnumExample,
},
ShortInput {
#[arg(short)]
input: ValueEnumExample,
},
LongInput {
#[arg(long)]
input: ValueEnumExample,
},
NestedCommand {
#[command(subcommand)]
sub_command: Box<N>,
},
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum ValueEnumExample {
Case1,
Case2,
C,
CaseWithoutHelp,
}
fn main() {
let prompt = DefaultPrompt {
left_prompt: DefaultPromptSegment::Basic("prompt-toolkit".to_owned()),
..DefaultPrompt::default()
};
let mut keybindings = default_emacs_keybindings();
keybindings.add_binding(
KeyModifiers::NONE,
reedline::KeyCode::Tab,
ReedlineEvent::UntilFound(vec![
ReedlineEvent::Menu("completion_menu".to_string()),
ReedlineEvent::Enter,
]),
);
let rl = ClapEditor::<ComplexCommand>::builder()
.with_prompt(Box::new(prompt))
.with_edit_mode(Box::new(Emacs::new(keybindings)))
.with_editor_hook(|reed| {
reed.with_history(Box::new(
FileBackedHistory::with_file(10000, "/tmp/clap-repl-simple-example-history".into())
.unwrap(),
))
})
.build();
rl.repl(|command| {
dbg!(command);
});
}