我正在尝试在项目上编译以下代码
...
namespace Buzzbox
{
class Program
{
//Command line options through CommandLine: http://commandline.codeplex.com/
class Options
{
[Option('i', "input",
Required = true,
HelpText = "Path to input file to be Encoded, must be in hearthstonejson format.")]
public string InputFile { get; set; }
[Option('o', "output",
HelpText = "Output file path.",
Default = "output.txt")]
public string OutputFile { get; set; }
[Option('e', "encoding",
HelpText = "Which encoding format to use. Supported formats are scfdivineFormat and MtgEncoderFormat.",
Default = EncodingFormats.MtgEncoderFormat)]
public EncodingFormats EncodingFormat { get; set; }
[Option("shuffle-fields", Default = false,
HelpText = "Shuffles the fields of the output in supported Encoding Formats.")]
public bool ShuffleFields { get; set; }
[Option("shuffle-cards", Default = false,
HelpText = "Shuffles the the cards, randomizing the order of output.")]
public bool ShuffleCards { get; set; }
[Option("flavor-text", Default = false,
HelpText = "Include flavortext field.")]
public bool FlavorText { get; set; }
[Option("verbose", Default = false,
HelpText = "Output additional information. Exclusive with the --silent option.")]
public bool Verbose { get; set; }
[Option("silent", Default = false,
HelpText = "Never output anything but error messages. Exclusive with the --verbose option.")]
public bool Silent { get; set; }
}
private static void Main(string[] args)
{
//Parse Commandline options
var options = new Options();
var encode = new Encode
{
ShuffleFields = options.ShuffleFields,
IncludeFlavorText = options.FlavorText
};
//Only continue if commandline options fullfilled. CommandLine will handle helptext if something was off.
if (CommandLine.Parser.Default.ParseArguments(args,options))
{
//extra things
}
}
}
}
但我似乎只是因为这一行出错了
CommandLine.Parser.Default.ParseArguments(args,options)
抛出异常
无法从'Buzzbox.Program.Options'转换为'System.Type'
它不允许我对它进行硬编辑,但我没有找到任何解决这个问题的东西,虽然我觉得解决方案可能相当简单,因为我发现其他一些人提到它就像你可以投它像这样的代码没有任何问题,如在这里
http://simontimms.com/2014/07/09/parsing-command-line-arguments-in-c/
答案 0 :(得分:3)
因此,我显然在最新版本的命令行解析器应用程序中进行了一些挖掘,要求您执行以下操作。
CommandLine.Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(opts => options = opts);
我有空余时间来寻找适当的方法来做这件事。
答案 1 :(得分:0)
他们的网站已经过时,但是github是最新的完整示例:
Program
{
public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
public bool Verbose { get; set; }
}
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if (o.Verbose)
{
Console.WriteLine($"Verbose output enabled. Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example! App is in Verbose mode!");
}
else
{
Console.WriteLine($"Current Arguments: -v {o.Verbose}");
Console.WriteLine("Quick Start Example!");
}
});
}
}