简体   繁体   English

如何在Mac应用程序中传递命令行参数

[英]how to pass command line argument in Mac Application

I have created a Command line tool application ( Xocde --> New App --> Command line tool) and its running without any problem, Now i want to run it through terminal and pass some command line argument, something like this 我创建了一个命令行工具应用程序(Xocde-> New App->命令行工具),它的运行没有任何问题,现在我想通过终端运行它并传递一些命令行参数,像这样

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

    std::cout << "got "<<argc<<" arguments";

    for ( int i = 0; i<argc;i++){
        std::cout << "argument:"<<i<<"= "<<argv[i];
    }

//// some other piece of code 
}

if i type on the terminal 如果我在终端上键入

>open VisiMacXsltConverter --args fdafsdfasf i am getting output 

got 1 argumentsargument:0= /Applications/VisiMacXsltConverte

I want to know through command line what is the way to pass the argument 我想通过命令行知道传递参数的方式

If you use just one - (hyphen) those values will go into a volatile UserDefaults dictionary (will also override other keys for the life of the process). 如果仅使用一个-(连字符),则这些值将进入易失的UserDefaults词典中(在过程的生命周期中还将覆盖其他键)。

./program -Param 4

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

    @autoreleasepool {
        NSLog(@"param = %@", [[NSUserDefaults standardUserDefaults] valueForKey:@"Param"]);        
    }
    return 0;
}

or you can just pass these in how ever you want and use the following which will give you an NSArray of NSStrings. 或者您可以按照自己的意愿传递这些,并使用以下代码为您提供NSStrings NSArrays。

[[NSProcessInfo processInfo] arguments];

Why you want to run it with open ? 为什么要以开放方式运行它?

I would run it with (if you have it in you $PATH you should omit the './'): 我会用它运行(如果您在$ PATH中包含它,则应省略“ ./”):

./VisiMacXsltConverter arg1 arg2 arg3 arg4

Hope I have not misunderstood you question. 希望我没有误会你的问题。

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

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