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