##// END OF EJS Templates
rhg: simplify the subcommands macro...
Arseniy Alekseyev -
r53419:92c6c8ab default
parent child Browse files
Show More
@@ -536,39 +536,51 mod commands {
536 pub mod status;
536 pub mod status;
537 }
537 }
538
538
539 macro_rules! subcommands {
539 pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
540 ($( $command: ident )+) => {
541
540
542 fn add_subcommand_args(app: clap::Command) -> clap::Command {
541 struct SubCommand {
543 app
542 run: RunFn,
544 $(
543 args: clap::Command,
545 .subcommand(commands::$command::args())
544 name: String,
546 )+
547 }
545 }
548
546
549 pub type RunFn = fn(&CliInvocation) -> Result<(), CommandError>;
547 macro_rules! subcommand {
550
548 ($command: ident) => {
551 fn subcommand_run_fn(name: &str) -> Option<RunFn> {
549 SubCommand {
552 match name {
550 args: commands::$command::args(),
553 $(
551 run: commands::$command::run,
554 stringify!($command) => Some(commands::$command::run),
552 name: stringify!($command).to_string(),
555 )+
556 _ => None,
557 }
558 }
553 }
559 };
554 };
560 }
555 }
556 fn subcommands() -> Vec<SubCommand> {
557 vec![
558 subcommand!(cat),
559 subcommand!(debugdata),
560 subcommand!(debugrequirements),
561 subcommand!(debugignorerhg),
562 subcommand!(debugrhgsparse),
563 subcommand!(files),
564 subcommand!(root),
565 subcommand!(config),
566 subcommand!(status),
567 ]
568 }
561
569
562 subcommands! {
570 fn add_subcommand_args(mut app: clap::Command) -> clap::Command {
563 cat
571 for s in subcommands() {
564 debugdata
572 app = app.subcommand(s.args)
565 debugrequirements
573 }
566 debugignorerhg
574 app
567 debugrhgsparse
575 }
568 files
576
569 root
577 fn subcommand_run_fn(name: &str) -> Option<RunFn> {
570 config
578 for s in subcommands() {
571 status
579 if s.name == name {
580 return Some(s.run);
581 }
582 }
583 None
572 }
584 }
573
585
574 pub struct CliInvocation<'a> {
586 pub struct CliInvocation<'a> {
General Comments 0
You need to be logged in to leave comments. Login now