schroot-options-base.cc

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

Generated on Sat Jan 27 16:11:04 2007 for schroot by  doxygen 1.5.1