命令行解析器库.NET能够检索版本号

has*_*123 3 .net c# command-line winforms command-line-parsing

我正在使用NuGet命令行解析器库。我希望能够设置一些命令行工具,并且希望命令(-v或--version)返回应用程序的当前版本。我设置了另一种方法来查找版本并将其设置为字符串,因此,我现在所需要的只是将命令行参数设置为该当前版本,而不是仅在命令后期待什么。谢谢您的帮助!

static string GetVersion() { 
    Assembly assembly = Assembly.GetExecutingAssembly();
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
    string currentVersion = fvi.FileVersion;
    return currentVersion;
}

class Options 
{
    [Option('v', "version", HelpText = "Sets version to be run")]
    public string Version { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

那只是重要的部分。

D S*_*ley 5

根据文档,您看起来像这样:

// Define a class to receive parsed values
class Options {

  [Option('v', "version", 
    HelpText = "Prints version information to standard output.")]
  public bool Version { get; set; }

  [ParserState]
  public IParserState LastParserState { get; set; }

  [HelpOption]
  public string GetUsage() {
    return HelpText.AutoBuild(this,
      (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
  }
}

// Consume them
static void Main(string[] args) {
  var options = new Options();
  if (CommandLine.Parser.Default.ParseArguments(args, options)) {
    // Values are available here
    if (options.Version) Console.WriteLine("Version: {0}", GetVersion());
  }
}
Run Code Online (Sandbox Code Playgroud)

您不需要该Version属性即可获取版本-您只需将其用作“开关”即可告诉程序显示版本。如果您希望用户设置版本,则获取/设置字符串属性将更为合适。