.
1 /*
2 Copyright (C) 2012 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 namespace BirdFont {
16
17 public class Argument : GLib.Object {
18
19 List<string> args;
20
21 public Argument (string line) {
22 args = new List<string> ();
23 set_argument (line);
24 }
25
26 public Argument.command_line (string[] arg) {
27 args = new List<string> ();
28
29 foreach (string a in arg) {
30 args.append (a);
31 }
32 }
33
34 /** returns 0 if all arguments are valid or index of the invalid parameter. */
35 public int validate () {
36 string prev = "";
37 int i = 0;
38 foreach (string a in args) {
39
40 if (a == "") {
41 continue;
42 }
43
44 // program name
45 if (i == 0) {
46 prev = a;
47 i++;
48 continue;
49 }
50
51 // file name
52 if (i == 1 && !a.has_prefix ("-")) {
53 prev = a;
54 i++;
55 continue;
56 }
57
58 // a single character, like -t
59 if (!a.has_prefix ("--") && a.has_prefix ("-")) {
60 a = expand_param (a);
61 }
62
63 // valid parameters
64 if (a == "--exit" ||
65 a == "--slow" ||
66 a == "--help" ||
67 a == "--test" ||
68 a == "--fatal-warning" ||
69 a == "--show-coordinates" ||
70 a == "--no-translation" ||
71 a == "--mac" ||
72 a == "--android" ||
73 a == "--log" ||
74 a == "--no-ucd" ||
75 a == "--windows") {
76 prev = a;
77 i++;
78 continue;
79 } else if (a.has_prefix ("--")) {
80 return i;
81 }
82
83 // not argument to parameter
84 if (!(prev == "--test")) {
85 return i;
86 }
87
88 prev = a;
89 i++;
90 }
91
92 return 0;
93 }
94
95 /** Return the font file parameter. */
96 public string get_file () {
97 string f = "";
98
99 if (args.length () >= 2) {
100 f = args.nth (1).data;
101 }
102
103 if (f.has_prefix ("-")) {
104 return "";
105 }
106
107 return f;
108 }
109
110 public void print_all () {
111 print (@"$(args.length ()) arguments:\n");
112
113 foreach (string p in args) {
114 print (@"$p\n");
115 }
116 }
117
118 public bool has_argument (string param) {
119 return (get_argument (param) != null);
120 }
121
122 /** Get commandline argument. */
123 public string? get_argument (string param) {
124 int i = 0;
125 string? n;
126 string p;
127
128 if (param.substring (0, 1) != "-") {
129 warning (@"parameters must begin with \"-\" got $param");
130 return null;
131 }
132
133 foreach (string s in args) {
134
135 // this is content not a parameter
136 if (s.substring (0, 1) != "-") continue;
137
138 // we might need to expand -t to test fo instance
139 if (s.substring (0, 2) != "--") {
140 p = expand_param (s);
141 } else {
142 p = s;
143 }
144
145 if (param == p) {
146 if (i + 2 >= args.length ()) {
147 return "";
148 }
149
150 n = args.nth (i + 2).data;
151 if (n == null) {
152 return "";
153 }
154
155 if (args.nth (i + 2).data.substring (0, 1) == "-") {
156 return "";
157 }
158
159 return args.nth (i + 2).data;
160 }
161
162 i++;
163 }
164
165 return null;
166 }
167
168 private void print_padded (string cmd, string desc) {
169 int l = 25 - cmd.char_count ();
170
171 stdout.printf (cmd);
172
173 for (int i = 0; i < l; i++) {
174 stdout.printf (" ");
175 }
176
177 stdout.printf (desc);
178 stdout.printf ("\n");
179 }
180
181 /** Return full command line parameter for the abbrevation.
182 * -t becomes --test.
183 */
184 private string expand_param (string? param) {
185 if (param == null) return "";
186 var p = (!) param;
187
188 if (p.length == 0) return "";
189 if (p.get_char (0) != '-') return "";
190 if (p.char_count () != 2) return "";
191
192 switch (p.get_char (1)) {
193 case 'c':
194 return "--show-coordinates";
195 case 'e':
196 return "--exit";
197 case 'f':
198 return "--fatal-warning";
199 case 'h':
200 return "--help";
201 case 'm':
202 return "--mac";
203 case 'n':
204 return "--no-translation";
205 case 's':
206 return "--slow";
207 case 't':
208 return "--test";
209 case 'a':
210 return "--android";
211 case 'l':
212 return "--log";
213 case 'w':
214 return "--windows";
215 }
216
217 return "";
218 }
219
220 private void set_argument (string arg) {
221 int i = 0;
222 int a;
223 string n;
224
225 if (arg.char_count () <= 1) {
226 return;
227 }
228
229 do {
230 a = arg.index_of (" ", i + 1);
231 n = arg.substring (i, a - i);
232
233 if (n.index_of ("\"") == 0) {
234 a = arg.index_of ("\"", i + 1);
235 n = arg.substring (i, a - i + 1);
236 }
237
238 args.append (n);
239
240 i += n.char_count () + 1;
241 } while (i < arg.char_count ());
242 }
243
244 public void print_help ()
245 requires (args.length () > 0)
246 {
247 stdout.printf (t_("Usage") + ": ");
248 stdout.printf (args.nth (0).data);
249 stdout.printf (" [" + t_("FILE") + "] [" + t_("OPTION") + " ...]\n");
250
251 print_padded ("-a, --android", t_("enable Android customizations"));
252 print_padded ("-c, --show-coordinates", t_("show coordinate in glyph view"));
253 print_padded ("-e, --exit", t_("exit if a test case fails"));
254 print_padded ("-f, --fatal-warning", t_("treat warnings as fatal"));
255 print_padded ("-h, --help", t_("show this message"));
256 print_padded ("-l, --log", t_("write a log file"));
257 print_padded ("-m, --mac", t_("enable Machintosh customizations"));
258 print_padded ("-w, --windows", t_("enable Windows customizations"));
259 print_padded ("-n, --no-translation", t_("don't translate"));
260 print_padded ("-s, --slow", t_("sleep between each command in test suite"));
261 print_padded ("-t --test [TEST]", t_("run test case"));
262
263 stdout.printf ("\n");
264 }
265
266 }
267
268 }
269