yat/utility/Option.cc

Code
Comments
Other
Rev Date Author Line
965 11 Oct 07 peter 1 // $Id$
965 11 Oct 07 peter 2
965 11 Oct 07 peter 3 /*
4359 23 Aug 23 peter 4   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 5   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 6   Copyright (C) 2009, 2010, 2012 Peter Johansson
965 11 Oct 07 peter 7
1437 25 Aug 08 peter 8   This file is part of the yat library, http://dev.thep.lu.se/yat
965 11 Oct 07 peter 9
965 11 Oct 07 peter 10   The yat library is free software; you can redistribute it and/or
965 11 Oct 07 peter 11   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 12   published by the Free Software Foundation; either version 3 of the
965 11 Oct 07 peter 13   License, or (at your option) any later version.
965 11 Oct 07 peter 14
965 11 Oct 07 peter 15   The yat library is distributed in the hope that it will be useful,
965 11 Oct 07 peter 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
965 11 Oct 07 peter 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
965 11 Oct 07 peter 18   General Public License for more details.
965 11 Oct 07 peter 19
965 11 Oct 07 peter 20   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 21   along with yat. If not, see <http://www.gnu.org/licenses/>.
965 11 Oct 07 peter 22 */
965 11 Oct 07 peter 23
2881 18 Nov 12 peter 24 #include <config.h>
2881 18 Nov 12 peter 25
965 11 Oct 07 peter 26 #include "Option.h"
965 11 Oct 07 peter 27 #include "CommandLine.h"
2210 05 Mar 10 peter 28 #include "Exception.h"
965 11 Oct 07 peter 29
965 11 Oct 07 peter 30 #include <iostream>
965 11 Oct 07 peter 31 #include <sstream>
965 11 Oct 07 peter 32 #include <string>
965 11 Oct 07 peter 33
965 11 Oct 07 peter 34 namespace theplu {
965 11 Oct 07 peter 35 namespace yat {
965 11 Oct 07 peter 36 namespace utility {
965 11 Oct 07 peter 37
965 11 Oct 07 peter 38
965 11 Oct 07 peter 39   Option::Option(CommandLine& cmd, std::string flag, std::string desc)
980 22 Oct 07 peter 40     : cmd_(cmd), description_(desc), present_(false)
965 11 Oct 07 peter 41   {
965 11 Oct 07 peter 42     if (flag.empty())
2210 05 Mar 10 peter 43       throw runtime_error("yat: Option: given flag is empty");
965 11 Oct 07 peter 44     if (flag.size()==1 || (flag.size()==2 && flag[1]==','))
965 11 Oct 07 peter 45       short_name_ = flag[0];
965 11 Oct 07 peter 46     else if (flag[1]==','){
965 11 Oct 07 peter 47       short_name_ = flag[0];
965 11 Oct 07 peter 48       long_name_ = flag.substr(2);
965 11 Oct 07 peter 49       if (long_name_.size()==1)
965 11 Oct 07 peter 50         long_name_="";
965 11 Oct 07 peter 51     }
965 11 Oct 07 peter 52     else {
965 11 Oct 07 peter 53       short_name_ = '\0';
965 11 Oct 07 peter 54       long_name_=flag;
965 11 Oct 07 peter 55     }
965 11 Oct 07 peter 56     cmd.add(*this);
965 11 Oct 07 peter 57   }
965 11 Oct 07 peter 58
965 11 Oct 07 peter 59
965 11 Oct 07 peter 60   Option::~Option(void)
965 11 Oct 07 peter 61   {
965 11 Oct 07 peter 62   }
965 11 Oct 07 peter 63
965 11 Oct 07 peter 64
980 22 Oct 07 peter 65   const CommandLine& Option::cmd(void) const
980 22 Oct 07 peter 66   {
980 22 Oct 07 peter 67     return cmd_;
980 22 Oct 07 peter 68   }
980 22 Oct 07 peter 69
980 22 Oct 07 peter 70
965 11 Oct 07 peter 71   std::string Option::description(void) const
965 11 Oct 07 peter 72   {
965 11 Oct 07 peter 73     return description_;
965 11 Oct 07 peter 74   }
965 11 Oct 07 peter 75
965 11 Oct 07 peter 76
2348 13 Nov 10 peter 77   void Option::description(const std::string& description)
2348 13 Nov 10 peter 78   {
2348 13 Nov 10 peter 79     description_ = description;
2348 13 Nov 10 peter 80   }
2348 13 Nov 10 peter 81
2348 13 Nov 10 peter 82
965 11 Oct 07 peter 83   std::string Option::long_name(void) const
965 11 Oct 07 peter 84   {
965 11 Oct 07 peter 85     return long_name_;
965 11 Oct 07 peter 86   }
965 11 Oct 07 peter 87
965 11 Oct 07 peter 88
4200 19 Aug 22 peter 89   void Option::parse(std::vector<std::string>::iterator& first,
1465 02 Sep 08 peter 90                      const std::vector<std::string>::iterator& last)
965 11 Oct 07 peter 91   {
965 11 Oct 07 peter 92     present_=true;
965 11 Oct 07 peter 93     do_parse(first, last);
965 11 Oct 07 peter 94   }
965 11 Oct 07 peter 95
965 11 Oct 07 peter 96
965 11 Oct 07 peter 97   bool Option::present(void) const
965 11 Oct 07 peter 98   {
1741 22 Jan 09 peter 99     if (!cmd().parsed()) {
2210 05 Mar 10 peter 100       std::string s("theplu::yat::utility::Option::present");
2210 05 Mar 10 peter 101       s += " called before Commandline was parsed";
1741 22 Jan 09 peter 102       throw std::logic_error(s);
1741 22 Jan 09 peter 103     }
965 11 Oct 07 peter 104     return present_;
965 11 Oct 07 peter 105   }
965 11 Oct 07 peter 106
965 11 Oct 07 peter 107
965 11 Oct 07 peter 108   std::string Option::print(void)
965 11 Oct 07 peter 109   {
965 11 Oct 07 peter 110     return print1()+print2()+print3()+std::string("\t")+print4();
965 11 Oct 07 peter 111   }
965 11 Oct 07 peter 112
965 11 Oct 07 peter 113
965 11 Oct 07 peter 114   std::string Option::print1(void) const
965 11 Oct 07 peter 115   {
986 22 Oct 07 peter 116     std::string str;
986 22 Oct 07 peter 117     if (short_name()){
986 22 Oct 07 peter 118       str = std::string("-")+short_name();
986 22 Oct 07 peter 119       if (!long_name().empty())
986 22 Oct 07 peter 120         str += std::string(",");
986 22 Oct 07 peter 121     }
986 22 Oct 07 peter 122     else
986 22 Oct 07 peter 123       str = std::string("   ");
986 22 Oct 07 peter 124     return str;
965 11 Oct 07 peter 125   }
965 11 Oct 07 peter 126
4200 19 Aug 22 peter 127
965 11 Oct 07 peter 128   std::string Option::print2(void) const
965 11 Oct 07 peter 129   {
4200 19 Aug 22 peter 130     if (long_name().size())
965 11 Oct 07 peter 131       return std::string(" --")+long_name();
965 11 Oct 07 peter 132     return std::string();
965 11 Oct 07 peter 133   }
965 11 Oct 07 peter 134
965 11 Oct 07 peter 135
965 11 Oct 07 peter 136   std::string Option::print3(void) const
965 11 Oct 07 peter 137   {
965 11 Oct 07 peter 138     return std::string();
965 11 Oct 07 peter 139   }
965 11 Oct 07 peter 140
965 11 Oct 07 peter 141
965 11 Oct 07 peter 142   std::string Option::print4(void) const
965 11 Oct 07 peter 143   {
965 11 Oct 07 peter 144     return description_;
965 11 Oct 07 peter 145   }
965 11 Oct 07 peter 146
965 11 Oct 07 peter 147
965 11 Oct 07 peter 148   void Option::reset(void)
965 11 Oct 07 peter 149   {
965 11 Oct 07 peter 150     present_=false;
965 11 Oct 07 peter 151   }
965 11 Oct 07 peter 152
965 11 Oct 07 peter 153
965 11 Oct 07 peter 154   char Option::short_name(void) const
965 11 Oct 07 peter 155   {
965 11 Oct 07 peter 156     return short_name_;
965 11 Oct 07 peter 157   }
965 11 Oct 07 peter 158
965 11 Oct 07 peter 159
965 11 Oct 07 peter 160   void Option::validate()
965 11 Oct 07 peter 161   {
965 11 Oct 07 peter 162     do_validate();
965 11 Oct 07 peter 163   }
965 11 Oct 07 peter 164
965 11 Oct 07 peter 165 }}} // of namespace utility, yat, and theplu