diff --git a/README.md b/README.md index d70bf04..37e3ff8 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,8 @@ The command to run Didot is simply `didot`. When executing it, you need to provi - `-t, --Template` (required): Specifies the path to the Scriban template file. - `-s, --Source`: Specifies the path to the source data file, which can be in YAML, JSON, or XML format. If this argument is not provided, the data will be read from the console input. In such cases, the `-p, --Parser` option becomes mandatory. -- `-p, --Parser`: Defines the parser to use when the source data is provided through the console. Accepted values are `yaml`, `json` or `xml`. This option is required only when the --Source argument is omitted. +- `-i, --StdIn`: Specifies that the input is coming from the console. This option is required only when the --Source argument is omitted. +- `-p, --Parser`: Defines the parser to use when the source data is provided through the console. Accepted values are `yaml`, `json` or `xml`. This option is required only when the `--Source` argument is omitted or if the extension of the source file is not recognized to determine the parser. - `-o, --Output`: Specifies the path to the output file where the generated content will be saved. If not provided, the output will be displayed directly in the console. #### Example: @@ -171,17 +172,17 @@ In this example: CMD: ```cmd -type data.json | didot -t template.hbs -p json +type data.json | didot --StdIn -t template.hbs -p json ``` PowerShell: ```powershell -Get-Content data.json data.json | didot -t template.hbs -p json +Get-Content data.json | didot --StdIn -t template.hbs -p json ``` Bash: ```bash -cat data.json | didot -t template.hbs -p json +cat data.json | didot --StdIn -t template.hbs -p json ``` In this example: diff --git a/src/Didot.Cli/Options.cs b/src/Didot.Cli/Options.cs index e609b64..bbae77d 100644 --- a/src/Didot.Cli/Options.cs +++ b/src/Didot.Cli/Options.cs @@ -11,9 +11,12 @@ public class Options [Option('t', "Template", Required = true, HelpText = "Path to the template file.")] public required string Template { get; set; } - [Option('s', "Source", Required = false, HelpText = "Path to the source file.")] + [Option('s', "Source", Required = false, HelpText = "Path to the source file.", SetName = "Input")] public string? Source { get; set; } + [Option('i', "StdIn", Required = false, HelpText = "Indicates that the input will come from stdin.", SetName = "Input")] + public bool StdIn { get; set; } + [Option('p', "Parser", Required = false, HelpText = "The parser to use when reading from StdIn.")] public string? Parser { get; set; } diff --git a/src/Didot.Cli/Program.cs b/src/Didot.Cli/Program.cs index 634d203..3fc0303 100644 --- a/src/Didot.Cli/Program.cs +++ b/src/Didot.Cli/Program.cs @@ -25,9 +25,15 @@ static void RunWithOptions(Options opts) return; } - var sourceExtension = string.IsNullOrEmpty(opts.Source) - ? $".{opts.Parser!.ToLowerInvariant()}" - : new FileInfo(opts.Source).Extension; + if (opts.StdIn && string.IsNullOrEmpty(opts.Parser)) + { + Console.WriteLine("Error: Missing input parser. When --StdIn is set, you must provide the --parser option."); + return; + } + + var sourceExtension = string.IsNullOrEmpty(opts.Parser) + ? new FileInfo(opts.Source!).Extension + : $".{opts.Parser!.ToLowerInvariant()}"; using var source = string.IsNullOrEmpty(opts.Source) ? copyInStream()