Skip to content

Instantly share code, notes, and snippets.

@jstrassburg
Created March 28, 2014 17:55
Show Gist options
  • Save jstrassburg/9838891 to your computer and use it in GitHub Desktop.
Save jstrassburg/9838891 to your computer and use it in GitHub Desktop.
Parsing command line parameters C# using CmdLine
[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; }
}
// 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