1. NuGet 包
导入CommandLineParser包
2. 定义Option类
public class Option
{
[Option('f', "file", Required = true, HelpText = "Excel文件路径")]
public string ExcelPath { get; set; }
}
使用Options类来封装命令行参数
3.输入参数
调试 -> 调试属性 ->命令行参数
如下图示:
3. 程序入口解析
using CommandLine;
static void Main(string[] args)
{
commandLine.Parser.Default.ParseArguments<Option>(args)
.WithParsed(option =>
{
var excelPath = option.ExcelPath;
//使用解析后的命令行参数操作
var excelDataDictionary = ExcelService.ReadExcel(excelPath);
}
.WithNotParsed(errorList =>
{
});
}