Created
July 25, 2023 17:41
-
-
Save baronfel/a8cc681437b12a2d1984c1637ec958f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.CommandLine; | |
using System.CommandLine.Builder; | |
using System.CommandLine.Parsing; | |
namespace scl | |
{ | |
public static class Usage | |
{ | |
public struct CmdArgs | |
{ | |
public int dpi; | |
public bool? grayscale; | |
public string? outputFile; | |
public string[]? inputFileList; | |
public CmdArgs() { | |
dpi = 0; | |
grayscale = false; | |
outputFile = ""; | |
inputFileList = new string[10]; | |
} | |
} | |
public static CmdArgs GetOpts(string[] args) | |
{ | |
CmdArgs cmdArgs = new CmdArgs(); | |
var dpiOption = new Option<int> (new[] {"--dpi"}, "Set image dots per inch."); | |
var grayscaleOption = new Option<bool?> (new[] { "--grayscale", "-g" }, "Convert Image to grayscale."); | |
var outputFileOption = new Option<string?> (new[] { "--output", "-o" }, description: "Output filename or basename when passing multiple output files, i.e. outputfile-1 outputfile-2 ...", getDefaultValue: () => { return ""; }); | |
// The line below produces the following build error: error CS1503: Argument 1: cannot convert from 'string[]' to 'string?' | |
var inputFileListArg = new Argument<string[]> ("input_file", description: "One or more image filename(s) to work on: image_file1 image_file2 ..."); | |
var rootCommand = new RootCommand("SDSI Image Transform and Enhancement"); | |
rootCommand.Add(dpiOption); | |
rootCommand.Add(outputFileOption); | |
rootCommand.Add(inputFileListArg); | |
rootCommand.SetHandler( | |
(dpiOption, grayscaleOption, inputFileListArg, outputFileOption) => | |
{ | |
cmdArgs.dpi = dpiOption; | |
cmdArgs.grayscale = grayscaleOption; | |
cmdArgs.inputFileList = inputFileListArg; | |
if (outputFileOption != null) { | |
cmdArgs.outputFile = outputFileOption.ToString(); | |
} | |
Console.WriteLine("DEBUG:/Usage.cs:GetOpts(): DPI = {0}", cmdArgs.dpi); | |
Console.WriteLine("DEBUG:/Usage.cs:GetOpts(): OUTPUT_FILE = {0}", cmdArgs.outputFile); | |
Console.WriteLine("DEBUG:/Usage.cs:GetOpts(): INPUT_FILE = {0}", cmdArgs.inputFileList); | |
}, | |
dpiOption, grayscaleOption, inputFileListArg, outputFileOption | |
); | |
rootCommand.Invoke(args); | |
return cmdArgs; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment