The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Argument.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Argument.vala.
Remove command line option for font config
1 /* 2 Copyright (C) 2012 2015 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 Gee.ArrayList<string> args; 20 21 public Argument (string line) { 22 args = new Gee.ArrayList<string> (); 23 set_argument (line); 24 } 25 26 public Argument.command_line (string[] arg) { 27 args = new Gee.ArrayList<string> (); 28 29 foreach (string a in arg) { 30 args.add (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 string[] p; 39 40 foreach (string a in args) { 41 if (a == "") { 42 continue; 43 } 44 45 // program name 46 if (i == 0) { 47 prev = a; 48 i++; 49 continue; 50 } 51 52 // file name 53 if (i == 1 && !a.has_prefix ("-")) { 54 prev = a; 55 i++; 56 continue; 57 } 58 59 if (a.index_of ("=") > -1) { 60 p = a.split ("="); 61 a = p[0]; 62 } 63 64 // a single character, like -t 65 if (!a.has_prefix ("--") && a.has_prefix ("-")) { 66 a = expand_param (a); 67 } 68 69 // valid parameters 70 if (a == "--exit" || 71 a == "--slow" || 72 a == "--help" || 73 a == "--test" || 74 a == "--fatal-warning" || 75 a == "--show-coordinates" || 76 a == "--no-translation" || 77 a == "--mac" || 78 a == "--android" || 79 a == "--log" || 80 a == "--windows" || 81 a == "--parse-ucd" || 82 a == "--no-fontconfig" || 83 a == "--codepages") { 84 prev = a; 85 i++; 86 continue; 87 } else if (a.has_prefix ("--")) { 88 return i; 89 } 90 91 // not argument to parameter 92 if (!(prev == "--test")) { 93 return i; 94 } 95 96 prev = a; 97 i++; 98 } 99 100 return 0; 101 } 102 103 /** Return the font file parameter. */ 104 public string get_file () { 105 string f = ""; 106 107 if (args.size >= 2) { 108 f = args.get (1); 109 } 110 111 if (f.has_prefix ("-")) { 112 return ""; 113 } 114 115 return f; 116 } 117 118 public void print_all () { 119 print (@"$(args.size) arguments:\n"); 120 121 foreach (string p in args) { 122 print (@"$p\n"); 123 } 124 } 125 126 public bool has_argument (string param) { 127 return (get_argument (param) != null); 128 } 129 130 /** Get commandline argument. */ 131 public string? get_argument (string param) { 132 int i = 0; 133 string? n; 134 string p; 135 string v = ""; 136 string[] pm; 137 138 if (param.substring (0, 1) != "-") { 139 warning (@"parameters must begin with \"-\" got $param"); 140 return null; 141 } 142 143 foreach (string s in args) { 144 145 if (s.index_of ("=") > -1) { 146 pm = s.split ("="); 147 148 if (pm.length > 1) { 149 v = pm[1]; 150 } 151 152 s = pm[0]; 153 } 154 155 // this is content not a parameter 156 if (s.substring (0, 1) != "-") { 157 continue; 158 } 159 160 // we might need to expand -t to test for instance 161 if (s.substring (0, 2) != "--") { 162 p = expand_param (s); 163 } else { 164 p = s; 165 } 166 167 if (param == p) { 168 if (v != "") { 169 return v; 170 } 171 172 if (i + 2 >= args.size) { 173 return ""; 174 } 175 176 n = args.get (i + 2); 177 if (n == null) { 178 return ""; 179 } 180 181 if (args.get (i + 2).substring (0, 1) == "-") { 182 return ""; 183 } 184 185 return args.get (i + 2); 186 } 187 188 i++; 189 } 190 191 return null; 192 } 193 194 private void print_padded (string cmd, string desc) { 195 int l = 25 - cmd.char_count (); 196 197 stdout.printf (cmd); 198 199 for (int i = 0; i < l; i++) { 200 stdout.printf (" "); 201 } 202 203 stdout.printf (desc); 204 stdout.printf ("\n"); 205 } 206 207 /** Return full command line parameter for the abbrevation. 208 * -t becomes --test. 209 */ 210 private string expand_param (string? param) { 211 if (param == null) return ""; 212 var p = (!) param; 213 214 if (p.get_char (0) != '-') { 215 return ""; 216 } 217 218 if (p.char_count () != 2) { 219 return ""; 220 } 221 222 switch (p.get_char (1)) { 223 case 'c': 224 return "--show-coordinates"; 225 case 'e': 226 return "--exit"; 227 case 'f': 228 return "--fatal-warning"; 229 case 'h': 230 return "--help"; 231 case 'm': 232 return "--mac"; 233 case 'n': 234 return "--no-translation"; 235 case 's': 236 return "--slow"; 237 case 't': 238 return "--test"; 239 case 'a': 240 return "--android"; 241 case 'l': 242 return "--log"; 243 case 'w': 244 return "--windows"; 245 } 246 247 return ""; 248 } 249 250 private void set_argument (string arg) { 251 int i = 0; 252 int a; 253 string n; 254 255 if (arg.char_count () <= 1) { 256 return; 257 } 258 259 do { 260 a = arg.index_of (" ", i + 1); 261 n = arg.substring (i, a - i); 262 263 if (n.index_of ("\"") == 0) { 264 a = arg.index_of ("\"", i + 1); 265 n = arg.substring (i, a - i + 1); 266 } 267 268 args.add (n); 269 270 i += n.char_count () + 1; 271 } while (i < arg.char_count ()); 272 } 273 274 public void print_help () 275 requires (args.size > 0) 276 { 277 stdout.printf (t_("Usage") + ": "); 278 stdout.printf (args.get (0)); 279 stdout.printf (" [" + t_("FILE") + "] [" + t_("OPTION") + " ...]\n"); 280 281 print_padded ("-a, --android", t_("enable Android customizations")); 282 print_padded ("-c, --show-coordinates", t_("show coordinate in glyph view")); 283 print_padded ("-e, --exit", t_("exit if a test case fails")); 284 print_padded ("-f, --fatal-warning", t_("treat warnings as fatal")); 285 print_padded ("-h, --help", t_("show this message")); 286 print_padded ("-l, --log", t_("write a log file")); 287 print_padded ("-m, --mac", t_("enable Machintosh customizations")); 288 print_padded ("-w, --windows", t_("enable Windows customizations")); 289 print_padded ("-n, --no-translation", t_("don't translate")); 290 print_padded ("-s, --slow", t_("sleep between each command in test suite")); 291 print_padded ("-t --test [TEST]", t_("run test case")); 292 293 stdout.printf ("\n"); 294 } 295 296 } 297 298 } 299