1 | // $Id: Option.cc 1275 2008-04-11 06:10:12Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2008 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Option.h" |
---|
26 | #include "CommandLine.h" |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | #include <sstream> |
---|
30 | #include <stdexcept> |
---|
31 | #include <string> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace utility { |
---|
36 | |
---|
37 | |
---|
38 | Option::Option(CommandLine& cmd, std::string flag, std::string desc) |
---|
39 | : cmd_(cmd), description_(desc), present_(false) |
---|
40 | { |
---|
41 | if (flag.empty()) |
---|
42 | throw std::runtime_error("yat: Option: given flag is empty"); |
---|
43 | if (flag.size()==1 || (flag.size()==2 && flag[1]==',')) |
---|
44 | short_name_ = flag[0]; |
---|
45 | else if (flag[1]==','){ |
---|
46 | short_name_ = flag[0]; |
---|
47 | long_name_ = flag.substr(2); |
---|
48 | if (long_name_.size()==1) |
---|
49 | long_name_=""; |
---|
50 | } |
---|
51 | else { |
---|
52 | short_name_ = '\0'; |
---|
53 | long_name_=flag; |
---|
54 | } |
---|
55 | cmd.add(*this); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | Option::~Option(void) |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | const CommandLine& Option::cmd(void) const |
---|
65 | { |
---|
66 | return cmd_; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | std::string Option::description(void) const |
---|
71 | { |
---|
72 | return description_; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | std::string Option::long_name(void) const |
---|
77 | { |
---|
78 | return long_name_; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void Option::parse(std::vector<std::string>::iterator first, |
---|
83 | std::vector<std::string>::iterator last) |
---|
84 | { |
---|
85 | present_=true; |
---|
86 | do_parse(first, last); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | bool Option::present(void) const |
---|
91 | { |
---|
92 | return present_; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | std::string Option::print(void) |
---|
97 | { |
---|
98 | return print1()+print2()+print3()+std::string("\t")+print4(); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | std::string Option::print1(void) const |
---|
103 | { |
---|
104 | std::string str; |
---|
105 | if (short_name()){ |
---|
106 | str = std::string("-")+short_name(); |
---|
107 | if (!long_name().empty()) |
---|
108 | str += std::string(","); |
---|
109 | } |
---|
110 | else |
---|
111 | str = std::string(" "); |
---|
112 | return str; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | std::string Option::print2(void) const |
---|
117 | { |
---|
118 | if (long_name().size()) |
---|
119 | return std::string(" --")+long_name(); |
---|
120 | return std::string(); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | std::string Option::print3(void) const |
---|
125 | { |
---|
126 | return std::string(); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | std::string Option::print4(void) const |
---|
131 | { |
---|
132 | return description_; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void Option::reset(void) |
---|
137 | { |
---|
138 | present_=false; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | char Option::short_name(void) const |
---|
143 | { |
---|
144 | return short_name_; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | void Option::validate() |
---|
149 | { |
---|
150 | do_validate(); |
---|
151 | } |
---|
152 | |
---|
153 | }}} // of namespace utility, yat, and theplu |
---|