1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.bugreport; 18 19 import com.android.bugreport.util.ArgParser; 20 21 import java.io.File; 22 23 /** 24 * Class to encapsulate the command line arguments. 25 */ 26 public class Options { 27 /** 28 * The original raw args array. 29 */ 30 public String[] args; 31 32 /** 33 * If there was an error parsing, the index into the args array that 34 * had the error. 35 * 36 * Will be -1 if there was no error. 37 */ 38 public int errorIndex; 39 40 /** 41 * Human readable description of the problem encountered while parsing. 42 * 43 * Will be null if there was no error. 44 */ 45 public String errorText; 46 47 /** 48 * The bugreport file to parse. 49 */ 50 public File bugreport; 51 52 /** 53 * The monkey log file to parse. 54 * 55 * Will be used instead of searching the logcat for the problem. 56 */ 57 public File monkey; 58 59 /** 60 * The logcat file to parse. 61 * 62 * Will be used instead of the "SYSTEM LOG" section of the bugreport. 63 */ 64 public File logcat; 65 66 /** 67 * The html file to output. 68 */ 69 public File html; 70 71 /** 72 * Parse the arguments. 73 * 74 * Always returns an Options object. It will either be a correctly formed one 75 * with all the arguments, or one with nothing set except for the args, errorText 76 * and errorIndex fields. 77 */ parseArgs(String[] args)78 public static Options parseArgs(String[] args) { 79 final Options result = new Options(args); 80 81 String flag; 82 final ArgParser argParser = new ArgParser(args); 83 while ((flag = argParser.nextFlag()) != null) { 84 if ("--monkey".equals(flag)) { 85 if (result.monkey != null || !argParser.hasData(1)) { 86 return new Options(args, argParser.pos(), 87 "--monkey flag requires an argument"); 88 } 89 result.monkey = new File(argParser.nextData()); 90 } else if ("--html".equals(flag)) { 91 if (result.html != null || !argParser.hasData(1)) { 92 return new Options(args, argParser.pos(), 93 "--html flag requires an argument"); 94 } 95 result.html = new File(argParser.nextData()); 96 } else if ("--logcat".equals(flag)) { 97 if (result.logcat != null || !argParser.hasData(1)) { 98 return new Options(args, argParser.pos(), 99 "--logcat flag requires an argument"); 100 } 101 result.logcat = new File(argParser.nextData()); 102 } else { 103 return new Options(args, argParser.pos(), 104 "Unknown flag: " + flag); 105 } 106 } 107 if ((!argParser.hasData(1)) || argParser.remaining() != 1) { 108 return new Options(args, argParser.pos(), 109 "bugreport file name required"); 110 } 111 result.bugreport = new File(argParser.nextData()); 112 113 return result; 114 } 115 116 /** 117 * Construct a "successful" Options object. 118 */ Options(String[] args)119 private Options(String[] args) { 120 this.args = args; 121 this.errorIndex = -1; 122 } 123 124 /** 125 * Construct an error Options object. 126 */ Options(String[] args, int errorIndex, String errorText)127 private Options(String[] args, int errorIndex, String errorText) { 128 this.args = args; 129 this.errorIndex = errorIndex; 130 this.errorText = errorText; 131 } 132 } 133 134 135 136