source: trunk/lib/yat/Option.cc @ 1039

Last change on this file since 1039 was 1039, checked in by Peter Johansson, 11 years ago

updating to the latest yat. refs #410

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