Skip to content

Commit

Permalink
feat: improve handling of StandardIn (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Oct 20, 2024
1 parent 1c74d40 commit e931bb9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -171,17 +172,17 @@ In this example:

<sub>CMD:</sub>
```cmd
type data.json | didot -t template.hbs -p json
type data.json | didot --StdIn -t template.hbs -p json
```

<sub>PowerShell:</sub>
```powershell
Get-Content data.json data.json | didot -t template.hbs -p json
Get-Content data.json | didot --StdIn -t template.hbs -p json
```

<sub>Bash:</sub>
```bash
cat data.json | didot -t template.hbs -p json
cat data.json | didot --StdIn -t template.hbs -p json
```

In this example:
Expand Down
5 changes: 4 additions & 1 deletion src/Didot.Cli/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
12 changes: 9 additions & 3 deletions src/Didot.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e931bb9

Please sign in to comment.