schroot-base-options.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
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_base;
00034 
00035 options::options ():
00036   quiet(false),
00037   verbose(false),
00038   general(_("General options")),
00039   hidden(_("Hidden options")),
00040   positional(),
00041   visible(),
00042   global(),
00043   vm()
00044 {
00045 }
00046 
00047 options::~options ()
00048 {
00049 }
00050 
00051 boost::program_options::options_description const&
00052 options::get_visible_options() const
00053 {
00054   return this->visible;
00055 }
00056 
00057 void
00058 options::parse (int   argc,
00059                 char *argv[])
00060 {
00061   add_options();
00062   add_option_groups();
00063 
00064   opt::store(opt::command_line_parser(argc, argv).
00065              options(global).positional(positional).run(), vm);
00066   opt::notify(vm);
00067 
00068   check_options();
00069   check_actions();
00070 }
00071 
00072 void
00073 options::add_options ()
00074 {
00075   general.add_options()
00076     ("help,h",
00077      _("Show help options"))
00078     ("version,V",
00079      _("Print version information"))
00080     ("quiet,q",
00081      _("Show less output"))
00082     ("verbose,v",
00083      _("Show more output"));
00084 
00085   hidden.add_options()
00086     ("debug", opt::value<std::string>(&this->debug_level),
00087      _("Enable debugging messages"));
00088 }
00089 
00090 void
00091 options::add_option_groups ()
00092 {
00093 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00094   if (!general.options().empty())
00095 #else
00096   if (!general.primary_keys().empty())
00097 #endif
00098     {
00099       visible.add(general);
00100       global.add(general);
00101     }
00102 #ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD
00103   if (!hidden.options().empty())
00104 #else
00105   if (!hidden.primary_keys().empty())
00106 #endif
00107     global.add(hidden);
00108 }
00109 
00110 void
00111 options::check_options ()
00112 {
00113   if (vm.count("quiet"))
00114     this->quiet = true;
00115   if (vm.count("verbose"))
00116     this->verbose = true;
00117 
00118   if (vm.count("debug"))
00119     {
00120       if (this->debug_level == "none")
00121         sbuild::debug_level = sbuild::DEBUG_NONE;
00122       else if (this->debug_level == "notice")
00123         sbuild::debug_level = sbuild::DEBUG_NOTICE;
00124       else if (this->debug_level == "info")
00125         sbuild::debug_level = sbuild::DEBUG_INFO;
00126       else if (this->debug_level == "warning")
00127         sbuild::debug_level = sbuild::DEBUG_WARNING;
00128       else if (this->debug_level == "critical")
00129         sbuild::debug_level = sbuild::DEBUG_CRITICAL;
00130       else
00131         throw opt::validation_error(_("Invalid debug level"));
00132     }
00133   else
00134     sbuild::debug_level = sbuild::DEBUG_NONE;
00135 }
00136 
00137 void
00138 options::check_actions ()
00139 {
00140 }

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1