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

Generated on Mon Jan 21 00:38:29 2008 for schroot by  doxygen 1.5.4