1 package org.sapia.console; 2 3 4 11 public class Option extends CmdElement { 12 private String _value; 13 14 19 public Option(String name) { 20 super(name); 21 } 22 23 29 public Option(String name, String value) { 30 super(name); 31 _value = value; 32 } 33 34 40 public String getValue() { 41 return _value; 42 } 43 44 52 public int asInt() throws InputException { 53 if (_value == null) { 54 throw new InputException("integer expected for option '" + getName() + 55 "'"); 56 } 57 58 try { 59 return Integer.parseInt(_value); 60 } catch (NumberFormatException e) { 61 throw new InputException("integer expected for option '" + getName() + 62 "'"); 63 } 64 } 65 66 73 public boolean asBoolean() throws InputException { 74 if (_value == null) { 75 throw new InputException( 76 "true/false, yes/no or on/off expected for option '" + getName() + 77 "'"); 78 } 79 80 return _value.equals("true") || _value.equals("yes") || 81 _value.equals("on"); 82 } 83 84 void setValue(String value) { 85 _value = value; 86 } 87 88 public String toString() { 89 if (_value != null) { 90 return "-" + _name + " " + _value; 91 } else { 92 return "-" + _name; 93 } 94 } 95 } 96 | Popular Tags |