| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.testing.robolectric.template; | |
| 6 | |
| 7 import java.nio.file.Path; | |
| 8 import java.nio.file.Paths; | |
| 9 import java.util.LinkedList; | |
| 10 import java.util.List; | |
| 11 | |
| 12 /** | |
| 13 * Parses command line arguments for ProcessRobolectricTemplate. | |
| 14 */ | |
| 15 public class ProcessTemplateArgParser { | |
| 16 | |
| 17 private List<TemplateFileInfo> mTemplateFileInfoList; | |
| 18 private Path mBaseTemplateDir; | |
| 19 private Path mOutputDir; | |
| 20 private Integer mApiLevel; | |
| 21 | |
| 22 public static ProcessTemplateArgParser parse(String[] args) { | |
| 23 | |
| 24 ProcessTemplateArgParser parsed = new ProcessTemplateArgParser(); | |
| 25 | |
| 26 for (int i = 0; i < args.length; ++i) { | |
| 27 if (args[i].startsWith("-")) { | |
| 28 String argName; | |
| 29 if (args[i].startsWith("-", 1)) { | |
| 30 argName = args[i].substring(2, args[i].length()); | |
| 31 } else { | |
| 32 argName = args[i].substring(1, args[i].length()); | |
| 33 } | |
| 34 try { | |
| 35 if ("process-file".equals(argName)) { | |
| 36 // Read the two command line arguments after the flag. | |
| 37 // Format is --process-file <template file> <output file
>. | |
| 38 // Argument can be passed multiple times. | |
| 39 Path templatePath = Paths.get(args[++i]); | |
| 40 Path outputPath = Paths.get(args[++i]); | |
| 41 parsed.addTemplateFileInfo( | |
| 42 new TemplateFileInfo(templatePath, outputPath)); | |
| 43 } else if ("api-level".equals(argName)) { | |
| 44 // Read the command line argument after the flag. | |
| 45 parsed.setApiLevel(args[++i]); | |
| 46 } else if ("base-template-dir".equals(argName)) { | |
| 47 // Read the command line argument after the flag. | |
| 48 parsed.setBaseTemplateDir(args[++i]); | |
| 49 } else { | |
| 50 System.out.println("Ignoring flag: \"" + argName + "\"")
; | |
| 51 } | |
| 52 } catch (ArrayIndexOutOfBoundsException e) { | |
| 53 System.err.println("No value specified for argument \"" + ar
gName + "\""); | |
| 54 System.exit(1); | |
| 55 } | |
| 56 } else { | |
| 57 System.out.println("Ignoring argument: \"" + args[i] + "\""); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 if (parsed.getBaseTemplateDir() == null) { | |
| 62 System.err.println("--base-template-dir argument required."); | |
| 63 System.exit(1); | |
| 64 } | |
| 65 | |
| 66 if (parsed.getApiLevel() == null) { | |
| 67 System.err.println("--api-level argument required."); | |
| 68 System.exit(1); | |
| 69 } | |
| 70 return parsed; | |
| 71 } | |
| 72 | |
| 73 private ProcessTemplateArgParser() { | |
| 74 mApiLevel = null; | |
| 75 mBaseTemplateDir = null; | |
| 76 mOutputDir = null; | |
| 77 mTemplateFileInfoList = new LinkedList<TemplateFileInfo>(); | |
| 78 } | |
| 79 | |
| 80 public Integer getApiLevel() { | |
| 81 return mApiLevel; | |
| 82 } | |
| 83 | |
| 84 public Path getBaseTemplateDir() { | |
| 85 return mBaseTemplateDir; | |
| 86 } | |
| 87 | |
| 88 public List<TemplateFileInfo> getTemplateFileInfoList() { | |
| 89 return mTemplateFileInfoList; | |
| 90 } | |
| 91 | |
| 92 private void setApiLevel(String integer) { | |
| 93 mApiLevel = Integer.parseInt(integer); | |
| 94 } | |
| 95 | |
| 96 private void setBaseTemplateDir(String path) { | |
| 97 mBaseTemplateDir = Paths.get(path); | |
| 98 } | |
| 99 | |
| 100 private void addTemplateFileInfo(TemplateFileInfo templateFileInfo) { | |
| 101 mTemplateFileInfoList.add(templateFileInfo); | |
| 102 } | |
| 103 } | |
| OLD | NEW |