diff --git a/README.md b/README.md index ac9f662..48f44c1 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ $ createnv | `--target` | File to write the result | `.env` | | `--source` | File to use as a sample | `.env.sample` | | `--chars-for-random-string` | Characters used to create random strings | All ASCII letters, numbers and a few extra characters (`!@#$%^&*(-_=+)`) | +| `--stdout` | Write to `stdout` instead of a file | | | `--overwrite` | Do not ask before overwriting files | | | `--use-default` | Do not ask for input on fields that have a default value | | diff --git a/src/main.rs b/src/main.rs index 6c17abf..cd26a4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,13 @@ fn main() -> Result<()> { .default_value(DEFAULT_ENV) .help("File to write the result"), ) + .arg( + clap::Arg::new("stdout") + .long("stdout") + .help("Writes to stdout instead of to a target file") + .action(ArgAction::SetTrue) + .conflicts_with("target"), + ) .arg( Arg::new("source") .long("source") @@ -80,9 +87,10 @@ fn main() -> Result<()> { ) .get_matches(); + let to_stdout = matches.get_one::("stdout").unwrap(); let target = matches.get_one::("target").unwrap(); let overwrite = matches.get_one::("overwrite").unwrap(); - if !overwrite && !should_write_to(target)? { + if !to_stdout && !overwrite && !should_write_to(target)? { exit(0); } @@ -95,7 +103,11 @@ fn main() -> Result<()> { let mut parser = Parser::new(source.as_str(), chars, use_default)?; parser.parse(&mut stdin().lock())?; - let mut output = File::create(target)?; - output.write_all(parser.to_string().as_bytes())?; + if *to_stdout { + println!("\n{}", parser); + } else { + let mut output = File::create(target)?; + output.write_all(parser.to_string().as_bytes())?; + } Ok(()) }