Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 07:22, 17 April 2008.

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 Discussions
Thread Thread Starter Forum Replies Last Post
Problem with PC Suite for 6600 rongmoul PC Suite API and PC Connectivity SDK 4 2003-12-29 21:17
Multi-language SIS files djgtram Symbian Tools & SDKs 5 2004-03-20 10:59
Creating jad and Jar using command line zivgr Mobile Java General 1 2003-07-14 13:47
Can't execute cpp at ... epocrc.pl line 161 alcau Symbian Tools & SDKs 12 2003-10-10 11:18
3 basics questions on Python scrips for N95 koool91270 Python 3 2007-10-17 15:47
 
Powered by MediaWiki