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

Generated on Thu May 10 23:11:36 2007 for schroot by  doxygen 1.5.2