紫红色 OS fxl::CommandLineFromArgcArgv() 用法?
Fuchsia OS fxl::CommandLineFromArgcArgv() usage?
Fuchsia OS > Guides > Syslog 有这个例子:
#include "src/lib/fsl/syslogger/init.h"
#include "src/lib/fxl/command_line.h"
int main(int argc, char** argv) {
auto command_line = fxl::CommandLineFromArgcArgv(argc, argv);
fsl::InitLoggerFromCommandLine(command_line, {"my_program"});
}
Fuchsia OS > Reference 没有列出 FXL:
对 Fuchsia Project site 的搜索还有一个包含 fxl::CommandLineFromArgcArgv()
的示例。
我在哪里可以找到有关 fxl::CommandLineFromArgcArgv()
的参数、return 值和其他详细信息?
fxl
API 未记录为官方 Fuchsia 参考的一部分(尚未)。
来自 fxl
目录 (link) 中的自述文件:
In an ideal world, FXL wouldn‘t exist and we could use the C++ standard library’s building blocks. [...] We‘d like to keep FXL small and focused on the problem of “fixing” the C++ standard library, which means you probably shouldn’t put your thing in FXL unless it is related to a particular deficiency of the C++ standard library.
根据这个说法,似乎 fxl
没有设置为 long-term 项目,而是打算成为 empty/obsolete,当 C++ 标准库已经足够改编。可能由于这个原因,文档工作受到限制。
我们要依赖直接在header(link)中提供的文档:
// Builds a |CommandLine| from the usual argc/argv.
inline CommandLine CommandLineFromArgcArgv(int argc, const char* const* argv)
CommandLine
class的定义与header相同。根据评论,它区分了可选参数和位置参数。可选参数的形式为 --key=value
或 --key
(没有值),但不是 --key value
。位置参数以不是这种形式的第一个参数开始(或特殊的 --
分隔符)。
CommandLine
成员函数是:
访问程序名(来自argv[0]
):
bool has_argv0() const;
const std::string& argv0() const;
访问可选参数和位置参数(Option
是一个包含成员 std::string name
/ std::string value
的简单结构):
const std::vector<Option>& options() const;
const std::vector<std::string>& positional_args() const;
比较:
bool operator==(const CommandLine& other) const;
bool operator!=(const CommandLine& other) const;
访问可选参数:
bool HasOption(StringView name, size_t* index = nullptr) const;
bool GetOptionValue(StringView name, std::string* value) const;
std::vector<StringView> GetOptionValues(StringView name) const;
std::string GetOptionValueWithDefault(StringView name, StringView default_value) const;
我们可以编写如下示例程序(使用structured-binding语法):
#include <iostream>
#include "src/lib/fxl/command_line.h"
int main(int argc, char** argv) {
const auto cl = fxl::CommandLineFromArgcArgv(argc, argv);
std::cout << "Program name = " << cl.argv0() << std::endl;
std::cout << "Optional: " << cl.options().size() << std::endl;
for (const auto& [name,value] : cl.options()) {
std::cout << name << " -> " << value << std::endl;
}
std::cout << "Positional: " << cl.positional_args().size() << std::endl;
for (const auto& arg : cl.positional_args()) {
std::cout << arg << std::endl;
}
return 0;
}
编译程序后(根据this答案),我们可以得到如下输出(演示第一个位置参数filename
如何将后面的所有参数变成位置参数):
$ hello_world_cpp --k1=v1 --k2 --k3=v3 filename --k4=v4
Program name = hello_world_cpp
Optional: 3
k1 -> v1
k2 ->
k3 -> v3
Positional: 2
filename
--k4=v4
演示 --
作为分隔符:
$ hello_world_cpp --k1=v1 -- --k2=v2
Program name = hello_world_cpp
Optional: 1
k1=v1
Positional: 1
--k2=v2
我们可以使用 HasOption
:
进行简单的参数解析
size_t index;
if (cl.HasOption("key", &index)) {
handle_key(cl.options.at(index).value);
}
将其添加到我们的程序中并使用 --key=abc
调用它会将 abc
传递给 handle_key
。
Fuchsia OS > Guides > Syslog 有这个例子:
#include "src/lib/fsl/syslogger/init.h"
#include "src/lib/fxl/command_line.h"
int main(int argc, char** argv) {
auto command_line = fxl::CommandLineFromArgcArgv(argc, argv);
fsl::InitLoggerFromCommandLine(command_line, {"my_program"});
}
Fuchsia OS > Reference 没有列出 FXL:
对 Fuchsia Project site 的搜索还有一个包含 fxl::CommandLineFromArgcArgv()
的示例。
我在哪里可以找到有关 fxl::CommandLineFromArgcArgv()
的参数、return 值和其他详细信息?
fxl
API 未记录为官方 Fuchsia 参考的一部分(尚未)。
来自 fxl
目录 (link) 中的自述文件:
In an ideal world, FXL wouldn‘t exist and we could use the C++ standard library’s building blocks. [...] We‘d like to keep FXL small and focused on the problem of “fixing” the C++ standard library, which means you probably shouldn’t put your thing in FXL unless it is related to a particular deficiency of the C++ standard library.
根据这个说法,似乎 fxl
没有设置为 long-term 项目,而是打算成为 empty/obsolete,当 C++ 标准库已经足够改编。可能由于这个原因,文档工作受到限制。
我们要依赖直接在header(link)中提供的文档:
// Builds a |CommandLine| from the usual argc/argv.
inline CommandLine CommandLineFromArgcArgv(int argc, const char* const* argv)
CommandLine
class的定义与header相同。根据评论,它区分了可选参数和位置参数。可选参数的形式为 --key=value
或 --key
(没有值),但不是 --key value
。位置参数以不是这种形式的第一个参数开始(或特殊的 --
分隔符)。
CommandLine
成员函数是:
访问程序名(来自
argv[0]
):bool has_argv0() const; const std::string& argv0() const;
访问可选参数和位置参数(
Option
是一个包含成员std::string name
/std::string value
的简单结构):const std::vector<Option>& options() const; const std::vector<std::string>& positional_args() const;
比较:
bool operator==(const CommandLine& other) const; bool operator!=(const CommandLine& other) const;
访问可选参数:
bool HasOption(StringView name, size_t* index = nullptr) const; bool GetOptionValue(StringView name, std::string* value) const; std::vector<StringView> GetOptionValues(StringView name) const; std::string GetOptionValueWithDefault(StringView name, StringView default_value) const;
我们可以编写如下示例程序(使用structured-binding语法):
#include <iostream>
#include "src/lib/fxl/command_line.h"
int main(int argc, char** argv) {
const auto cl = fxl::CommandLineFromArgcArgv(argc, argv);
std::cout << "Program name = " << cl.argv0() << std::endl;
std::cout << "Optional: " << cl.options().size() << std::endl;
for (const auto& [name,value] : cl.options()) {
std::cout << name << " -> " << value << std::endl;
}
std::cout << "Positional: " << cl.positional_args().size() << std::endl;
for (const auto& arg : cl.positional_args()) {
std::cout << arg << std::endl;
}
return 0;
}
编译程序后(根据this答案),我们可以得到如下输出(演示第一个位置参数filename
如何将后面的所有参数变成位置参数):
$ hello_world_cpp --k1=v1 --k2 --k3=v3 filename --k4=v4
Program name = hello_world_cpp
Optional: 3
k1 -> v1
k2 ->
k3 -> v3
Positional: 2
filename
--k4=v4
演示 --
作为分隔符:
$ hello_world_cpp --k1=v1 -- --k2=v2
Program name = hello_world_cpp
Optional: 1
k1=v1
Positional: 1
--k2=v2
我们可以使用 HasOption
:
size_t index;
if (cl.HasOption("key", &index)) {
handle_key(cl.options.at(index).value);
}
将其添加到我们的程序中并使用 --key=abc
调用它会将 abc
传递给 handle_key
。