接受Stdin或CLI参数的设计模式
Design pattern for accepting stdin OR CLI arguments
对于C ,我如何接受CLI参数或 stdin?
例如,假设我有一个函数foo()
,我想调用一个可变数量的参数。对于标准ARG,我只使用以下内容:
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "usage goes here.n";
} else {
for (int i; i < argc; ++i) {
foo(argv[i]);
}
}
}
但是,如果他们通过stdin将它们发送给我并将参数运送到我的应用程序怎么办?有没有办法检测和接受/处理/处理两者?在现代C (C 11及以后)中,有效的设计模式是什么?
?我对设计模式/示例实现感兴趣。请随时参考执行此操作的库(Boost?),但请分享/解释一个示例实现。
通常,您只会从stdin读取输入,而不是参数/选项。通过阅读和评估参数/选项,该程序应确定该程序是否期望来自STDIN或E.G.的输入。文件参数。
作为GREP的手册的例如:
概要
grep [options]模式[file ...]
描述
GREP搜索命名命名的命名输入文件(或标准输入命名,或者给出了单个连字符>连字符( - )作为文件名)包含与给定模式匹配的行。
缺少文件参数或 - 选项指示 grep 读取stdin。
您的程序的调用可能看起来像这样,缺乏文件参数表明从stdin读取输入:
# file argument, input is in the file
command -o someoption filename
# file content supplied via stdin
command -o someoption < filename
# with pipe and - (stdin) as file argument
othercommand | command -o someoption -
用于解析选项/参数Boost具有程序选项库
相关文章:
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 资源管理设计模式
- 用于在回调中调用解析器的设计模式
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 在这种情况下我应该使用哪种设计模式
- C++中物体改变识别的设计模式?
- 确保所有构造函数调用相同的函数 c++ 设计模式
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 工厂设计模式优化
- 使用C++模板的数据映射器设计模式
- 什么是包裹着色器参数值的类的好设计模式
- 接受Stdin或CLI参数的设计模式
- 依赖注入/继承设计模式的构造函数参数太多
- 是否有推荐的设计模式用于与过程通信参数
- 如何在PIMPL设计模式中调用参数化构造函数
- 初始化算法的设计模式,每个算法接受不同的参数
- 用于分配器类型参数的C++设计模式
- 设计模式- c++运行时成员参数的实例化
- 传递大量参数的c++设计模式