CLAP - Complex Example

// example2.cc
// ~~~~~~~~~~~

#include <cmdline.hh>

CmdArgBool   help('h', "help", "Show the usage of the program",
                  CmdArg::isOPT | CmdArg::isHIDDEN);
CmdArgBool    dev('d', "dev", "Use development environment",
                  CmdArg::isOPT | CmdArg::isHIDDEN);
CmdArgChar     ch('c', "ch", "char", "The character to count",
                  CmdArg::isREQ | CmdArg::isVALREQ);
CmdArgStr     str('s', "str", "string", "The string to change",
                  CmdArg::isOPT | CmdArg::isVALREQ, "Hello World");
CmdArgFloat   num('n', "num", "prec", "Precentage from the total",
                  CmdArg::isOPT | CmdArg::isVALREQ, 0.05);
 

int main(int argc, char* argv[])
{
  CmdLine cmd(argv[0], &help, &str, &num, &ch, &dev, NULL);
  cmd.parse(argc, argv);

  if (help)
    cmd.usage();

  if (dev)
    cout << "using development environment." << endl;
 
  int length = strlen(str);
  int count = 0;
  for (int i = 0; i < length; i++)
    if (ch == str[i])
      count++;
  cout << "the character [" << ch << "] appear in the string \""
       << str << "\" " << count << " times." << endl;

  double prec = 100 * num;
  cout << "the total precentage is: " << prec << "%" << endl;
 
  return 0;
}

compilation

% g++ -I/users/silver2/projects/cgproj/package/include example1.cc -L/users/silver2/projects/cgproj/package/lib -lcmd -o example
running examples

% exapmle2
Error: the switch -ch must be supplied
 
Usage: example2 -ch char [-num prec] [-str string]
Where:
       char - The character to count
       prec - Precentage from the total
     string - The string to change

% exapmle2 -ch e -prec
Warning: argument "-prec" looks strange, ignoring.
the character [e] appear in the string "Hello World" 1 times.
the total precentage is: 5%
% example2 -ch l -dev
using development environment.
the character [l] appear in the string "Hello World" 3 times.
the total precentage is: 5%
% example2 -str "HAGGAI DAVID" -ch A -num .245
the character [A] appear in the string "HAGGAI DAVID" 3 times.
the total precentage is: 24.5%
%

 


This page was designed by Haggai David.
Send your comments to davidcha@cs.bgu.ac.il
Copyright © 1997 Haggai David, All rights reserved.

Back