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

Generated on Mon Jan 21 00:38:28 2008 for schroot by  doxygen 1.5.4