Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Using command line arguments in Open C

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

}

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition (initial release)
Article
Keywords: getopt(), getopt_long()
Created: aknyman (11 Apr 2008)
Last edited: lpvalente (11 Aug 2012)

Contents

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.

This page was last modified on 11 August 2012, at 18:14.
23 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×