Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Parser Library .NET being able to retrieve version number

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.

like image 571
hasti123 Avatar asked Mar 03 '14 19:03

hasti123


1 Answers

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.

like image 177
D Stanley Avatar answered Sep 30 '22 08:09

D Stanley