Là một dự án backend, jarro2783/cxxopts đã đạt 4.8k sao trên GitHub, ngôn ngữ C++. Lightweight C++ command line option parser
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Note that master is generally a work in progress, and you probably want to use a
tagged release version.
Version 3 breaking changes
If you have used version 2, there are a couple of breaking changes in version 3
that you should be aware of. If you are new to cxxopts you can skip this
section.
The parser no longer modifies its arguments, so you can pass a const argc and
argv and expect them not to be changed.
The ParseResult object no longer depends on the parser. So it can be returned
from a scope outside the parser and still work. Now that the inputs are not
modified, ParseResult stores a list of the unmatched arguments. These are
retrieved like follows:
auto result = options.parse(argc, argv);
result.unmatched(); // get the unmatched arguments
Quick start
This is a lightweight C++ option parser library, supporting the standard GNU
style syntax for options.
Options can be given as:
--long
--long=argument
--long argument
-a
-ab
-abc argument
where c takes an argument, but a and b do not.
Additionally, anything after -- will be parsed as a positional argument.
Basics
#include <cxxopts.hpp>
Create a cxxopts::Options instance.
cxxopts::Options options("MyProgram", "One line description of MyProgram");
Options are declared with a long and an optional short option. A description
must be provided. The third argument is the value, if omitted it is boolean.
Any type can be given as long as it can be parsed, with operator>>.
To parse the command line do:
auto result = options.parse(argc, argv);
To retrieve an option use result.count("option") to get the number of times
it appeared, and
result["opt"].as<type>()
to get its value. If "opt" doesn't exist, or isn't of the right type, then an
exception will be thrown.
Unrecognised arguments
You can allow unrecognised arguments to be skipped. This applies to both
positional arguments that are not parsed into another option, and --
arguments that do not match an argument that you specify. This is done by
calling:
options.allow_unrecognised_options();
and in the result object they are retrieved with:
result.unmatched()
Exceptions
Exceptional situations throw C++ exceptions. There are two types of
exceptions: errors defining the options, and errors when parsing a list of
arguments. All exceptions derive from cxxopts::exceptions::exception. Errors
defining options derive from cxxopts::exceptions::specification and errors
parsing arguments derive from cxxopts::exceptions::parsing.
All exceptions define a what() function to get a printable string
explaining the error.
Help groups
Options can be placed into groups for the purposes of displaying help messages.
To place options in a group, pass the group as a string to add_options. Then, when displaying the help, help by default prints help for all groups. If you want to only print help for a specific group, pass the groups that you would like displayed as a vector to the help function.
Positional Arguments
Positional arguments are those given without a preceding flag and can be used
alongside non-positional arguments. There may be multiple positional arguments,
and the final positional argument may be a container type to hold a list of all
remaining positionals.
To set up positional arguments, first declare the options, then configure a
set of those arguments as positional like:
options.add_options()
("script", "The script file to execute", cxxopts::value<std::string>())
("server", "The server to execute on", cxxopts::value<std::string>())
("filenames", "The filename(s) to process", cxxopts::value<std::vector<std::string>>());
options.parse_positional({"script", "server", "filenames"});
// Parse options the usual way
options.parse(argc, argv);
Note : parse_positional defaults to replacing the positionals with the arguments provided in the function call. To append to the existing list of positionals, please use cxxopts::PositionalMode::Append.
An implicit value is the value that an option takes when it is given on the
command line without an argument. The following specifies an implicit value:
If an option had both, then not specifying it would give the value "value",
writing it on the command line as --option would give the value "implicit",
and writing --option=another would give it the value "another".
Note that if option has an implicit value, specifying --option another will not work. You must use = syntax - --option=another or -o=another(assumming o is a short name for the option). This is because no argument is required. Hence, there is no good way to determine if the next string is an argument to your option or a positional argument.
Note that the default and implicit value is always stored as a string,
regardless of the type that you want to store it in. It will be parsed as
though it was given on the command line.
Default values are not counted by Options::count.
Implicit values with disabled arguments
You can specify an option for which an argument cannot be specified using below syntax.
In this case specifying option as --option=<value> is disallowed and will throw specified_disabled_args exception. Note that --option value is not supported for implicit values. It is treated as positional/unmatched.
Boolean values
Boolean options have a default implicit value of "true", which can be
overridden. The effect is that writing -o by itself will set option o to
true. However, they can also be written with various strings using =value.
There is no way to disambiguate positional arguments from the value following
a boolean, so we have chosen that they will be positional arguments, and
therefore, -o false does not work.
std::vector<T> values
Parsing a list of values into a std::vector<T> is also supported, as long as T
can be parsed. To separate single values in a list the define symbol CXXOPTS_VECTOR_DELIMITER
is used, which is ',' by default. Ensure that you use no whitespaces between values
(unless the entire list is double quoted) because those would be interpreted as the
next command line option. Example for a command line option
that can be parsed as a std::vector<double>:
--my_list=1,-2.1,3,4.5
If the list of values is quoted, then spaces can be included. For example:
This will be parsed into review, memory sanitize, build help, and reformat.
Options specified multiple times
The same option can be specified several times, with different arguments, which will all
be recorded in order of appearance. An example:
--use train --use bus --use ferry
this is supported through the use of a vector of value for the option:
options.add_options()
("use", "Usable means of transport", cxxopts::value<std::vector<std::string>>())
Custom help
The string after the program name on the first line of the help can be
completely replaced by calling options.custom_help. Note that you might
also want to override the positional help by calling options.positional_help.
Example
Putting all together:
#include <iostream>
#include "cxxopts.hpp"
int main(int argc, char** argv)
{
cxxopts::Options options("test", "A brief description");
options.add_options()
("b,bar", "Param bar", cxxopts::value<std::string>())
("d,debug", "Enable debugging", cxxopts::value<bool>()->default_value("false"))
("f,foo", "Param foo", cxxopts::value<int>()->default_value("10"))
("h,help", "Print usage")
;
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help() << std::endl;
exit(0);
}
bool debug = result["debug"].as<bool>();
std::string bar;
if (result.count("bar"))
bar = result["bar"].as<std::string>();
int foo = result["foo"].as<int>();
return 0;
}
Linking
This is a header only library.
Requirements
The only build requirement is a C++ compiler that supports C++11 features such as:
regex
constexpr
default constructors
GCC >= 4.9 or clang >= 3.1 with libc++ are known to work.
jarro2783/cxxopts có 4.8k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/jarro2783/cxxopts. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
jarro2783/cxxopts có những chủ đề gì?
GitHub topics của jarro2783/cxxopts: "c-plus-plus", "option-parser", "positional-arguments". TopGit xếp repo vào nhóm Backend.
jarro2783/cxxopts còn đang phát triển không?
Commit gần nhất trên jarro2783/cxxopts là 1 tháng trước (theo timestamp GitHub). Repo có 652 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
jarro2783/cxxopts là gì?
jarro2783/cxxopts (jarro2783/cxxopts) là dự án C++ trên GitHub. Theo mô tả gốc: Lightweight C++ command line option parser
jarro2783/cxxopts so với các dự án Backend khác thế nào?
jarro2783/cxxopts được TopGit xếp vào nhóm Backend, với 4.8k sao GitHub và viết bằng C++. Xem trang chủ đề Backend trên TopGit để so sánh với các dự án tương tự theo số sao và mức độ hoạt động.
jarro2783/cxxopts viết bằng ngôn ngữ gì?
jarro2783/cxxopts chủ yếu viết bằng C++. Trường "language" của GitHub dựa trên phần lớn byte ở nhánh mặc định.
Vì sao jarro2783/cxxopts được xếp vào nhóm Backend?
TopGit xếp jarro2783/cxxopts vào nhóm Backend dựa trên GitHub topics và mô tả của repo (gắn thẻ: "c-plus-plus", "option-parser", "positional-arguments"). Việc phân loại dựa trên metadata thật của repo, không phải đoán theo cảm tính biên tập.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về cxxopts?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về cxxopts.