getopt

コマンドラインのオプションを解析する定番で、C言語以外でも使われている。
今回はC#版をNugetからインポートして使ってみた。

using System;

using Gnu.Getopt;

namespace getopt
{
    class Program
    {
        static void Main(string[] args)
        {
            Getopt opt = new Getopt(args[0],args,"abc:f:");
            int c;
            while((c = opt.getopt())!=-1){
                switch (c)
                {
                    case 'a':
                    case 'b':
                        Console.WriteLine("オプション - "+(char)c);
                        break;
                    case 'c':
                    case 'f':
                        Console.WriteLine("オプション - " + (char)c + "  " + opt.Optarg);
                        break;
                }
            }
            int n = opt.Optind;
            int i;
            for (i = n; i < opt.Argv.Length; i++)
            {
                Console.WriteLine("パラメータ - " + opt.Argv[i]);
            }
        }
    }
}
カテゴリー: プログラム, 仕事 タグ: , , パーマリンク