00001 /* Copyright © 2006-2007 Roger Leigh <rleigh@debian.org> 00002 * 00003 * schroot is free software; you can redistribute it and/or modify it 00004 * under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation; either version 2 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * schroot is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00016 * MA 02111-1307 USA 00017 * 00018 *********************************************************************/ 00019 00020 #include <config.h> 00021 00022 #include <sbuild/sbuild-i18n.h> 00023 00024 #include "schroot-base-option-action.h" 00025 00026 #include <iomanip> 00027 00028 #include <boost/format.hpp> 00029 #include <boost/program_options.hpp> 00030 00031 using std::endl; 00032 using boost::format; 00033 using sbuild::_; 00034 namespace opt = boost::program_options; 00035 using namespace schroot_base; 00036 00037 option_action::option_action (): 00038 default_action(), 00039 current_action(), 00040 actions() 00041 { 00042 } 00043 00044 option_action::~option_action () 00045 { 00046 } 00047 00048 void 00049 option_action::add (action_type const& action) 00050 { 00051 this->actions.insert(action); 00052 } 00053 00054 option_action::action_type const& 00055 option_action::get_default () 00056 { 00057 return this->default_action; 00058 } 00059 00060 void 00061 option_action::set_default (action_type const& action) 00062 { 00063 if (valid(action)) 00064 this->default_action = action; 00065 else 00066 throw std::logic_error((format(_("%1%: invalid action")) % action).str()); 00067 } 00068 00069 option_action::action_type const& 00070 option_action::get () 00071 { 00072 if (this->current_action != "") 00073 return this->current_action; 00074 else 00075 return this->default_action; 00076 } 00077 00078 void 00079 option_action::set (action_type const& action) 00080 { 00081 if (valid(action)) 00082 { 00083 if (this->current_action == "") 00084 this->current_action = action; 00085 else 00086 throw opt::validation_error(_("Only one action may be specified")); 00087 } 00088 else 00089 throw std::logic_error((format(_("%1%: invalid action")) % action).str()); 00090 } 00091 00092 bool 00093 option_action::valid (action_type const& action) 00094 { 00095 return this->actions.find(action) != this->actions.end(); 00096 } 00097