Skip to content

Commit 395d657

Browse files
committed
feat: provide empty implementations for Args and Subcommand
1 parent d045daa commit 395d657

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

clap_builder/src/derive.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use crate::builder::PossibleValue;
55
use crate::{ArgMatches, Command, Error};
6+
use std::convert::Infallible;
67

78
use std::ffi::OsString;
89

@@ -387,3 +388,66 @@ fn format_error<I: CommandFactory>(err: Error) -> Error {
387388
let mut cmd = I::command();
388389
err.format(&mut cmd)
389390
}
391+
392+
impl FromArgMatches for () {
393+
fn from_arg_matches(_matches: &ArgMatches) -> Result<Self, Error> {
394+
Ok(())
395+
}
396+
397+
fn update_from_arg_matches(&mut self, _matches: &ArgMatches) -> Result<(), Error> {
398+
Ok(())
399+
}
400+
}
401+
402+
impl Args for () {
403+
fn augment_args(cmd: Command) -> Command {
404+
cmd
405+
}
406+
407+
fn augment_args_for_update(cmd: Command) -> Command {
408+
cmd
409+
}
410+
}
411+
412+
impl Subcommand for () {
413+
fn augment_subcommands(cmd: Command) -> Command {
414+
cmd
415+
}
416+
417+
fn augment_subcommands_for_update(cmd: Command) -> Command {
418+
cmd
419+
}
420+
421+
fn has_subcommand(_name: &str) -> bool {
422+
false
423+
}
424+
}
425+
426+
impl FromArgMatches for Infallible {
427+
fn from_arg_matches(_matches: &ArgMatches) -> Result<Self, Error> {
428+
Err(Error::raw(
429+
crate::error::ErrorKind::MissingSubcommand,
430+
"a subcommand is required but one was not provided",
431+
))
432+
}
433+
434+
fn update_from_arg_matches(&mut self, _matches: &ArgMatches) -> Result<(), Error> {
435+
unreachable!(
436+
"there will never be an instance of Infallible and thus &mut self can never be called"
437+
);
438+
}
439+
}
440+
441+
impl Subcommand for Infallible {
442+
fn augment_subcommands(cmd: Command) -> Command {
443+
cmd
444+
}
445+
446+
fn augment_subcommands_for_update(cmd: Command) -> Command {
447+
cmd
448+
}
449+
450+
fn has_subcommand(_name: &str) -> bool {
451+
false
452+
}
453+
}

tests/derive_ui/enum_variant_not_args.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ error[E0277]: the trait bound `SubCmd: clap::Args` is not satisfied
44
3 | Sub(SubCmd),
55
| ^^^^^^ the trait `clap::Args` is not implemented for `SubCmd`
66
|
7-
= help: the trait `clap::Args` is implemented for `Box<T>`
7+
= help: the following other types implement trait `clap::Args`:
8+
()
9+
Box<T>

tests/derive_ui/flatten_enum_in_struct.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ error[E0277]: the trait bound `SubCmd: clap::Args` is not satisfied
55
| ^^^^^^ the trait `clap::Args` is not implemented for `SubCmd`
66
|
77
= help: the following other types implement trait `clap::Args`:
8+
()
89
Box<T>
910
Opt

tests/derive_ui/flatten_struct_in_enum.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ error[E0277]: the trait bound `SubCmd: Subcommand` is not satisfied
55
| ^^^^^^ the trait `Subcommand` is not implemented for `SubCmd`
66
|
77
= help: the following other types implement trait `Subcommand`:
8+
()
89
Box<T>
10+
Infallible
911
Opt

0 commit comments

Comments
 (0)