libutil++  1.9.3
 All Classes Functions Variables
CommandLineOption.cpp
1 /*
2 ** libutil++
3 ** $Id: CommandLineOption.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: CommandLineOption.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "CommandLineOption.h"
12 
13 #include <stdio.h>
14 
15 #include <string>
16 
17 using namespace sella::util;
18 
19 CommandLineOption::CommandLineOption(char opt, const std::string &option, const std::string &usage, const std::string &label, const std::string &value) throw () :
20  opt(opt),
21  option(option),
22 
23  usage(usage),
24  value(value),
25 
26  argumented(label.size() > 0)
27 {
28  char buffer[1024];
29 
30  if (((opt >= 'a') && (opt <= 'z')) || ((opt >= 'A') && (opt <= 'Z')) || ((opt >= '0') && (opt <= '9'))) {
31  if (this->argumented) {
32  snprintf(buffer, sizeof(buffer), " -%c,--%s=%s", opt, option.c_str(), label.c_str());
33  } else {
34  snprintf(buffer, sizeof(buffer), " -%c,--%s", opt, option.c_str());
35  }
36  } else {
37  if (this->argumented) {
38  snprintf(buffer, sizeof(buffer), " --%s=%s", option.c_str(), label.c_str());
39  } else {
40  snprintf(buffer, sizeof(buffer), " --%s", option.c_str());
41  }
42  }
43  this->tags = buffer;
44 }
45 
46 CommandLineOption::CommandLineOption() throw () :
47  opt(),
48  option(),
49 
50  usage(),
51  value(),
52 
53  tags(),
54 
55  argumented(false)
56 { }
57 
58 CommandLineOption::CommandLineOption(const CommandLineOption &copy) throw () :
59  opt(copy.getOpt()),
60  option(copy.getOption()),
61 
62  usage(copy.getUsage()),
63  value(copy.getValue()),
64 
65  tags(copy.getTags()),
66 
67  argumented(copy.getArgumented())
68 { }
69 
70 CommandLineOption::~CommandLineOption() throw () {
71  this->option.clear();
72  this->usage.clear();
73  this->value.clear();
74  this->tags.clear();
75 }
76 
77 CommandLineOption & CommandLineOption::operator = (const CommandLineOption &other) throw () {
78  if (this != &other) {
79  this->setOpt(other.getOpt());
80  this->setOption(other.getOption());
81 
82  this->setUsage(other.getUsage());
83  this->setValue(other.getValue());
84 
85  this->setTags(other.getTags());
86 
87  this->setArgumented(other.getArgumented());
88  }
89  return *this;
90 }
91 
92 void CommandLineOption::setOpt(char opt) throw () {
93  this->opt = opt;
94 }
95 
96 void CommandLineOption::setOption(const std::string &option) throw () {
97  this->option = option;
98 }
99 
100 void CommandLineOption::setUsage(const std::string &usage) throw () {
101  this->usage = usage;
102 }
103 
104 void CommandLineOption::setValue(const std::string &value) throw () {
105  this->value = value;
106 }
107 
108 void CommandLineOption::setTags(const std::string &tags) throw () {
109  this->tags = tags;
110 }
111 
112 void CommandLineOption::setArgumented(bool value) throw () {
113  this->argumented = value;
114 }
115 
116 char CommandLineOption::getOpt() const throw () {
117  return this->opt;
118 }
119 
120 const std::string & CommandLineOption::getOption() const throw () {
121  return this->option;
122 }
123 
124 const std::string & CommandLineOption::getUsage() const throw () {
125  return this->usage;
126 }
127 
128 const std::string & CommandLineOption::getValue() const throw () {
129  return this->value;
130 }
131 
132 const std::string & CommandLineOption::getTags() const throw () {
133  return this->tags;
134 }
135 
136 bool CommandLineOption::getArgumented() const throw () {
137  return this->argumented;
138 }
139 
140 /*
141 ** vim: noet ts=3 sw=3
142 */