简体   繁体   English

参数中的命令行分配

[英]Command line assignment in arguments

Is it possible to make command like arguments like below? 是否可以使命令像下面这样的参数?

./exe FROM_FILE=true 

Here in this case, how I expect in the program is, when FROM_FILE value is true, I wil take data from file and if false, I can take standard values. 在这种情况下,我在程序中的期望是,当FROM_FILE值为true时,我将从文件中获取数据,如果为false,则可以采用标准值。 The other alternatives I find for the purpose is to use 我找到的其他替代方法是使用

  1. -options -options
  2. Simply pass arguments. 只需传递参数即可。 But in this case I have to worry about the order of arguments. 但是在这种情况下,我必须担心参数的顺序。

This one just came to mind when doing a makefile. 制作makefile时就想到了这一点。

$>cat makefile
exe     :
        echo ${FROM_FILE}
$>make FROM_FILE=true
echo true
true
$>

You can do 你可以做

FROM_FILE=true ./exe

and then read the FROM_FILE environment variable from inside the app with the environ pointer or getenv() . 然后使用environ指针或getenv()从应用程序内部读取FROM_FILE环境变量。 See http://linux.die.net/man/7/environ 参见http://linux.die.net/man/7/environ

EDIT : The more I think about it, the less a good idea it seems to break with common command line interface conventions. 编辑 :我考虑得越多,似乎就没有与常见的命令行界面约定相抵触的好主意。 maybe you should read this: http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html 也许您应该阅读以下内容: http : //www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html

have you tried environment variables? 您是否尝试过环境变量?

you can do 你可以做

FROM_FILE=true ./exe

and in exe.c: 并在exe.c中:

[...]
char *file = getenv("FROM_FILE");
if (file == NULL)
  *file = DEFAULT;
[...]

alternatively, for a more GNU-ish approach to command line interfaces, have a look at getopt, or argp. 另外,对于命令行界面而言,要获得更多GNU风格的方法,请查看getopt或argp。 ( http://www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html#Parsing-Program-Arguments ) http://www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html#Parsing-Program-Arguments

Possible? 可能? Well, since make apparently does it (I haven't tried it myself), it's obviously possible . 好吧,既然make显然可以做到(我自己还没有尝试过),那显然是有可能的 It might trip up some people because it's certainly an unusual syntax, though. 但是,它可能会使某些人不满意,因为它肯定是一种不寻常的语法。

What you'd want to do then is probably to iterate over argv[] , find any parameters on the form something=something , and do your magic. 然后,您想要做的可能是遍历argv[] ,在something=something形式上找到任何参数,然后大功告成。 Maybe even zero them out after you are done with them, and then do something more normal like handing over the grunt work to getopt or a similar library. 在使用完它们之后,甚至可以将它们归零,然后执行更常规的操作,例如将艰苦的工作移交给getopt或类似的库。

One thing to consider, especially if you want this to be generic, is what to do when someone passes a file name to your application that contains a = , expecting that file name to be opened, processed or whatever it is your application does for a living. 要考虑的一件事是,当有人将包含=的文件名传递到您的应用程序时,希望该文件名被打开,处理,或者您的应用程序对a所做的任何事情,特别是如果您希望此名称通用的话活的。

Of course it is possible! 当然可以! Something like this (untested): 像这样(未经测试):

int main(int argc, char **argv)
{
    int FROM_FILE = 0;
    const char *FROM_FILE_key = "FROM_FILE";
    int i;
    /* Iterate through all the arguments. */
    for (i = 1; i < argc; ++i)
    {
        /* Look for the '='. If not there, skip this argument. */
        char *eq = strchr(argv[i], '=');
        if (!eq)
            continue;

        /* Compare the key. If not what we want, skip it. */
        if (strncmp(argv[i], FROM_FILE_key, strlen(FROM_FILE_key)) != 0)
            continue;

        /* Parse the value. You probably want to make this more flexible 
          (true, 1, yes, y...)*/
        if (strcmp(eq+1, "true") == 0)
            FROM_FILE = 1;
    }

    printf("FROM_FILE=%s", FROM_FILE? "true" : "false");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM