DCCL v3
cli_option.h
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CLIOPTION20140107H
23 #define CLIOPTION20140107H
24 
25 #include <getopt.h>
26 
27 #include <sstream>
28 #include <iomanip>
29 
30 namespace dccl
31 {
33  class Option
34  {
35  public:
42  Option(char shortname,
43  const char* longname,
44  int has_argument,
45  const std::string& description)
46  : description_(description)
47  {
48  c_opt_.name = longname;
49 
50  c_opt_.has_arg = has_argument;
51  c_opt_.flag = 0;
52  c_opt_.val = shortname;
53  }
54 
56  option c_opt() const { return c_opt_; }
57 
59  std::string opt_code() const
60  {
61  std::string opt_code;
62  if(c_opt_.val == 0)
63  return opt_code;
64 
65  opt_code += std::string(1, c_opt_.val);
66  if(c_opt_.has_arg == no_argument)
67  return opt_code;
68  else if(c_opt_.has_arg == required_argument)
69  return opt_code + ":";
70  else if(c_opt_.has_arg == optional_argument)
71  return opt_code + "::";
72  else
73  return "";
74 
75  }
76 
78  std::string usage() const
79  {
80  std::stringstream usage;
81  if(c_opt_.val != 0)
82  usage << "-" << (char)c_opt_.val << ", ";
83  usage << "--" << c_opt_.name;
84  usage << std::string(std::max(1, (int)(20-usage.str().size())), ' ') << description_;
85  return usage.str();
86  }
87 
89  static void convert_vector(const std::vector<Option>& options, std::vector<option>* c_options, std::string* opt_string)
90  {
91  for(int i = 0, n = options.size(); i < n; ++i)
92  {
93  c_options->push_back(options[i].c_opt());
94  *opt_string += options[i].opt_code();
95  }
96  option zero = { 0, 0, 0, 0 };
97  c_options->push_back(zero);
98  }
99 
100  private:
101  option c_opt_;
102  std::string description_;
103  };
104 }
105 
106 #endif
107 
option c_opt() const
Definition: cli_option.h:56
static void convert_vector(const std::vector< Option > &options, std::vector< option > *c_options, std::string *opt_string)
Convert a vector of Options into a vector of options (from getopt.h) and an opt_string, suitable for use in getopt_long()
Definition: cli_option.h:89
Option(char shortname, const char *longname, int has_argument, const std::string &description)
Create a command line option.
Definition: cli_option.h:42
std::string opt_code() const
Definition: cli_option.h:59
std::string usage() const
Definition: cli_option.h:78
Dynamic Compact Control Language namespace.
Represents a command line option.
Definition: cli_option.h:33