简体   繁体   中英

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

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).

./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.

[[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 './'):

./VisiMacXsltConverter arg1 arg2 arg3 arg4

Hope I have not misunderstood you question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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