clap-action-command 0.1.0

A command-map pattern layered on Clap subcommands
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! A command-map pattern for easily configuring and dispatching modular
//! subcommands using the Clap argument parser.
//!
//! The first part is the [`ActionCommand`] trait, which is implemented
//! to associate a behavior with a [`clap::Command`].
//!
//! ```rust
//! # use clap::{
//! #     Arg, ArgMatches, Command,
//! #     builder::NonEmptyStringValueParser,
//! # };
//! # use vec1::Vec1;
//! # use clap_action_command::{ActionCommand, get_one};
//! static NAME_ARG: &str = "name";
//!
//! struct HelloWorldCommand {}
//!
//! impl ActionCommand for HelloWorldCommand {
//!     // This specifies the name of the ActionCommand, so hello-world
//!     // here would be invokable as my-program hello-world if it were
//!     // to be run from a binary called my-program.
//!     fn name(&self) -> &'static str {
//!         "hello-world"
//!     }
//!
//!     // This is the command builder. The command itself is configured
//!     // with the name given previously, and here it may be configured
//!     // with additional args or aliases.
//!     fn command(&self, command: Command) -> Command {
//!         command
//!             .about("Say hello to the world")
//!             .alias("h")
//!             .arg(
//!                 Arg::new(NAME_ARG)
//!                     .short('n')
//!                     .value_name("NAME")
//!                     .required(false)
//!                     .value_parser(NonEmptyStringValueParser::new())
//!             )
//!     }
//!
//!     // The action to take when the ActionCommand is matched, given
//!     // a list of all (sub)commands for argument/flag retrieval.
//!     fn action(
//!         &self, matches: Vec1<&ArgMatches>
//!     ) -> Result<(), Box<dyn std::error::Error>> {
//!         if let Some(name_arg) =
//!                 get_one::<String>(&matches, NAME_ARG) {
//!             println!("Hello, {}!", name_arg);
//!         } else {
//!             println!("Hello, world!");
//!         }
//!
//!         Ok(())
//!     }
//! }
//! ```
//!
//! The second part is the [`CommandMap`] type, which aggregates one or
//! more [`ActionCommand`]s and handles dispatching to them based on
//! a [`clap::ArgMatches`].
//!
//! ```rust
//! # use clap::{ArgMatches, Command, command};
//! # use vec1::vec1;
//! # use clap_action_command::{
//! #     ActionCommand, CommandMap, HelloWorldCommand
//! # };
//! #
//! // Add commands to the map by pushing ActionCommand trait objects
//! // onto the map
//! let command_map = CommandMap::builder()
//!     .push(HelloWorldCommand {})
//!     .build();
//! // Tell the relevant Clap Command about all the subcommands
//! let command = Command::new("my-program")
//!     .subcommands(command_map.commands());
//! let matches = command.get_matches_from(
//!     ["my-program", "hello-world"]
//! );
//! // Dispatch to the relevant subcommand, saving some if/else-if
//! // chains
//! command_map.dispatch(vec1![&matches]);
//! ```

pub use vec1;

use clap::{
    parser::{RawValues, ValueSource, ValuesRef},
    ArgMatches, Command,
};
use snafu::{ResultExt, Snafu};
use std::{any::Any, collections::HashMap, error::Error, iter::Flatten, vec::IntoIter};
use vec1::Vec1;

/// A type that encapsulates the Clap [`Command`] and associated
/// behavior of that command when it is matched.
///
/// ```rust
/// # use clap::{ArgMatches, Command};
/// # use clap_action_command::ActionCommand;
/// # use vec1::Vec1;
/// #
/// struct HelloWorldCommand {}
///
/// impl ActionCommand for HelloWorldCommand {
///     fn name(&self) -> &'static str {
///         "hello-world"
///     }
///
///     fn command(&self, command: Command) -> Command {
///         command
///             .about("Say hello to the world")
///             .alias("h")
///     }
///
///     fn action(
///         &self, _matches: Vec1<&ArgMatches>,
///     ) -> Result<(), Box<dyn std::error::Error>> {
///         println!("Hello, world!");
///         Ok(())
///     }
/// }
/// ```
pub trait ActionCommand<T = (), E = Box<dyn Error>> {
    /// The name of the command.
    fn name(&self) -> &'static str;

    /// The [`Command`] that describes how to match this on the command
    /// line using Clap. `command` is already constructed using
    /// [`Self::name`] for convenience.
    fn command(&self, command: Command) -> Command;

    /// The action to take when this command is matched on the command
    /// line. [`CommandMap`]s may be nested, and this is represented
    /// by the matches being returned as a list of at least one element.
    fn action(&self, matches: Vec1<&ArgMatches>) -> Result<T, E>;
}

/// A type which has a set of [`ActionCommand`]s and can provide the
/// Clap [`Command`] for command line arg parsing, as well as map a
/// matched [`Command`] back to its [`ActionCommand`] for dispatch to
/// its action function.
///
/// ```rust
/// # use clap::{ArgMatches, Command, command};
/// # use vec1::vec1;
/// # use clap_action_command::{
/// #     ActionCommand, CommandMap, HelloWorldCommand
/// # };
/// #
/// // Add commands to the map by pushing ActionCommand trait objects
/// // onto the map
/// let command_map = CommandMap::builder()
///     .push(HelloWorldCommand {})
///     .build();
/// // Tell the relevant Clap Command about all the subcommands
/// let command = Command::new("my-program")
///     .subcommands(command_map.commands());
/// let matches = command.get_matches_from(
///     ["my-program", "hello-world"]
/// );
/// // Dispatch to the relevant subcommand, saving some if/else-if
/// // chains
/// command_map.dispatch(vec1![&matches]);
/// ```
///
/// This type can be composed, for example on a subcommand with multiple
/// subcommands of its own. See [`CommandMapActionCommand`] for a
/// minimal example.
pub struct CommandMap<'a, T = (), E = Box<dyn Error>> {
    command_map: HashMap<&'static str, Box<dyn ActionCommand<T, E> + Send + Sync + 'a>>,
}

impl<'a> CommandMap<'a> {
    /// Creates a builder type which is used to tell the [`CommandMap`]
    /// about the [`ActionCommand`]s it will be mapping over.
    pub fn builder() -> CommandMapBuilder<'a> {
        CommandMapBuilder {
            command_map: HashMap::new(),
        }
    }

    /// The Clap [`Command`]s for this [`CommandMap`]. Use this with
    /// [`clap::Command::subcommands`] to configure it to use this
    /// [`CommandMap`].
    pub fn commands(&self) -> Vec<Command> {
        self.command_map
            .values()
            .map(|v| v.command(Command::new(v.name())))
            .collect()
    }

    /// Dispatch this [`CommandMap`] using [`ArgMatches`].
    ///
    /// When starting from scratch simply construct a new
    /// [`vec1::Vec1`] with a single [`clap::ArgMatches`] in it; when
    /// nesting multiple [`CommandMap`]s it is helpful to keep the
    /// previous subcommand stack accessible by extending the matches
    /// vector using [`vec1::Vec1::from_vec_push`].
    pub fn dispatch(&self, matches: Vec1<&ArgMatches>) -> Result<(), DispatchError> {
        let local_matches = matches.last();
        if let Some((command_name, subcommand)) = local_matches.subcommand() {
            if let Some(action_command) = self.command_map.get(command_name) {
                action_command
                    .action(Vec1::from_vec_push(matches.to_vec(), subcommand))
                    .with_context(|_| ActionCommandSnafu {
                        command_name: command_name.to_owned(),
                    })?;

                return Ok(());
            }

            return Err(DispatchError::SubcommandNotInMap {
                command_name: command_name.to_owned(),
                all_commands: self
                    .command_map
                    .values()
                    .map(|action_command| action_command.name())
                    .collect(),
            });
        }

        Err(DispatchError::NoSubcommand)
    }
}

/// Error generated by [`CommandMap::dispatch`].
#[derive(Debug, Snafu)]
pub enum DispatchError {
    /// An error originating from the execution of the [`ActionCommand`]
    /// itself - an error in the business logic.
    ActionCommand {
        command_name: String,
        source: Box<dyn std::error::Error>,
    },

    /// The [`CommandMap`] does not have an associated [`ActionCommand`]
    /// named the same thing as the [`clap::ArgMatches`] matched
    /// command. This may happen if additional [`clap::Command`]s have
    /// been added beyond those present in the [`CommandMap`].
    SubcommandNotInMap {
        command_name: String,
        all_commands: Vec<&'static str>,
    },

    /// The [`clap::ArgMatches`] does not have a subcommand, which means
    /// that the [`clap::Command`] which matched this
    /// [`clap::ArgMatches`] is the most specific. For example, if
    /// `my-program` has a `hello-world` subcommand, but the
    /// [`CommandMap`] returns [`DispatchError::NoSubcommand`], it means
    /// that the program was invoked as `my-program` with no subcommand
    /// at all.
    NoSubcommand,
}

/// Used to fluently construct [`CommandMap`]s.
pub struct CommandMapBuilder<'a> {
    command_map: HashMap<&'static str, Box<dyn ActionCommand + Send + Sync + 'a>>,
}

impl<'a> CommandMapBuilder<'a> {
    /// Add a new [`ActionCommand`] to the [`CommandMap`].
    pub fn push(
        mut self,
        action_command: impl ActionCommand + Send + Sync + 'a,
    ) -> CommandMapBuilder<'a> {
        self.command_map
            .insert(action_command.name(), Box::new(action_command));

        self
    }

    /// Add zero or more [`ActionCommand`]s to the [`CommandMap`].
    pub fn push_all(
        mut self,
        action_commands: impl IntoIterator<Item = impl ActionCommand + Send + Sync + 'a>,
    ) -> CommandMapBuilder<'a> {
        for action_command in action_commands {
            self.command_map
                .insert(action_command.name(), Box::new(action_command));
        }

        self
    }

    /// Finalize this builder and generate a [`CommandMap`].
    pub fn build(self) -> CommandMap<'a> {
        CommandMap {
            command_map: self.command_map,
        }
    }
}

/// An [`ActionCommand`] that only composes a [`CommandMap`]'s
/// subcommands. Any customization of the [`clap::Command`] or the
/// behavior of [`CommandMapActionCommand::action`] will require a
/// custom type.
///
/// ```rust
/// # use clap_action_command::{
/// #     CommandMap, CommandMapActionCommand, HelloWorldCommand
/// # };
/// // create a new CommandMapActionCommand, including all of the
/// // subcommands it must dispatch to.
/// let foo = CommandMapActionCommand::new(
///     "foo",
///     CommandMap::builder()
///         .push(HelloWorldCommand {})
///         .build(),
/// );
/// // add that CommandMapActionCommand to its parent CommandMap, which
/// // will automatically dispatch and route to subcommands
/// let command_map = CommandMap::builder()
///     .push(foo)
///     .build();
/// ```
pub struct CommandMapActionCommand<'a> {
    name: &'static str,
    command_map: CommandMap<'a>,
}

impl<'a> CommandMapActionCommand<'a> {
    /// Create a new [`CommandMapActionCommand`] give a name and a
    /// [`CommandMap`].
    pub fn new(name: &'static str, command_map: CommandMap<'a>) -> CommandMapActionCommand<'a> {
        CommandMapActionCommand { name, command_map }
    }
}

impl<'a> ActionCommand for CommandMapActionCommand<'a> {
    fn name(&self) -> &'static str {
        self.name
    }

    fn command(&self, command: clap::Command) -> clap::Command {
        command.subcommands(self.command_map.commands())
    }

    fn action(&self, matches: Vec1<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
        match self.command_map.dispatch(matches) {
            Ok(()) => Ok(()),
            Err(e) => Err(Box::new(e)),
        }
    }
}

/// Helper function for dealing with chains of [`ArgMatches`] while
/// working in [`ActionCommand::action`] to find arguments which may
/// have been spcified anywhere in the subcommand tree.
///
/// ```rust
/// # use clap_action_command::get_many;
/// # use clap::{Arg, Command};
/// #
/// # let command = Command::new("my-program")
/// #     .arg(Arg::new("my-arg").long("my-arg"))
/// #     .subcommand(
/// #           Command::new("my-subcommand")
/// #               .arg(Arg::new("my-arg").long("my-arg")));
/// # let matches = command.get_matches_from([
/// #     "my-program",
/// #     "--my-arg",
/// #     "alpha",
/// #     "my-subcommand",
/// #     "--my-arg",
/// #     "beta",
/// # ]);
/// # let matches = vec![&matches, matches.subcommand().unwrap().1];
/// #
/// // my-program --my-arg alpha my-subcommand --my-arg beta
/// let arg = get_many::<String>(&matches, "my-arg");
/// assert_eq!(vec!["alpha", "beta"], arg.collect::<Vec<_>>());
/// ```
pub fn get_many<'a, T: Any + Clone + Send + Sync + 'static>(
    matches: &[&'a ArgMatches],
    id: &str,
) -> Flatten<IntoIter<ValuesRef<'a, T>>> {
    let mut collected_values = vec![];

    for matches in matches.iter() {
        if let Ok(Some(values)) = matches.try_get_many(id) {
            collected_values.push(values);
        }
    }

    collected_values.into_iter().flatten()
}

/// Helper function for dealing with chains of [`ArgMatches`] while
/// working in [`ActionCommand::action`] to find arguments which may
/// have been specified anywhere in the subcommand tree.
///
/// ```rust
/// # use clap_action_command::get_one;
/// # use clap::{Arg, Command};
/// #
/// # let command = Command::new("my-program")
/// #     .arg(Arg::new("my-arg").long("my-arg"))
/// #     .subcommand(
/// #           Command::new("my-subcommand")
/// #               .arg(Arg::new("my-arg").long("my-arg")));
/// # let matches = command.get_matches_from([
/// #     "my-program",
/// #     "--my-arg",
/// #     "alpha",
/// #     "my-subcommand",
/// #     "--my-arg",
/// #     "beta",
/// # ]);
/// # let matches = vec![&matches, matches.subcommand().unwrap().1];
/// #
/// // my-program --my-arg alpha my-subcommand --my-arg beta
/// let arg = get_one::<String>(&matches, "my-arg");
/// assert_eq!("beta", arg.unwrap());
/// ```
///
/// This function respects the provenance
/// ([`clap::parser::ValueSource`]) of arguments. For example, a default
/// or environment-sourced value will never override a value specified
/// explicitly on the command line.
pub fn get_one<'a, T: Any + Clone + Send + Sync + 'static>(
    matches: &[&'a ArgMatches],
    id: &str,
) -> Option<&'a T> {
    let mut best_match = None;
    let mut best_match_specificity = ValueSource::DefaultValue;

    for matches in matches.iter() {
        let current_match = match matches.try_get_one::<T>(id) {
            Ok(arg_match) => arg_match,
            Err(_) => continue,
        };
        let current_specificity = matches.value_source(id);

        if let Some(current_specificity) = current_specificity {
            if best_match_specificity <= current_specificity {
                best_match = current_match;
                best_match_specificity = current_specificity;
            }
        }
    }

    best_match
}

/// Helper function for dealing with chains of [`ArgMatches`] while
/// working in [`ActionCommand::action`] to find arguments which may
/// have been specified anywhere in the subcommand tree.
///
/// ```rust
/// # use clap_action_command::get_raw;
/// # use clap::{Arg, Command};
/// # use std::ffi::OsStr;
/// #
/// # let command = Command::new("my-program")
/// #     .arg(Arg::new("my-arg").long("my-arg"))
/// #     .subcommand(
/// #           Command::new("my-subcommand")
/// #               .arg(Arg::new("my-arg").long("my-arg")));
/// # let matches = command.get_matches_from([
/// #     "my-program",
/// #     "--my-arg",
/// #     "alpha",
/// #     "my-subcommand",
/// #     "--my-arg",
/// #     "beta",
/// # ]);
/// # let matches = vec![&matches, matches.subcommand().unwrap().1];
/// #
/// // my-program --my-arg alpha my-subcommand --my-arg beta
/// let arg = get_raw(&matches, "my-arg");
/// assert_eq!(
///     vec![OsStr::new("alpha"), OsStr::new("beta")],
///     arg.collect::<Vec<_>>(),
/// );
/// ```
pub fn get_raw<'a>(matches: &[&'a ArgMatches], id: &str) -> Flatten<IntoIter<RawValues<'a>>> {
    let mut collected_values = vec![];

    for matches in matches.iter() {
        if let Ok(Some(values)) = matches.try_get_raw(id) {
            collected_values.push(values);
        }
    }

    collected_values.into_iter().flatten()
}

// just here to clean up some doctests
// TODO: can this be turned off in normal builds with eg a
//       #[cfg(doctest)]?
#[doc(hidden)]
pub struct HelloWorldCommand {}

impl ActionCommand for HelloWorldCommand {
    fn name(&self) -> &'static str {
        "hello-world"
    }

    fn command(&self, command: Command) -> Command {
        command.about("Say hello to the world").alias("h")
    }

    fn action(&self, _matches: Vec1<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
        println!("Hello, world!");
        Ok(())
    }
}

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
struct ReadmeDoctests {}

#[cfg(test)]
mod tests {
    use super::{get_one, ActionCommand, CommandMap, CommandMapActionCommand, DispatchError};
    use clap::{builder::NonEmptyStringValueParser, Arg, ArgMatches, Command};
    use std::ffi::OsString;
    use vec1::{vec1, Vec1};

    struct HelloWorldCommand {}

    impl ActionCommand for HelloWorldCommand {
        fn name(&self) -> &'static str {
            "hello-world"
        }

        fn command(&self, command: clap::Command) -> clap::Command {
            command.alias("h").arg(
                Arg::new("bar")
                    .short('b')
                    .value_parser(NonEmptyStringValueParser::new())
                    .required(true),
            )
        }

        fn action(&self, matches: Vec1<&ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
            println!(
                "Hello, World! My args are {{ foo: {}, bar: {} }}",
                matches.first().get_one::<String>("foo").unwrap(),
                matches.last().get_one::<String>("bar").unwrap(),
            );
            Ok(())
        }
    }

    fn example_dispatch(
        itr: impl IntoIterator<Item = impl Into<OsString> + Clone>,
    ) -> Result<(), DispatchError> {
        let base_command = Command::new("command_matching").arg(
            Arg::new("foo")
                .short('f')
                .value_parser(NonEmptyStringValueParser::new())
                .required(true),
        );
        let command_map_action_command = CommandMapActionCommand::new(
            "foo",
            CommandMap::builder().push(HelloWorldCommand {}).build(),
        );
        let command_map = CommandMap::builder()
            .push(command_map_action_command)
            .build();
        let base_command = base_command
            .subcommands(command_map.commands())
            .subcommand(Command::new("bar"));
        let matches = base_command.get_matches_from(itr);

        command_map.dispatch(vec1![&matches])
    }

    // --------------------------------------------------------------
    // these tests are too large for the CommandMap doctest, and they
    // generally focus on failure modes or subtleties not appropriate
    // for the brevity of docs
    // --------------------------------------------------------------

    #[test]
    fn alias_matching() {
        let r = example_dispatch([
            "command_matching",
            "-f",
            "my_foo",
            "foo",
            "h", // use an alias for a command rather than its full name
            "-b",
            "my_bar",
        ]);

        assert!(r.is_ok());
    }

    #[test]
    fn subcommand_not_in_map() {
        let r = example_dispatch(["command_matching", "-f", "my_foo", "bar"]);

        assert!(matches!(
            r,
            Err(DispatchError::SubcommandNotInMap {
                command_name: _,
                all_commands: _,
            })
        ));
    }

    #[test]
    fn no_subcommand() {
        let r = example_dispatch(["command_matching", "-f", "my_foo"]);

        assert!(matches!(r, Err(DispatchError::NoSubcommand)));
    }

    #[test]
    fn get_one_picks_most_specific() {
        let command = Command::new("my-program")
            .arg(Arg::new("my-arg").long("my-arg").default_value("gamma"))
            .subcommand(Command::new("my-subcommand").arg(Arg::new("my-arg").long("my-arg")));
        let matches = command.get_matches_from([
            "my-program",
            "--my-arg",
            "alpha",
            "my-subcommand",
            "--my-arg",
            "beta",
        ]);
        let command_matches = vec![&matches, matches.subcommand().unwrap().1];
        let arg = get_one::<String>(&command_matches, "my-arg");

        assert_eq!("beta", arg.unwrap());
    }

    #[test]
    fn get_one_ignores_defaults() {
        let command = Command::new("my-program")
            .arg(Arg::new("my-arg").long("my-arg").default_value("gamma"))
            .subcommand(Command::new("my-subcommand").arg(Arg::new("my-arg").long("my-arg")));
        let matches = command.get_matches_from([
            "my-program",
            // --my-arg defaults to gamma
            "my-subcommand",
            "--my-arg",
            "beta",
        ]);
        let command_matches = vec![&matches, matches.subcommand().unwrap().1];
        let arg = get_one::<String>(&command_matches, "my-arg");

        assert_eq!("beta", arg.unwrap());
    }
}