Skip to content

Instantly share code, notes, and snippets.

@ishisaka
Created July 19, 2013 06:42
Show Gist options
  • Save ishisaka/6037134 to your computer and use it in GitHub Desktop.
Save ishisaka/6037134 to your computer and use it in GitHub Desktop.
Mono.Options試し書き
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mono.Options;
namespace CommandLineOption
{
class Program
{
static void Main(string[] args) {
string name = "";
int year = 0;
bool showHelp = false;
var p = new OptionSet()
{
{"n|name=", "your name", v => name = v},
{"y|year=", "your birth year", (int v) => year = v},
{"h|help", "show help.", v => showHelp = v != null}
};
List<string> extra;
try {
extra = p.Parse(args);
}
catch (OptionException e) {
Console.WriteLine("CommandLineOption:");
Console.WriteLine(e.Message);
Console.WriteLine("Try `CommandLineOption --help' for more information.");
return;
}
if (showHelp) {
ShowHelp(p);
return;
}
string message;
if (extra.Count > 0) {
message = string.Join("", extra.ToArray());
}
else {
message = "";
}
int thisYear = DateTime.Now.Year;
Console.WriteLine("Hello, {0}, You are {1} years old.\n{2}", name, thisYear - year, message);
}
private static void ShowHelp(OptionSet p) {
Console.WriteLine("Usage: CommnadLineOption [OPTIONS] + message");
Console.WriteLine( );
p.WriteOptionDescriptions(Console.Out);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment