Home | History | Annotate | Download | only in util
      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.util;
     18 
     19 /**
     20  * Helper class for parsing command line arguments.
     21  */
     22 public class ArgParser {
     23     private final String[] mArgs;
     24     private int mPos;
     25 
     26     /**
     27      * Construct with the arguments from main(String[]).
     28      */
     29     public ArgParser(String[] args) {
     30         mArgs = args;
     31     }
     32 
     33     public String nextFlag() {
     34         if (mPos < mArgs.length) {
     35             if (mArgs[mPos].startsWith("--")) {
     36                 return mArgs[mPos++];
     37             }
     38         }
     39         return null;
     40     }
     41 
     42     public boolean hasData(int count) {
     43         for (int i=mPos; i<mPos+count; i++) {
     44             if (i >= mArgs.length) {
     45                 return false;
     46             }
     47             if (mArgs[i].startsWith("--")) {
     48                 return false;
     49             }
     50         }
     51         return true;
     52     }
     53 
     54     public String nextData() {
     55         if (mPos < mArgs.length) {
     56             return mArgs[mPos++];
     57         }
     58         return null;
     59     }
     60 
     61     public int remaining() {
     62         return mArgs.length - mPos;
     63     }
     64 
     65     public int pos() {
     66         return mPos;
     67     }
     68 }
     69