schroot-options-base.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-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 "schroot-options.h"
00023 
00024 #include <cstdlib>
00025 #include <iostream>
00026 
00027 #include <boost/format.hpp>
00028 #include <boost/program_options.hpp>
00029 
00030 using std::endl;
00031 using boost::format;
00032 using sbuild::_;
00033 namespace opt = boost::program_options;
00034 using namespace schroot;
00035 
00036 const options_base::action_type options_base::ACTION_SESSION_AUTO ("session_auto");
00037 const options_base::action_type options_base::ACTION_SESSION_BEGIN ("session_begin");
00038 const options_base::action_type options_base::ACTION_SESSION_RECOVER ("session_recover");
00039 const options_base::action_type options_base::ACTION_SESSION_RUN ("session_run");
00040 const options_base::action_type options_base::ACTION_SESSION_END ("session_end");
00041 const options_base::action_type options_base::ACTION_LIST ("list");
00042 const options_base::action_type options_base::ACTION_INFO ("info");
00043 const options_base::action_type options_base::ACTION_LOCATION ("location");
00044 const options_base::action_type options_base::ACTION_CONFIG ("config");
00045 
00046 options_base::options_base ():
00047   schroot_base::options (),
00048   chroots(),
00049   command(),
00050   user(),
00051   preserve(false),
00052   all(false),
00053   all_chroots(false),
00054   all_sessions(false),
00055   session_name(),
00056   session_force(false),
00057   chroot(_("Chroot selection")),
00058   chrootenv(_("Chroot environment")),
00059   session_actions(_("Session actions")),
00060   session_options(_("Session options"))
00061 {
00062 }
00063 
00064 options_base::~options_base ()
00065 {
00066 }
00067 
00068 void
00069 options_base::add_options ()
00070 {
00071   // Chain up to add basic options.
00072   schroot_base::options::add_options();
00073 
00074   action.add(ACTION_SESSION_AUTO);
00075   action.set_default(ACTION_SESSION_AUTO);
00076   action.add(ACTION_SESSION_BEGIN);
00077   action.add(ACTION_SESSION_RECOVER);
00078   action.add(ACTION_SESSION_RUN);
00079   action.add(ACTION_SESSION_END);
00080   action.add(ACTION_LIST);
00081   action.add(ACTION_INFO);
00082   action.add(ACTION_LOCATION);
00083   action.add(ACTION_CONFIG);
00084 
00085   actions.add_options()
00086     ("list,l",
00087      _("List available chroots"))
00088     ("info,i",
00089      _("Show information about selected chroots"))
00090     ("config",
00091      _("Dump configuration of selected chroots"));
00092 
00093   chroot.add_options()
00094     ("chroot,c", opt::value<sbuild::string_list>(&this->chroots),
00095      _("Use specified chroot"));
00096 
00097   hidden.add_options()
00098     ("command", opt::value<sbuild::string_list>(&this->command),
00099      _("Command to run"));
00100 
00101   positional.add("command", -1);
00102 }
00103 
00104 void
00105 options_base::add_option_groups ()
00106 {
00107   // Chain up to add basic option groups.
00108   schroot_base::options::add_option_groups();
00109 
00110 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00111   if (!chroot.options().empty())
00112 #else
00113   if (!chroot.primary_keys().empty())
00114 #endif
00115     {
00116       visible.add(chroot);
00117       global.add(chroot);
00118     }
00119 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00120   if (!chrootenv.options().empty())
00121 #else
00122   if (!chrootenv.primary_keys().empty())
00123 #endif
00124     {
00125       visible.add(chrootenv);
00126       global.add(chrootenv);
00127     }
00128 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00129   if (!session_actions.options().empty())
00130 #else
00131   if (!session_actions.primary_keys().empty())
00132 #endif
00133     {
00134       visible.add(session_actions);
00135       global.add(session_actions);
00136     }
00137 
00138 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00139   if (!session_options.options().empty())
00140 #else
00141   if (!session_options.primary_keys().empty())
00142 #endif
00143     {
00144       visible.add(session_options);
00145       global.add(session_options);
00146     }
00147 }
00148 
00149 void
00150 options_base::check_options ()
00151 {
00152   // Chain up to check basic options.
00153   schroot_base::options::check_options();
00154 
00155   if (vm.count("list"))
00156     this->action = ACTION_LIST;
00157   if (vm.count("info"))
00158     this->action = ACTION_INFO;
00159   if (vm.count("config"))
00160     this->action = ACTION_CONFIG;
00161 }
00162 
00163 void
00164 options_base::check_actions ()
00165 {
00166   // Chain up to check basic actions.
00167   schroot_base::options::check_actions();
00168 
00169   if (this->quiet && this->verbose)
00170     {
00171       sbuild::log_warning()
00172         << _("--quiet and --verbose may not be used at the same time")
00173         << endl;
00174       sbuild::log_info() << _("Using verbose output") << endl;
00175     }
00176 
00177   if (!this->chroots.empty() && all_used())
00178     {
00179       sbuild::log_warning()
00180         << _("--chroot and --all may not be used at the same time")
00181         << endl;
00182       sbuild::log_info() << _("Using --chroots only") << endl;
00183       this->all = this->all_chroots = this->all_sessions = false;
00184     }
00185 
00186   /* Determine which chroots to load and use. */
00187   if (this->action == ACTION_SESSION_AUTO)
00188     {
00189       // Only allow normal chroots
00190       this->load_chroots = true;
00191       this->load_sessions = false;
00192       this->all = this->all_sessions = false;
00193 
00194       // If no chroot was specified, fall back to the "default" chroot.
00195       if (this->chroots.empty() && all_used() == false)
00196         this->chroots.push_back("default");
00197     }
00198   else if (this->action == ACTION_SESSION_BEGIN)
00199     {
00200       // Only allow one session chroot
00201       this->load_chroots = true;
00202       this->load_sessions = false;
00203       if (this->chroots.size() != 1 || all_used())
00204         throw opt::validation_error(_("Exactly one chroot must be specified when beginning a session"));
00205 
00206       this->all = this->all_chroots = this->all_sessions = false;
00207     }
00208   else if (this->action == ACTION_SESSION_RECOVER ||
00209            this->action == ACTION_SESSION_RUN ||
00210            this->action == ACTION_SESSION_END)
00211     {
00212       // Session operations work on all chroots.
00213       this->load_chroots = this->load_sessions = true;
00214     }
00215   else if (this->action == ACTION_HELP ||
00216            this->action == ACTION_VERSION)
00217     {
00218       // Chroots don't make sense here.
00219       this->load_chroots = this->load_sessions = false;
00220       this->all = this->all_chroots = this->all_sessions = false;
00221     }
00222   else if (this->action == ACTION_LIST)
00223     {
00224       // If not specified otherwise, load normal chroots, but allow
00225       // --all options.
00226       if (!all_used())
00227         this->load_chroots = true;
00228       if (this->all_chroots)
00229         this->load_chroots = true;
00230       if (this->all_sessions)
00231         this->load_sessions = true;
00232       if (!this->chroots.empty())
00233         throw opt::validation_error(_("--chroot may not be used with --list"));
00234     }
00235   else if (this->action == ACTION_INFO ||
00236            this->action == ACTION_LOCATION ||
00237            this->action == ACTION_CONFIG)
00238     {
00239       // If not specified otherwise, load normal chroots, but allow
00240       // --all options.
00241       if (!this->chroots.empty()) // chroot specified
00242         this->load_chroots = this->load_sessions = true;
00243       else if (!all_used()) // no chroots specified
00244         {
00245           this->all_chroots = true;
00246           this->load_chroots = true;
00247         }
00248       if (this->all_chroots)
00249         this->load_chroots = true;
00250       if (this->all_sessions)
00251         this->load_sessions = true;
00252     }
00253   else
00254     {
00255       // Something went wrong
00256       this->load_chroots = this->load_sessions = false;
00257       this->all = this->all_chroots = this->all_sessions = false;
00258       throw opt::validation_error(_("Unknown action specified"));
00259     }
00260 }

Generated on Sun Jul 8 21:23:55 2007 for schroot by  doxygen 1.5.2