使用 CommandLineParser 指定相同类型的多个输入
Using CommandLineParser to specify multiple inputs of the same type
我想使用 CommandLineParser 包构建一个实用程序,可以在命令行上指定任意数量的文件路径。根据我目前对这个包的理解,这样做的规范方法是:
/// <summary>
/// Enumerable list of files
/// </summary>
[Option('i', "input", HelpText = "Input file(s) to process", Separator =',')]
public IEnumerable<string>Input { get; set; }
...
// Process the input file paths
foreach(var path in Input)
{
...
}
这需要一个如下所示的命令行:
MyProgram -i c:\path\to\first\file1.txt,c:\path\to\second\file2.txt,c:\path\to\third\file3.txt
但我真正想要的是一个如下所示的命令行:
MyProgram -i c:\path\to\first\file1.txt ^
-i c:\path\to\second\file2.txt ^
-i c:\path\to\third\file3.txt
(注意^字符是windows命令行延续字符)
虽然我会很高兴:
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
目前是否有 CommandLineParser 包可以实现我想要的功能?我对包不是很精通,所以我可能忽略了一些东西。
由于我的无知,我没有意识到可枚举选项的默认分隔符实际上是 space 字符。因此,如果我将定义保留为:
/// <summary>
/// Enumerable list of files
/// </summary>
[Option('i', "input", HelpText = "Input file(s) to process")]
public IEnumerable<string>Input { get; set; }
然后开箱即用的 CommandLineParser 将支持:
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
但我还发现了报告的 Separator does not override Space 问题,该问题建议对命令行进行预处理。这将允许我转换我喜欢的格式:
MyProgram -i c:\path\to\first\file1.txt ^
-i c:\path\to\second\file2.txt ^
-i c:\path\to\third\file3.txt
进入
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
在将其交给 CommandLineParser 本身之前。
Commandlineparser 只允许您设置属性并且仍然强制您编写实际调用方法的代码作为输入您的路径列表。
UltraMapper.CommandLine 允许您直接从命令行调用方法,传递任意数量的输入参数。
我几天前在 github 上发布了 UltraMapper.CommandLine 作为开源项目。
Check it out,可能对你的下一个项目有帮助!
我想使用 CommandLineParser 包构建一个实用程序,可以在命令行上指定任意数量的文件路径。根据我目前对这个包的理解,这样做的规范方法是:
/// <summary>
/// Enumerable list of files
/// </summary>
[Option('i', "input", HelpText = "Input file(s) to process", Separator =',')]
public IEnumerable<string>Input { get; set; }
...
// Process the input file paths
foreach(var path in Input)
{
...
}
这需要一个如下所示的命令行:
MyProgram -i c:\path\to\first\file1.txt,c:\path\to\second\file2.txt,c:\path\to\third\file3.txt
但我真正想要的是一个如下所示的命令行:
MyProgram -i c:\path\to\first\file1.txt ^
-i c:\path\to\second\file2.txt ^
-i c:\path\to\third\file3.txt
(注意^字符是windows命令行延续字符)
虽然我会很高兴:
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
目前是否有 CommandLineParser 包可以实现我想要的功能?我对包不是很精通,所以我可能忽略了一些东西。
由于我的无知,我没有意识到可枚举选项的默认分隔符实际上是 space 字符。因此,如果我将定义保留为:
/// <summary>
/// Enumerable list of files
/// </summary>
[Option('i', "input", HelpText = "Input file(s) to process")]
public IEnumerable<string>Input { get; set; }
然后开箱即用的 CommandLineParser 将支持:
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
但我还发现了报告的 Separator does not override Space 问题,该问题建议对命令行进行预处理。这将允许我转换我喜欢的格式:
MyProgram -i c:\path\to\first\file1.txt ^
-i c:\path\to\second\file2.txt ^
-i c:\path\to\third\file3.txt
进入
MyProgram -i c:\path\to\first\file1.txt ^
c:\path\to\second\file2.txt ^
c:\path\to\third\file3.txt
在将其交给 CommandLineParser 本身之前。
Commandlineparser 只允许您设置属性并且仍然强制您编写实际调用方法的代码作为输入您的路径列表。
UltraMapper.CommandLine 允许您直接从命令行调用方法,传递任意数量的输入参数。
我几天前在 github 上发布了 UltraMapper.CommandLine 作为开源项目。
Check it out,可能对你的下一个项目有帮助!