00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00152 switch (this->action)
00153 {
00154 case ACTION_SESSION_AUTO:
00155
00156 this->load_chroots = true;
00157 this->load_sessions = false;
00158 this->all = this->all_sessions = false;
00159
00160
00161 if (this->chroots.empty() && all_used() == false)
00162 this->chroots.push_back("default");
00163
00164 break;
00165 case ACTION_SESSION_BEGIN:
00166
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
00178 this->load_chroots = this->load_sessions = true;
00179 break;
00180 case ACTION_HELP:
00181 case ACTION_VERSION:
00182
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
00188
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
00202
00203 if (!this->chroots.empty())
00204 this->load_chroots = this->load_sessions = true;
00205 else if (!all_used())
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:
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 }