1

My goal is to have my cli program accept a stream of files

eg. Directory example_data has the following files

  • a.txt
  • b.txt
  • c.txt

I would like my rust program to handle something like the following

cargo run < example_data/*.txt

Is this type of setup possible? What's an alternative to this? I don't want to have to pass the file names individually to the program. Any suggestions?

3
  • 1
    This isn't really a Rust question since the behavior is dictated by the shell. cargo run <a.txt will redirect the file contents into the program's stdin, but only one file, it doesn't work for globs (i.e. *s for multiple files). You can do cargo run *.txt which will send multiple arguments to the process. You can access those in Rust via std::env::args. Commented Sep 23, 2022 at 0:48
  • See std::io::stdin() Commented Sep 23, 2022 at 0:54
  • Note that if you just want all the files concatenated together, that's what cat is for. That is, whatever < example_data/*.txt does not work, because the wildcard is not expanded in that position, but in cat example_data/*.txt | whatever the wildcard gets expanded and cat combines the files in one stream that is passes to the standard input of whatever. Whatever might be cargo run, that is rather incidental. Commented Sep 23, 2022 at 8:03

1 Answer 1

2

If you can call your program with *.txt, the shell will pass multiple arguments to your program (depends on the shell). You can access program arguments in Rust via std::env::args:

> cargo run *.txt
fn main() {
    for arg in std::env::args().skip(1) {
        println!("arg: {}", arg);
    }
}
arg: a.txt
arg: b.txt
arg: c.txt

You can then process the file names as you wish.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.