Created
          March 28, 2014 17:55 
        
      - 
      
- 
        Save jstrassburg/9838891 to your computer and use it in GitHub Desktop. 
    Parsing command line parameters C# using CmdLine
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | [CommandLineArguments(Program = "MyProgram", Title = "MyProgramTitle", Description = "My awesome program")] | |
| internal class Arguments | |
| { | |
| [CommandLineParameter(Command = "?", Default = false, Description = "Show help", Name = "Help", IsHelp = true)] | |
| public bool Help { get; set; } | |
| [CommandLineParameter(Name = "requiredParameter", ParameterIndex = 1, Required = true, | |
| Description = "This parameter is required")] | |
| public string RequiredParameter { get; set; } | |
| [CommandLineParameter(Command = "dosomething", Description = "Use /dosomething", Required = false)] | |
| public bool DoSomething { get; set; } | |
| [CommandLineParameter(Command = "providesomething", Description = "Use /providesomething:AValue", Required = false)] | |
| public string ProvideSomething { get; set; } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Uses the CmdLine package: http://www.nuget.org/packages/CmdLine/ | |
| static void Main() | |
| { | |
| try | |
| { | |
| var arguments = CommandLine.Parse<Arguments>(); | |
| // do stuff with arguments | |
| } | |
| catch (CommandLineException exception) | |
| { | |
| Console.WriteLine(exception.ArgumentHelp.GetHelpText(Console.BufferWidth)); | |
| } | |
| catch (Exception exception) | |
| { | |
| Console.WriteLine(exception); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment