158a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust/** 258a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Copyright (C) 2010 the original author or authors. 358a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * See the notice.md file distributed with this work for additional 458a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * information regarding copyright ownership. 558a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * 658a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Licensed under the Apache License, Version 2.0 (the "License"); 758a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * you may not use this file except in compliance with the License. 858a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * You may obtain a copy of the License at 958a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * 1058a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * http://www.apache.org/licenses/LICENSE-2.0 1158a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * 1258a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * Unless required by applicable law or agreed to in writing, software 1358a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * distributed under the License is distributed on an "AS IS" BASIS, 1458a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1558a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * See the License for the specific language governing permissions and 1658a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust * limitations under the License. 1758a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust */ 1858a4af720c30d8dba072a96e3713577c8d30aeaeCedric Beust 195a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beustpackage com.beust.jcommander.converters; 205a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust 2176c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beustimport com.beust.jcommander.ParameterException; 225a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust 2330e1bb6cbc100c085435a7abdae9d272603726b3Cedric Beust/** 2430e1bb6cbc100c085435a7abdae9d272603726b3Cedric Beust * Converts a string to a boolean. 2530e1bb6cbc100c085435a7abdae9d272603726b3Cedric Beust * 2630e1bb6cbc100c085435a7abdae9d272603726b3Cedric Beust * @author cbeust 2730e1bb6cbc100c085435a7abdae9d272603726b3Cedric Beust */ 2876c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beustpublic class BooleanConverter extends BaseConverter<Boolean> { 2976c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust 3076c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust public BooleanConverter(String optionName) { 3176c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust super(optionName); 3276c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust } 335a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust 345a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust public Boolean convert(String value) { 3576c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust if ("false".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value)) { 3676c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust return Boolean.parseBoolean(value); 3776c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust } else { 3876c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust throw new ParameterException(getErrorString(value, "a boolean")); 3976c4b6ace154f18794f9eb0c3d6211101bf426d0Cedric Beust } 405a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust } 415a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust 425a8d2cda56e06eccac04f985cfb6df4886cb270eCedric Beust} 43