clap/examples/derive_ref/interop_tests.md
Ed Page 42c943844c fix(help): Use Command in place of Subcommand
In switching to title case for help headings (#4123), it caused me to
look at "subcommand" in a fresh light.  I can't quite put my finger on
it but "Subcommand" looks a bit sloppy.  I also have recently been
surveying other CLIs and they just use "command" as well.

All of them are commands anyways, just some are children of others
(subcommands) while others are not (root or top-level commands, or just
command).  Context is good enough for clarifying subcommands from root
commands.

This is part of #4132
2022-08-31 08:53:10 -05:00

4.4 KiB

Following are tests for the interop examples in this directory.

Augment Args

$ interop_augment_args
Value of built: false
Value of derived via ArgMatches: false
Value of derived: DerivedArgs {
    derived: false,
}

$ interop_augment_args -b --derived
Value of built: true
Value of derived via ArgMatches: true
Value of derived: DerivedArgs {
    derived: true,
}

$ interop_augment_args -d --built
Value of built: true
Value of derived via ArgMatches: true
Value of derived: DerivedArgs {
    derived: true,
}

$ interop_augment_args --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

	If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`

Usage:
    interop_augment_args[EXE] [OPTIONS]

For more information try --help

Augment Subcommands

$ interop_augment_subcommands
? failed
error: A subcommand is required but one was not provided.
$ interop_augment_subcommands derived
Derived subcommands: Derived {
    derived_flag: false,
}

$ interop_augment_subcommands derived --derived-flag
Derived subcommands: Derived {
    derived_flag: true,
}

$ interop_augment_subcommands derived --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

	If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`

Usage:
    interop_augment_subcommands[EXE] derived [OPTIONS]

For more information try --help

$ interop_augment_subcommands unknown
? failed
error: Found argument 'unknown' which wasn't expected, or isn't valid in this context

Usage:
    interop_augment_subcommands[EXE] [COMMAND]

For more information try --help

Hand-Implemented Subcommand

$ interop_hand_subcommand
? failed
clap 

Usage:
    interop_hand_subcommand[EXE] [OPTIONS] <COMMAND>

Commands:
    add       
    remove    
    help      Print this message or the help of the given subcommand(s)

Options:
    -t, --top-level    
    -h, --help         Print help information

$ interop_hand_subcommand add
Cli {
    top_level: false,
    subcommand: Add(
        AddArgs {
            name: [],
        },
    ),
}

$ interop_hand_subcommand add a b c
Cli {
    top_level: false,
    subcommand: Add(
        AddArgs {
            name: [
                "a",
                "b",
                "c",
            ],
        },
    ),
}

$ interop_hand_subcommand add --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

	If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`

Usage:
    interop_hand_subcommand[EXE] add [NAME]...

For more information try --help

$ interop_hand_subcommand remove
Cli {
    top_level: false,
    subcommand: Remove(
        RemoveArgs {
            force: false,
            name: [],
        },
    ),
}

$ interop_hand_subcommand remove --force a b c
Cli {
    top_level: false,
    subcommand: Remove(
        RemoveArgs {
            force: true,
            name: [
                "a",
                "b",
                "c",
            ],
        },
    ),
}

$ interop_hand_subcommand unknown
? failed
error: Found argument 'unknown' which wasn't expected, or isn't valid in this context

Usage:
    interop_hand_subcommand[EXE] [OPTIONS] <COMMAND>

For more information try --help

Flatten Hand-Implemented Args

$ interop_flatten_hand_args
Cli {
    top_level: false,
    more_args: CliArgs {
        foo: false,
        bar: false,
        quuz: None,
    },
}

$ interop_flatten_hand_args -f --bar
Cli {
    top_level: false,
    more_args: CliArgs {
        foo: true,
        bar: true,
        quuz: None,
    },
}

$ interop_flatten_hand_args --quuz abc
Cli {
    top_level: false,
    more_args: CliArgs {
        foo: false,
        bar: false,
        quuz: Some(
            "abc",
        ),
    },
}

$ interop_flatten_hand_args --unknown
? failed
error: Found argument '--unknown' which wasn't expected, or isn't valid in this context

	If you tried to supply `--unknown` as a value rather than a flag, use `-- --unknown`

Usage:
    interop_flatten_hand_args[EXE] [OPTIONS]

For more information try --help