Created
January 2, 2020 14:04
-
-
Save 0x414c49/818c681e0ab7b5a5bb89350652c65568 to your computer and use it in GitHub Desktop.
Complete and working sample of CLI
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 McMaster.Extensions.CommandLineUtils; | |
using System; | |
using System.IO; | |
namespace CSVUtil | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var app = new CommandLineApplication<Program>(throwOnUnexpectedArg: false); | |
#region global-argumetns-options | |
var inputFileOption = app.Option<string>("-i|--input", "Input file to convert" | |
, CommandOptionType.SingleValue) | |
.IsRequired(); | |
var verboseOption = app.Option("-v|--verbose", "Display operation details" | |
, CommandOptionType.NoValue); | |
#endregion | |
#region convert-command | |
app.Command("convert", | |
(convert) => | |
{ | |
var outputOption = convert.Option("-o|--output", "Output File" | |
, CommandOptionType.SingleValue); | |
var firstRowOption = convert.Option("-f|--first-row-header", "First row as header" | |
, CommandOptionType.NoValue); | |
convert.HelpOption("-? | -h | --help"); | |
convert.OnExecute(() => | |
{ | |
if (verboseOption.HasValue()) | |
Console.WriteLine($"Init process file {inputFileOption.ParsedValue}."); | |
var inputFile = GetInputFile(inputFileOption); | |
var outputFile = outputOption.Value() | |
?? Path.GetFileNameWithoutExtension(inputFile); | |
if (verboseOption.HasValue() && firstRowOption.HasValue()) | |
Console.WriteLine("Find first row as header."); | |
if (verboseOption.HasValue()) | |
Console.WriteLine($"Convert to {outputFile}.xlsx"); | |
// do the conversation! | |
}); | |
} | |
); | |
#endregion convert-command | |
#region print-command | |
app.Command("print", (print) => | |
{ | |
var tailOptions = print.Option<bool>("-t|--tail", "Show n rows from the tail." | |
, CommandOptionType.NoValue); | |
var rowOption = print.Option<int>("-r|--rows", | |
"Number of rows to show", CommandOptionType.SingleValue); | |
print.HelpOption("-? | -h | --help"); | |
print.OnExecute(() => | |
{ | |
var inputFile = GetInputFile(inputFileOption); | |
// Zero = all rows | |
var rows = rowOption.HasValue() ? Math.Abs(rowOption.ParsedValue) : 0; | |
if (verboseOption.HasValue()) | |
Console.WriteLine(tailOptions.HasValue() && rows > 0 | |
? $"Print {rows} records from the tail." | |
: $"Print {rows} records from the top."); | |
// print file | |
Console.WriteLine("print."); | |
}); | |
}); | |
#endregion | |
app.HelpOption("-h|--help|-?"); | |
app.Execute(args); | |
} | |
private static string GetInputFile(CommandOption<string> inputOption) | |
{ | |
var inputFile = inputOption.ParsedValue; | |
if (!Path.IsPathFullyQualified(inputOption.ParsedValue)) | |
inputFile = Path.GetFullPath(inputOption.ParsedValue); | |
//if (!File.Exists(inputFile)) | |
// Console.WriteLine("File not found."); | |
return inputFile; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment