diff --git a/src/DocFxOpenApi/DocFxOpenApi.cs b/src/DocFxOpenApi/DocFxOpenApi.cs index 3b6ab6d..465a085 100644 --- a/src/DocFxOpenApi/DocFxOpenApi.cs +++ b/src/DocFxOpenApi/DocFxOpenApi.cs @@ -75,13 +75,13 @@ private void RunLogic() _options.OutputFolder = _options.SpecFolder; } - _message.Verbose($"Specification folder: {_options.SpecFolder}"); + _message.Verbose($"Specification file/folder: {_options.SpecFolder ?? _options.SpecFile}"); _message.Verbose($"Output folder : {_options.OutputFolder}"); _message.Verbose($"Verbose : {_options.Verbose}"); - if (!Directory.Exists(_options.SpecFolder)) + if ((_options.SpecFolder ?? _options.SpecFile) == null) { - _message.Error($"ERROR: Specification folder '{_options.SpecFolder}' doesn't exist."); + _message.Error($"ERROR: Specification folder/file '{_options.SpecSource}' doesn't exist."); _returnvalue = 1; return; } @@ -93,9 +93,16 @@ private void RunLogic() private void ConvertOpenApiFiles() { - foreach (var extension in _openApiFileExtensions) + if (_options.SpecFolder != null) { - this.ConvertOpenApiFiles(extension); + foreach (var extension in _openApiFileExtensions) + { + this.ConvertOpenApiFiles(extension); + } + } + else + { + this.ConvertOpenApiFile(_options.SpecFile!); } } diff --git a/src/DocFxOpenApi/Domain/CommandlineOptions.cs b/src/DocFxOpenApi/Domain/CommandlineOptions.cs index 068322d..7d0c290 100644 --- a/src/DocFxOpenApi/Domain/CommandlineOptions.cs +++ b/src/DocFxOpenApi/Domain/CommandlineOptions.cs @@ -3,18 +3,23 @@ namespace DocFxOpenApi.Domain { + using System.IO; using CommandLine; /// - /// Class for command line options. + /// Class for command line options. /// public class CommandlineOptions { /// /// Gets or sets the folder with specifications. /// - [Option('s', "specfolder", Required = true, HelpText = "Folder containing the OpenAPI specification.")] - public string? SpecFolder { get; set; } + [Option('s', "specsource", Required = true, HelpText = "Folder or File containing the OpenAPI specification.")] + public string? SpecSource + { + get => SpecFolder ?? SpecFile; + set => SetSource(value); + } /// /// Gets or sets the output folder. @@ -27,5 +32,32 @@ public class CommandlineOptions /// [Option('v', "verbose", Required = false, HelpText = "Show verbose messages.")] public bool Verbose { get; set; } + + /// + /// Gets the folder with specifications, if the source is a folder. + /// + public string? SpecFolder { get; private set; } + + /// + /// Gets the file with specifications, if the source is a file. + /// + public string? SpecFile { get; private set; } + + private void SetSource(string? value) + { + if (value == null) + { + return; + } + + if (Directory.Exists(value)) + { + SpecFolder = value; + } + else if (File.Exists(value)) + { + SpecFile = value; + } + } } -} +} \ No newline at end of file diff --git a/src/DocFxOpenApi/README.md b/src/DocFxOpenApi/README.md index e180c1e..7ee3830 100644 --- a/src/DocFxOpenApi/README.md +++ b/src/DocFxOpenApi/README.md @@ -6,14 +6,15 @@ This tool converts existing [OpenAPI](https://www.openapis.org/) specification f ```text DocFxOpenApi -s [-o ] [-v] - -s, --specfolder Required. Folder containing the OpenAPI specification. + -s, --specsource Required. Folder or file containing the OpenAPI specification. -o, --outputfolder Folder to write the resulting specifications in. - -v, --verbose Show verbose messages. - --help Display this help screen. - --version Display version information. + -v, --verbose Show verbose messages. + --help Display this help screen. + --version Display version information. ``` -The tool converts any `*.json`, `*.yaml`, `*.yml` file from the provided specification folder into the output folder. It supports JSON or YAML-format, OpenAPI v2 or v3 (including 3.0.1) format files. +When a folder is provided to the `specsource` parameter, the tool converts all `*.json`, `*.yaml`, `*.yml` files in the folder and its subfolders. When a file is provided, the tool converts only that file. +It supports JSON or YAML-format, OpenAPI v2 or v3 (including 3.0.1) format files. If the `-o or --outputfolder` is not provided, the output folder is set to the input specs folder.