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