- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我开始自学 C。我在这里和那里遇到了一些颠簸,但现在我被 getOpt()
难住了。给我带来麻烦的主要事情是当我试图使某些标志依赖于其他标志时。例如我希望这些工作:
./myfile -a -b -c blue
但是没有-a,其他选项都无法工作。因此 ./myfile -b -c purple
将无效。 getopt 是否有可能处理这种“标志相关”标准?我该怎么做呢?其次,比方说,无论传递什么旗帜,都必须伴随着一种颜色。
所以 ./myfile -a -b green
和 ./myfile red
都是有效的。我知道这一切都在 getOpt() 的选项参数内(目前设置为看起来像这样的“abc”),但是我如何在不执行“a:b:c:”的情况下为每个实例设置一个参数: "因为如果没有传递标志,这将不包括强制性颜色。
最佳答案
这是 getopt
的示例(来自联机帮助页):
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int
main (int argc, char *argv[])
{
int flags, opt;
int nsecs, tfnd;
nsecs = 0;
tfnd = 0;
flags = 0;
while ((opt = getopt (argc, argv, "nt:")) != -1)
{
switch (opt)
{
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi (optarg);
tfnd = 1;
break;
default: /* '?' */
fprintf (stderr, "Usage: %s [-t nsecs] [-n] name\n", argv[0]);
exit (EXIT_FAILURE);
}
}
printf ("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);
if (optind >= argc)
{
fprintf (stderr, "Expected argument after options\n");
exit (EXIT_FAILURE);
}
printf ("name argument = %s\n", argv[optind]);
/* Other code omitted */
exit (EXIT_SUCCESS);
}
请注意,您需要添加一些声明和一个 main()
函数才能使其正常工作。
您可以看到上面的示例 n
是一个标志,并且像您的 b
选项一样工作。上面的 t
选项接受一个参数,并且像您的 c
选项一样工作。如果你想有一个 a
选项也是一个标志,你可以使 getopt
参数 "abf:"
(即添加一个 a
in without a colon), and a stanza to the switch
like this:
case 'a':
aflag = 1;
break;
首先将 aflag
设置为 0。最后,您将检查在未设置 aflag
的情况下传递其他选项的非法情况。
总而言之,它看起来像这样:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int
main (int argc, char *argv[])
{
int flags, opt;
int nsecs, tfnd;
int aflag;
nsecs = 0;
tfnd = 0;
flags = 0;
aflag = 0;
while ((opt = getopt (argc, argv, "ant:")) != -1)
{
switch (opt)
{
case 'a':
aflag = 1;
break;
case 'n':
flags = 1;
break;
case 't':
nsecs = atoi (optarg);
tfnd = 1;
break;
default: /* '?' */
fprintf (stderr, "Usage: %s [-t nsecs] [-n] name\n", argv[0]);
exit (EXIT_FAILURE);
}
}
printf ("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);
if (optind >= argc)
{
fprintf (stderr, "Expected argument after options\n");
exit (EXIT_FAILURE);
}
if (!aflag && (flags || tfnd))
{
fprintf (stderr, "Must specify a flag to use n or t flag\n");
exit (EXIT_FAILURE);
}
printf ("name argument = %s\n", argv[optind]);
/* Other code omitted */
exit (EXIT_SUCCESS);
}
关于使用 getopt() : giving command line flags criteria 进行 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139701/
我从未见过这样初始化的 for 循环,不明白为什么要这样写? 我正在研究如何连接到 .NET 中的 IMAP 服务器,并开始查看名为 ImapX 的库中的代码。 .我在将数据写入 NetworkStr
我正在尝试解析这个标志“dataroot”(字符串类型)。 将此视为演示代码: from absl import flags from absl.flags import FLAGS flags.DE
我想知道调用之间是否有任何区别(或可能的副作用): AtomicBoolean.set(true) 和 AtomicBoolean.compareAndset(false, true) AtomicB
这个问题已经有答案了: Do you prefer "if (var)" or "if (var != 0)"? [closed] (26 个回答) 已关闭 6 年前。 两者之间有性能差异吗 if (
在 TensorFlow 中解析标志的目的是什么?什么是 tf.flags.FLAGS?&此代码语句的作用是什么? FLAGS = tf.flags.FLAGS FLAGS._parse_flags(
面试题:if (flag==0) 和 if (0==flag) 哪个会执行得更快?为什么? 最佳答案 我还没有看到任何正确的答案(并且已经有一些)警告:Nawaz 确实指出了用户定义的陷阱。而且我很遗
在我的一个站点上,我有一个主用户表,其中包含每个用户的唯一用户 ID、电子邮件地址、密码等。 我需要开始跟踪很多与每个用户相关的二进制标志,比如他们是否确认了他们的电子邮件,他们是否发布了消息,他们是
我有以下辅助函数用于使用 argparse 解析参数: def get_cli_arguments(): parser = argparse.ArgumentParser(prog='Xtra
var a = 'toto titi (should be removed) 5'.replace(/\(.*\)|\[.*\]|[^a-zA-Z0-9 ]|tata|tutu|tyty/gi, '!
我是 Golang 的新手,我一直无法使用标志找到解决此问题的方法。 我如何使用标志以便我的程序可以处理这样的调用,其中 -term 标志可能出现可变次数,包括 0 次: ./myprogram -f
我收到以下有关此正则表达式模式的控制台警告:。我看不到如何为该警告创建有效的正则表达式模式。请问,有没有人能解释一下这个错误以及如何解决它?。已尝试查看文档,但看不到如何使其对v标志有效
嗨,这两者有什么区别 在 SQL Server 中set @flag=1 和set @@flag = 1? 谢谢 最佳答案 什么都没有。作为惯例,某些内置函数的名称以双 at 符号 @@ 开头。为了避
是否可以在不覆盖现有标志的情况下使用 java 邮件在 IMAP 邮件消息上设置自定义标志?例如,我需要在已处理消息上设置一个标志“已处理”,而不将其状态更改为 SEEN/DELETED 或没有邮件客
我对以下行为感到困惑。变量标志从 Controller 传递到jsp代码: flag: flag eq 'Y': ${requestScope.flag eq 'Y'} flag == 'Y': $
我正在尝试根据标志值构建一个字符串 return `${super.getDetails()} Electric: ${this.isElectric} ${flag && '|hatchback'}
此外,将它们直接列在相应标签的对面是否为调试和发布设置了它们? 最佳答案 Xcode 帮助提供了相当清晰的定义: Name: Other C Flags Abstract: Space-separat
当我在 android studio 中创建或克隆一个 flutter 项目时,它显示“创建项目时出错设置 VM 标志失败:无法识别的标志:disable-dart-dev” 我使用的是 Androi
我有一些Python argparse命令行处理代码,最初看起来像这样: import argparse ap = argparse.ArgumentParser() ap.add_argument(
我尝试创建一个多线程单例模式类。 标题: class HL{ public: static HL* getInstance(); ......... priva
我在阅读别人的代码时遇到了很多标志, if (condition1) var1 = true else var1 = false 后来, if (var1 == true) /
我是一名优秀的程序员,十分优秀!