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.
Related
I'm trying to use this method on an NSObject in a macOS Command Line Tool.
https://developer.apple.com/documentation/objectivec/nsobject/1416176-performselector?language=objc
This works fine in a normal app but when I try to use it in a Command Line Tool nothings happens. I'm keeping my app running by using dispatch_main(). What else do I need to do to have this method working on an NSObject?
int main(int argc, const char * argv[])
{
#autoreleasepool
{
// ...
}
dispatch_main();
return 0;
}
I have a class named blender that has a single instance variable, speed. speed is of type enum BlenderSpeed defined as
typedef NS_ENUM(NSUInteger, BlenderSpeed)
{
BlenderSpeedStir=1 ,
BlenderSpeedCop=2 ,
BlenderSpeedCrush=3
} ;
The main.m is as below. Basically, I was expecting there to be a warning when I passed 5 as argument in the setSpeed message send since speed is only supposed to take on values 1, 2 and 3 per enum definition.
But I don't see any issues when I build and run the code. The speed gets set to 5 without any issues.
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
blender *myBlender = [[blender alloc] init] ;
[myBlender setSpeed:2] ;
NSLog(#"Current blender speeed = %lu",[myBlender speed]);
[myBlender setSpeed:5] ;
NSLog(#"Current blender speeed = %lu",[myBlender speed]);
}
return 0;
}
Any pointers will be greatly appreciated.
int main(int argc, char *argv[])
{
strncpy(argv[1], "fookBar", 7);
return NSApplicationMain(argc, (const char **)argv);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSArray *args = [[NSProcessInfo processInfo] arguments];
}
the args array in applicationDidFinishLaunching doesnt show the chages made to argv[1] in main. ?. Why ?
According to the apple doc
"NSApplicationMain itself ignores the argc and argv arguments. Instead, Cocoa gets its arguments indirectly via _NSGetArgv, _NSGetArgc, and _NSGetEnviron (see )."
Thats the reason why even when you make changes to the argv in main, it does not reflect in applicationDidFinishLaunching
I'm creating a small C-program and would like a char pointer array holding only the arguments the executable was started with.
Currently this code also outputs all environment variables:
int main (int argc, const char * argv[]) {
while(argv) {
NSLog(#"Parameter %s\n", *argv);
argv++;
}
}
Instead of doing the cycle the way you do, use argc. The size of argv array is argc, with the first value argv[0] being how the name of the program being executed.
int main (int argc, const char * argv[]) {
for (int i = 1; i < argc; ++i) {
NSLog(#"Parameter %s\n", argv[i]);
}
}
Your code is also dumping the environment variables because they are supplied as an additional parameter after argv. In fact you are accessing memory out of bounds for argv and it is pure luck this works.
Change while(argv) to while(*argv). That will give you just the arguments.
main() is actually called like this main(int argc, char **argv, char **environ)
What is happening is you are going past argv and into environ. This behavior
is undefined should not be relied on. Your code, as it is, will also keep on going past environ
and won't stop, you'll be printing garbage.
You can, of course, do it the other way:
for(int i = 0; i < argc; i++) {
NSLog(#"Parameter %s\n", argv[i]);
}
argv[0] contains the program name, the rest are the arguments.
I am new to the objective c and i write the code according to a reference book.
but something went wrong and I don't know why.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
if (argc==1){
NSLog(#"you need to provide a file name");
return (1);
}
FILE *wordFile = fopen(argv[1], "r");
char word[100];
while(fgets(word , 100, wordFile)){
word[strlen(word)-1] = '\0';
NSLog(#"the length of the %s is %lu", word, strlen(word));
}
fclose(wordFile);
return 0;
}
the tool indicates that the while part went wrong, EXC_BAD_ACCESS.
Any idea?
It compiles and runs fine on my machine. But imagine you have an empty line in your file. Then strlen(word) will return zero. Hence word[strlen(word)-1] = '\0'; will try to set some memory which might not be valid since word[-1] might not be a valid memory cell, or a memory cell that you can legally access.
Oh, and by the way, it has nothing to do with objective-c. This is mostly (but for the NSLog call) pure ansi C.