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-base-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_base;
00035
00037 const options::action_type options::ACTION_HELP ("help");
00039 const options::action_type options::ACTION_VERSION ("version");
00040
00041 options::options ():
00042 action(),
00043 quiet(false),
00044 verbose(false),
00045 actions(_("Actions")),
00046 general(_("General options")),
00047 hidden(_("Hidden options")),
00048 positional(),
00049 visible(),
00050 global(),
00051 vm()
00052 {
00053 }
00054
00055 options::~options ()
00056 {
00057 }
00058
00059 boost::program_options::options_description const&
00060 options::get_visible_options() const
00061 {
00062 return this->visible;
00063 }
00064
00065 void
00066 options::parse (int argc,
00067 char *argv[])
00068 {
00069 add_options();
00070 add_option_groups();
00071
00072 opt::store(opt::command_line_parser(argc, argv).
00073 options(global).positional(positional).run(), vm);
00074 opt::notify(vm);
00075
00076 check_options();
00077 check_actions();
00078 }
00079
00080 void
00081 options::add_options ()
00082 {
00083 this->action.add(ACTION_HELP);
00084 this->action.add(ACTION_VERSION);
00085
00086 actions.add_options()
00087 ("help,h",
00088 _("Show help options"))
00089 ("version,V",
00090 _("Print version information"));
00091
00092 general.add_options()
00093 ("quiet,q",
00094 _("Show less output"))
00095 ("verbose,v",
00096 _("Show more output"));
00097
00098 hidden.add_options()
00099 ("debug", opt::value<std::string>(&this->debug_level),
00100 _("Enable debugging messages"));
00101 }
00102
00103 void
00104 options::add_option_groups ()
00105 {
00106 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00107 if (!actions.options().empty())
00108 #else
00109 if (!actions.primary_keys().empty())
00110 #endif
00111 {
00112 visible.add(actions);
00113 global.add(actions);
00114 }
00115 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00116 if (!general.options().empty())
00117 #else
00118 if (!general.primary_keys().empty())
00119 #endif
00120 {
00121 visible.add(general);
00122 global.add(general);
00123 }
00124 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00125 if (!hidden.options().empty())
00126 #else
00127 if (!hidden.primary_keys().empty())
00128 #endif
00129 global.add(hidden);
00130 }
00131
00132 void
00133 options::check_options ()
00134 {
00135 if (vm.count("help"))
00136 this->action = ACTION_HELP;
00137
00138 if (vm.count("version"))
00139 this->action = ACTION_VERSION;
00140
00141 if (vm.count("quiet"))
00142 this->quiet = true;
00143 if (vm.count("verbose"))
00144 this->verbose = true;
00145
00146 if (vm.count("debug"))
00147 {
00148 if (this->debug_level == "none")
00149 sbuild::debug_level = sbuild::DEBUG_NONE;
00150 else if (this->debug_level == "notice")
00151 sbuild::debug_level = sbuild::DEBUG_NOTICE;
00152 else if (this->debug_level == "info")
00153 sbuild::debug_level = sbuild::DEBUG_INFO;
00154 else if (this->debug_level == "warning")
00155 sbuild::debug_level = sbuild::DEBUG_WARNING;
00156 else if (this->debug_level == "critical")
00157 sbuild::debug_level = sbuild::DEBUG_CRITICAL;
00158 else
00159 throw opt::validation_error(_("Invalid debug level"));
00160 }
00161 else
00162 sbuild::debug_level = sbuild::DEBUG_NONE;
00163 }
00164
00165 void
00166 options::check_actions ()
00167 {
00168 }