1 | // $Id: Option.cc 2384 2010-12-22 14:03:36Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2009, 2010 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 | |
---|
31 | namespace theplu { |
---|
32 | namespace yat { |
---|
33 | namespace 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 | void Option::description(const std::string& description) |
---|
75 | { |
---|
76 | description_ = description; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | std::string Option::long_name(void) const |
---|
81 | { |
---|
82 | return long_name_; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | void Option::parse(std::vector<std::string>::iterator& first, |
---|
87 | const std::vector<std::string>::iterator& last) |
---|
88 | { |
---|
89 | present_=true; |
---|
90 | do_parse(first, last); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | bool Option::present(void) const |
---|
95 | { |
---|
96 | if (!cmd().parsed()) { |
---|
97 | std::string s("theplu::yat::utility::Option::present"); |
---|
98 | s += " called before Commandline was parsed"; |
---|
99 | throw std::logic_error(s); |
---|
100 | } |
---|
101 | return present_; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | std::string Option::print(void) |
---|
106 | { |
---|
107 | return print1()+print2()+print3()+std::string("\t")+print4(); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | std::string Option::print1(void) const |
---|
112 | { |
---|
113 | std::string str; |
---|
114 | if (short_name()){ |
---|
115 | str = std::string("-")+short_name(); |
---|
116 | if (!long_name().empty()) |
---|
117 | str += std::string(","); |
---|
118 | } |
---|
119 | else |
---|
120 | str = std::string(" "); |
---|
121 | return str; |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | std::string Option::print2(void) const |
---|
126 | { |
---|
127 | if (long_name().size()) |
---|
128 | return std::string(" --")+long_name(); |
---|
129 | return std::string(); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | std::string Option::print3(void) const |
---|
134 | { |
---|
135 | return std::string(); |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | std::string Option::print4(void) const |
---|
140 | { |
---|
141 | return description_; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | void Option::reset(void) |
---|
146 | { |
---|
147 | present_=false; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | char Option::short_name(void) const |
---|
152 | { |
---|
153 | return short_name_; |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void Option::validate() |
---|
158 | { |
---|
159 | do_validate(); |
---|
160 | } |
---|
161 | |
---|
162 | }}} // of namespace utility, yat, and theplu |
---|