foo.cpp

来自「最新的版本ACE-5.6.8,刚从外文网上搬下,与大家分享.」· C++ 代码 · 共 185 行

CPP
185
字号
#include <typeinfo>
#include <string>
#include <iostream>

/* FUZZ: disable check_for_improper_main_declaration */

namespace CommandLine
{
  struct Parser
  {
  };
}

using std::string;
using std::cerr;
using std::endl;

using namespace CommandLine;

class Command
{
public:
  enum Value
  {
    HELP,
    VERSION,
    DEFAULT
  };

  Command (Value v = Command::DEFAULT)
      : v_ (v)
  {
  }

  operator Value () const
  {
    return v_;
  }

private:
  Value v_;
};


int
version ();

int
help (int argc, char* argv[]);

int
main (int argc, char* argv[])
{

  // Step 1: determine command
  //
  // * there is usually one command
  // * command can be optional
  // * command usually takes up one argument
  //

  CommandParser<Command> cp;

  switch (cp.parse (argc, argv))
  {
  case Command::VERSION:
    {
      return version ();
    }
  case Command::HELP:
    {
      return help (argc, argv);
    }
  }

  // Step 2: parse options
  //
  // * options are usually optional
  // * options are usually position-independant
  // * options usually do not repeat
  // * options can take up more than one argument
  //

  OptionMap om;

  CompositeParser op;

  op.add (OptionParser<string> ("string", "--string", "-s"));
  op.add (OptionParser<unsigned long> ("number", "--number", "-n"));

  while (argc != 1 && !op.empty ())
  {
    om.insert (op.parse (argc, argv));
  }

  // Step 3: parse operands
  //
  // * operands usually position-dependant
  // * operand usually take up one argument
  //

  OperandParser<string> odp;

  string str = odp.parse (argc, argv);

  unsigned long num = 0;

  if (argc != 1)
  {
    OperandParser<unsigned long> op;
    num = op.parse (argc, argv);
  }

  string s = om.count ("string") ? om["string"] : "default";
  unsigned long l = om["number"];

  // om.at ()
  // om.get ()
  // om.resolve ()
  // om.option ()
  // om.value ()

  cerr << "opreation settings are:" << endl << endl
       << "option string  : " << om.get<string> ("string", "default") << endl
       << "option number  : " << om.get ("number", 10UL) << endl
       << "operand string : " << str << endl
       << "operand number : " << num << endl;
}


//
//
//
int
version ()
{
  cerr << "foo 1.0" << endl;
  return 0;
}


//
//
//
int
help (int argc, char* argv[])
{
  Command subject;

  if (argc != 1)
  {
    OperandParser<Command> op;
    subject = op.parse (argc, argv);
  }

  switch (subject)
  {
  case Command::HELP:
    {
      cerr << "foo help [<command>]" << endl << endl
           << "\t If <command> is specified then print extended help" << endl
           << "\t information for specified command. Otherwise print" << endl
           << "\t general usage information." << endl;
      break;
    }
  case Command::VERSION:
    {
      cerr << "foo version" << endl << endl
           << "\t Print version information." << endl;
      break;
    }
  default:
    {
      cerr << "foo version" << endl
           << "foo help [<command>]" << endl
           << "foo [-s|--string <str>] [-n|--number <num>] <str> [<num>]"
           << endl;
      break;
    }
  }

  return 0;
}
//$Id: foo.cpp 80826 2008-03-04 14:51:23Z wotte $

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?