OSDN Git Service

改行コード指定
[jindolf/Jindolf.git] / src / main / java / jp / sourceforge / jindolf / CmdOption.java
1 /*
2  * command line options
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf;
9
10 import java.io.IOException;
11 import java.util.LinkedList;
12 import java.util.List;
13
14 /**
15  * コマンドラインオプションの列挙。
16  */
17 public enum CmdOption{
18
19     /** ヘルプ。 */
20     OPT_HELP("help", "h", "-help", "?"),
21     /** 版数表示。 */
22     OPT_VERSION("version"),
23     /** UI文字制御。 */
24     OPT_BOLDMETAL("boldMetal"),
25     /** スプラッシュ制御。 */
26     OPT_NOSPLASH("nosplash"),
27     /** ウィンドウ位置指定。 */
28     OPT_GEOMETRY("geometry"),
29     /** 実行環境出力。 */
30     OPT_VMINFO("vminfo"),
31     /** コンソールログ。 */
32     OPT_CONSOLELOG("consolelog"),
33     /** フォント指定。 */
34     OPT_INITFONT("initfont"),
35     /** アンチエイリアス。 */
36     OPT_ANTIALIAS("antialias"),
37     /** サブピクセル制御。 */
38     OPT_FRACTIONAL("fractional"),
39     /** 設定格納ディレクトリ指定。 */
40     OPT_CONFDIR("confdir"),
41     /** 設定格納ディレクトリ不使用。 */
42     OPT_NOCONF("noconfdir"),
43     ;
44
45
46     private final List<String> nameList = new LinkedList<String>();
47
48
49     /**
50      * コンストラクタ。
51      * @param names 頭のハイフンを除いたオプション名の一覧
52      */
53     private CmdOption(CharSequence ... names){
54         if(names == null) throw new NullPointerException();
55         if(names.length <= 0) throw new IllegalArgumentException();
56
57         for(CharSequence name : names){
58             if(name == null) throw new NullPointerException();
59             this.nameList.add(name.toString().intern());
60         }
61
62         return;
63     }
64
65
66     /**
67      * オプション名に合致するEnumを返す。
68      * @param seq ハイフン付きオプション名
69      * @return 合致したEnum。どれとも合致しなければnull
70      */
71     public static CmdOption parseCmdOption(CharSequence seq){
72         for(CmdOption option : values()){
73             if(option.matchHyphened(seq)) return option;
74         }
75         return null;
76     }
77
78     /**
79      * 単体で意味をなすオプションか判定する。
80      * @param option オプション
81      * @return 単体で意味をなすならtrue
82      */
83     public static boolean isIndepOption(CmdOption option){
84         switch(option){
85         case OPT_HELP:
86         case OPT_VERSION:
87         case OPT_VMINFO:
88         case OPT_BOLDMETAL:
89         case OPT_NOSPLASH:
90         case OPT_CONSOLELOG:
91         case OPT_NOCONF:
92             return true;
93         default:
94             break;
95         }
96
97         return false;
98     }
99
100     /**
101      * 真偽指定を一つ必要とするオプションか判定する。
102      * @param option オプション
103      * @return 真偽指定を一つ必要とするオプションならtrue
104      */
105     public static boolean isBooleanOption(CmdOption option){
106         switch(option){
107         case OPT_ANTIALIAS:
108         case OPT_FRACTIONAL:
109             return true;
110         default:
111             break;
112         }
113
114         return false;
115     }
116
117     /**
118      * ヘルプメッセージ(オプションの説明)を返す。
119      * @return ヘルプメッセージ
120      */
121     public static CharSequence getHelpText(){
122         CharSequence helpText;
123
124         try{
125             helpText = Jindolf.loadResourceText("resources/help.txt");
126         }catch(IOException e){
127             helpText = "";
128         }
129
130         return helpText;
131     }
132
133     /**
134      * 頭のハイフンを除いたオプション名を返す。
135      * オプション名が複数指定されていた場合は最初のオプション名
136      * @return オプション名
137      */
138     @Override
139     public String toString(){
140         return this.nameList.get(0);
141     }
142
143     /**
144      * 頭のハイフンが付いたオプション名を返す。
145      * オプション名が複数指定されていた場合は最初のオプション名
146      * @return オプション名
147      */
148     public String toHyphened(){
149         return "-" + toString();
150     }
151
152     /**
153      * 任意のオプション文字列がこのオプションに合致するか判定する。
154      * @param option ハイフンの付いたオプション文字列
155      * @return 合致すればtrue
156      */
157     public boolean matchHyphened(CharSequence option){
158         if(option == null) return false;
159
160         for(String name : this.nameList){
161             String hyphened = "-" + name;
162             if(hyphened.equals(option.toString())) return true;
163         }
164
165         return false;
166     }
167
168 }