You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 17:20.

CS000903 - Using command line arguments

From Forum Nokia Wiki


}


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

Overview

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.

  • getopt() takes argc, argv, and optstring as parameters and returns the first known option in optstring. The second call with the same arguments returns the next option, and so on, until -1 is returned.
  • getopt_long() works like getopt() except that it also accepts long options, started out by two dashes (--). getopt_long() takes also two additional arguments, longopts and longindex.

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.

MMP file

The following libraries are required:

STATICLIBRARY libcrt0.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <stdio.h>  //printf
#include <unistd.h> //optarg, optind, optopt
#include <getopt.h> //getopt, getopt_long
#include <stdlib.h> //exit
  • processing command line arguments without library functions
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;
}
  • processing command line arguments with the getopt() function
/* 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;
}
  • processing command line arguments with the getopt_long() function
/* 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;
}

Postconditions

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");

See also

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.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fFileE3aMicrokernelE5fArchitectureE2eGIFX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ