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..9131ae8 100644 --- a/src/DocFxOpenApi/Domain/CommandlineOptions.cs +++ b/src/DocFxOpenApi/Domain/CommandlineOptions.cs @@ -3,29 +3,61 @@ 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. + /// 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. + /// Gets or sets the output folder. /// [Option('o', "outputfolder", Required = false, HelpText = "Folder to write the resulting specifications in.")] public string? OutputFolder { get; set; } /// - /// Gets or sets a value indicating whether verbose information is shown in the output. + /// Gets or sets a value indicating whether verbose information is shown in the output. /// [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