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 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
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
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
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
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
00187 if (this->action == ACTION_SESSION_AUTO)
00188 {
00189
00190 this->load_chroots = true;
00191 this->load_sessions = false;
00192 this->all = this->all_sessions = false;
00193
00194
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
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
00213 this->load_chroots = this->load_sessions = true;
00214 }
00215 else if (this->action == ACTION_HELP ||
00216 this->action == ACTION_VERSION)
00217 {
00218
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
00225
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
00240
00241 if (!this->chroots.empty())
00242 this->load_chroots = this->load_sessions = true;
00243 else if (!all_used())
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
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 }