Skip to content

Instantly share code, notes, and snippets.

@kensykora
Created October 1, 2015 14:31
Show Gist options
  • Save kensykora/f486cdf239a823390409 to your computer and use it in GitHub Desktop.
Save kensykora/f486cdf239a823390409 to your computer and use it in GitHub Desktop.
CommandLine Parser Issue
using System;
using CommandLine;
using CommandLine.Text;
using System.Collections.Generic;
/*
Output:
push-registration.exe register
Push Helper 1.0
ERROR(S):
A value not bound to option name is defined with a bad format.
A required value not bound to option name is missing.
USAGE:
Normal scenario:
push-registration register <token> <tags>
...
Expected:
Normal scenario:
push-registration register iOS <token> <tags>
*/
public class Program
{
public static int Main(string[] args)
{
args = new[] { "--help" };
return Parser.Default.ParseArguments<RegisterOptions>(args)
.MapResult(HandleRegister, errs => 1);
}
private static int HandleRegister(RegisterOptions options)
{
// do stuff
return 0;
}
public class VerbOptionsBase
{
[Option("connectionString", Default = "conn", HelpText = "Connection string to Azure")]
public string ConnectionString { get; set; }
[Option("hub", Default = "hub", HelpText = "Hub Name within the connection string")]
public string HubName { get; set; }
}
[Verb("register", HelpText = "Registers a device with Azure for push")]
public class RegisterOptions : VerbOptionsBase
{
[Usage]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Normal scenario", new RegisterOptions { Platform = PlatformType.iOS, PushToken = "<token>", Tags = new[] { "<tags>" } });
}
}
public enum PlatformType
{
iOS,
Android
}
[Value(0, MetaName = "platform", HelpText = "Platform associated wth push token. iOS/Android supported.", Required = true)]
public PlatformType Platform { get; set; }
[Value(1, MetaName = "token", HelpText = "The push token", Required = true)]
public string PushToken { get; set; }
[Value(2, MetaName = "tags", HelpText = "Tags associated with registration", Required = false)]
public IEnumerable<string> Tags { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment