| ID | CS000903 | Creation date | April 17, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Open C | Subcategory | Files/Data |
| Keywords (APIs, classes, methods, functions): getopt(), getopt_long() |
This snippet shows how command line arguments can be retrieved in Open C when a developer writes main() (with parameters argc and argv) as an entry point and links the program with the libcrt0.lib library. The argc parameter contains a count of arguments and argv contains an array of pointers to those arguments. The received arguments are the program's name (at index 0) and anything after that separated by the whitespace character.
Open C provides library functions getopt() and getopt_long() to help processing command line parameters and use them to control programs with different options.
When parsing the command line, these functions set global variables optarg (current option argument), optind (next argv pointer), and optopt (last known option) that can be used.
Note: In order to use this code, you need to install the Open C plug-in.
This snippet can be self-signed.
The following libraries are required:
STATICLIBRARY libcrt0.lib
LIBRARY libc.lib
LIBRARY euser.lib
#include <stdio.h> //printf
#include <unistd.h> //optarg, optind, optopt
#include <getopt.h> //getopt, getopt_long
#include <stdlib.h> //exit
int main(int argc, char *argv[])
{
int index;
printf("number of arguments:%d\n",argc);
printf("arguments:\n");
for (index=0; index<argc; index++)
{
printf("\t%s\n",argv[index]);
}
return 0;
}
/* options that have optargs are followed by a ':' */
static const char *optstring = "ab:c:h?";
int main(int argc, char *argv[])
{
int opt = 0;
int option_a = 0;
int option_b = 0;
int option_c = 0;
int optarg_b = 0;
const char *optarg_c = NULL;
opt = getopt(argc, argv, optstring);
while(opt != -1)
{
switch( opt )
{
case 'a':
option_a = 1;
break;
case 'b':
option_b = 1;
optarg_b = atoi(optarg);
break;
case 'c':
option_c = 1;
optarg_c = optarg;
break;
case 'h':
case '?':
printf("usage: name.exe [-a] [-b] 'number' "
"[-c] 'string' \n");
getchar();
exit(0);
default: break;
}
opt = getopt(argc, argv, optstring);
}
printf("number of arguments:%d\n",argc);
printf("found options:\n");
if(option_a)
printf("-a\n");
if(option_b)
printf("-b [%d]\n", optarg_b);
if(option_c)
printf("-c [%s]\n", optarg_c);
return 0;
}
/* options that have optargs are followed by ':' */
static const char *optstring = "ab:c:h?";
static const struct option longopts[] =
{
{ "option_a", no_argument, NULL, 'a' },
{ "option_b", required_argument, NULL, 'b' },
{ "option_c", required_argument, NULL, 'c' },
{ "help", no_argument, NULL, 'h' },
/* the last element of the longopts has to be filled with NULL values */
{ NULL, no_argument, NULL, 0 }
};
int main(int argc, char *argv[])
{
int opt = 0;
int longindex = 0;
int option_a = 0;
int option_b = 0;
int option_c = 0;
int optarg_b = 0;
const char *optarg_c = NULL;
opt = getopt_long(argc, argv, optstring, longopts, &longindex);
while(opt != -1)
{
switch( opt )
{
case 'a':
option_a = 1;
break;
case 'b':
option_b = 1;
optarg_b = atoi(optarg);
break;
case 'c':
option_c = 1;
optarg_c = optarg;
break;
case 'h':
case '?':
printf("usage: name.exe\n[-a|--option_a]\n"
"[-b|--option_b] 'number'\n"
"[-c|--option_c] 'string'\n");
getchar();
exit(0);
default: break;
}
opt = getopt_long(argc, argv, optstring, longopts, &longindex);
}
printf("number of arguments:%d\n",argc);
printf("found options:\n");
if(option_a)
printf("option_a\n");
if(option_b)
printf("option_b [%d]\n", optarg_b);
if(option_c)
printf("option_c [%s]\n", optarg_c);
return 0;
}
These three snippet programs show how many arguments they have received from the user. The parsed input options are also displayed to the standard output and if they are invalid, the help text is shown.
Tip: To test and execute these snippet programs, use e.g. the system() function call from another program.
#include <stdlib.h>
system("name.exe -a --option_b 123 -c abc");
If an E32Main() entry point is used in a hybrid application and command line argument parsing is needed, methods User::CommandLineLength() and User::CommandLine(TDes &aCommand) can be used.
See S60 documentation for further information.
No related wiki articles found