Command Line Arguments in Objective-C
Last Updated :
28 Apr, 2025
In Objective-C, command-line arguments are strings of text that are passed to a command-line program when it is launched. They can be used to specify options or parameters that control the behavior of the program or to provide input data that the program can process.
To access command-line arguments in an Objective-C program, you can use the argc and argv variables that are passed to the main() function. argc (short for "argument count") is an integer that specifies the number of command-line arguments passed to the program, and argv (short for "argument vector") is an array of char pointers that points to the individual arguments.
For example, consider the following command-line invocation of a program named "main":
./main -v input.txt output.txt
In this example, argc would be equal to 4, and argv would be an array with the following elements:
argv[0] = "./main"
argv[1] = "-v"
argv[2] = "input.txt"
argv[3] = "output.txt"
You can access the command-line arguments in your Objective-C program by iterating over the argv array and processing the individual arguments. For example:
ObjectiveC
// Objective-C program for command line argument
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i;
for (i = 0; i < argc; i++)
{
NSString *str = [NSString stringWithUTF8String:argv[i]];
NSLog(@"argv[%d] = '%@'", i, str);
}
[pool drain];
return 0;
}
Output:
It's important to note that the first element of the argv array (argv[0]) is the name of the program itself, not a command-line argument. The actual command-line arguments start at argv[1].
In addition to accessing the command-line arguments directly, you can also use the NSUserDefaults class to parse and process command-line arguments in a more convenient way. The NSUserDefaults class allows you to define a set of options and their corresponding values, and then parse the command-line arguments to extract the values of those options. This can be particularly useful if your program has a large number of command-line options, or if you want to use a consistent syntax for specifying options across multiple programs.
Similar Reads
Command Line Arguments in C++ Command-line arguments are arguments that are passed to a program when it is executed from the command line or terminal. They are provided in the command-line shell of operating systems with the program execution command.The main function of C++ generally has the following signature:C++int main(){ /
3 min read
Command Line Argument in Scala The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings. runtime. But it never accepts parameters from any other method in the program. Sy
2 min read
Command Line Arguments in Golang Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as "Args". Args is an
2 min read
Command Line Arguments in Golang Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as "Args". Args is an
2 min read
Bash Script - How to use Command Line Arguments In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
Passing Arrays as Function Arguments in Objective-C Every programming language has a data structure that is used to store elements. Similarly, Objective-C also supports data structure which is known as an array. An array is used to store elements of the same data types in a fixed-size sequential collection. In an array, you are not allowed to store d
5 min read