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_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
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
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
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
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
00175 if (this->action == ACTION_SESSION_AUTO)
00176 {
00177
00178 this->load_chroots = true;
00179 this->load_sessions = false;
00180 this->all = this->all_sessions = false;
00181
00182
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
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
00201 this->load_chroots = this->load_sessions = true;
00202 }
00203 else if (this->action == ACTION_HELP ||
00204 this->action == ACTION_VERSION)
00205 {
00206
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
00213
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
00228
00229 if (!this->chroots.empty())
00230 this->load_chroots = this->load_sessions = true;
00231 else if (!all_used())
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
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 }