This library offers two wrapper types for clap
Arg
s that help
for cases where values may be passed in via stdin
. When an Arg
value is to be read
from stdin
, the user will pass the commonly used stdin
alias: -
MaybeStdin
: Used when a value can be passed in via args OR stdin
FileOrStdin
: Used when a value can be read in from a file OR stdin
MaybeStdin
Example usage with clap
's derive
feature for a positional argument:
```rust,no_run
use clap::Parser;
use clap_stdin::MaybeStdin;
struct Args {
value: MaybeStdin
let args = Args::parse(); println!("value={}", args.value); ```
Calling this CLI: ```sh
$ echo "testing" | cargo run -- - value=testing ```
[MaybeStdin
] can wrap any time that matches the trait bounds for Arg
: FromStr
and Clone
```rust
use std::path::PathBuf;
use clap::Parser;
use clap_stdin::MaybeStdin;
struct Args {
path: MaybeStdin
sh
$ pwd | ./example -
FileOrStdin
Example usage with clap
's derive
feature for a positional argument:
```rust,no_run
use clap::Parser;
use clap_stdin::FileOrStdin;
struct Args { contents: FileOrStdin, }
let args = Args::parse(); println!("contents={}", args.contents); ```
Calling this CLI: ```sh
$ echo "testing" | cargo run -- - contents=testing
$ echo "testing" > contents.txt $ cargo run -- contents.txt contents=testing ```
[FileOrStdin
] can wrap any time that matches the trait bounds for Arg
: FromStr
and Clone
```rust
use std::path::PathBuf;
use clap::Parser;
use clap_stdin::FileOrStdin;
struct Args {
path: FileOrStdin
```sh
$ wc ~/myfile.txt -l | ./example -
$ cat myfile.txt 42 $ .example myfile.txt ```
MaybeStdin
or FileOrStdin
multiple timesBoth [MaybeStdin
] and [FileOrStdin
] will check at runtime if stdin
is being read from multiple times. You can use this
as a feature if you have mutually exclusive args that should both be able to read from stdin, but know
that the user will receive an error if 2+ MaybeStdin
args receive the "-" value.
For example, this compiles: ```rust use clap_stdin::{FileOrStdin, MaybeStdin};
struct Args {
first: FileOrStdin,
second: MaybeStdin
and it will work fine if the stdin alias -
is only passed for one of the arguments:
sh
$ echo "2" | ./example FIRST -
But if stdin
is attempted to be used for both arguments, there will be no value for the second
arg
sh
$ echo "2" | ./example - -
error: invalid value '-' for '<SECOND>': stdin argument used more than once
clap-stdin
is both MIT and Apache License, Version 2.0 licensed, as found
in the LICENSE-MIT and LICENSE-APACHE files.