DCCL v4
Loading...
Searching...
No Matches
cli_option.h
1// Copyright 2014-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24#ifndef CLIOPTION20140107H
25#define CLIOPTION20140107H
26
27#include <getopt.h>
28
29#include <iomanip>
30#include <sstream>
31#include <utility>
32
33namespace dccl
34{
36class Option
37{
38 public:
45 Option(char shortname, const char* longname, int has_argument, std::string description)
46 : description_(std::move(description))
47 {
48 c_opt_.name = longname;
49
50 c_opt_.has_arg = has_argument;
51 c_opt_.flag = nullptr;
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
77 std::string usage() const
78 {
79 std::stringstream usage;
80 if (c_opt_.val != 0)
81 usage << "-" << (char)c_opt_.val << ", ";
82 usage << "--" << c_opt_.name;
83 usage << std::string(std::max(1, (int)(20 - usage.str().size())), ' ') << description_;
84 return usage.str();
85 }
86
88 static void convert_vector(const std::vector<Option>& options, std::vector<option>* c_options,
89 std::string* opt_string)
90 {
91 for (const auto& option : options)
92 {
93 c_options->push_back(option.c_opt());
94 *opt_string += option.opt_code();
95 }
96 option zero = {nullptr, 0, nullptr, 0};
97 c_options->push_back(zero);
98 }
99
100 private:
101 option c_opt_;
102 std::string description_;
103};
104} // namespace dccl
105
106#endif
Represents a command line option.
Definition cli_option.h:37
option c_opt() const
Definition cli_option.h:56
std::string usage() const
Definition cli_option.h:77
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,...
Definition cli_option.h:88
std::string opt_code() const
Definition cli_option.h:59
Option(char shortname, const char *longname, int has_argument, std::string description)
Create a command line option.
Definition cli_option.h:45
Dynamic Compact Control Language namespace.
Definition any.h:47