Question

I'm working with the NuGet Command Line Parser Library. I want to be able to set up some command line tools and I want the command(-v or --version) to return the current version of the application. I have another method set up to find the version and set it to a string so all I need now is that command line argument to set to that current version rather than just expecting something after the command. thanks for the help!

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; }
}

that's just the important parts.

Was it helpful?

Solution

Based on the documentation it looks like you want something like this:

// 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());
  }
}

You don't need the Version property to get the version - you can just use it as a "switch" to tell the program to display the version. If you wanted the user to set the version then a get/set string property would be more appropriate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top