c - 如何指定可选标志?

标签 c printing flags

我必须指定这些标志/参数。如果缺少参数,请为标志使用指定的默认值。如果参数无法转换为数值,则使用该参数的默认值。

-eye:如果存在此标志,则接下来的三个参数指定眼睛位置的 x、y 和 z 坐标。如果此标志不存在,则眼睛默认位于位置 <0.0, 0.0, -14.0>。

-view:如果存在此标志,则接下来的六个参数指定 View 矩形的 min_x、max_x、min_y、max_y、宽度和高度组件。如果标志不存在,则 View 矩形默认位置 min_x 为 -10,max_x 为 10,min_y 为 -7.5,max_y 为 7.5,宽度为 1024,高度为 768。

-light:如果存在此标志,则接下来的六个参数指定光位置的 x、y 和 z 坐标以及光颜色的 r、g 和 b 值。如果标志不存在,则灯光默认位置 <-100.0, 100.0, -100.0> 和颜色/强度 (1.5, 1.5, 1.5)。

-ambient:如果存在此标志,则接下来的三个参数指定环境光颜色的 r、g 和 b 值。如果该标志不存在,则环境光颜色默认为 (1.0, 1.0, 1.0)。

我在想,我是否必须为每个标志创建一个函数?到目前为止,我对如何将其合并到我的代码中感到有点困惑:

#include "commandline.h"

int main(int argc, char const *argv[])
{
   FILE *input;

   input = fopen(argv[1], "r");

   if (input == NULL)
   {
      perror("Error\n");
   }
}

最佳答案

你在找这个吗:

if(argc < NoOfArgumentNeeded){
 fprintf(stderr, "Usage: %s message string", argv[0]);
 return EXIT_FAILURE;
}

您可以编写单独的Usage(),这里有一些很好的例子:(1) A Little C Primer/C Command Line Arguments , (2) Command Line Parameter Parsing

编辑:

当你想打印错误信息时,Stderr 很有用

Stderr :

Standard error stream: The standard error stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed by default to the text console (generally, on the screen).

stderr can be used as an argument for any function that takes an argument of type FILE* expecting an output stream, like fputs or fprintf.

关于c - 如何指定可选标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16746898/

相关文章:

c++ - 初始化 pthread 互斥锁

c - C 中指向数组的指针作为函数参数

c - 叶节点的度数是多少?

JQuery 窗口打印功能问题

c# - 如何保存和恢复 `PrinterSettings` ?

c - C数组的打印地址

php - 在javascript中打印页面

C++ 独立列出所有枚举标志

go - 我如何使用标志来选择我想调用的函数

c# - 如何在 C# 中比较标志?