COIN-OR::LEMON - Graph Library

Changeset 2410:fe46b61da4e3 in lemon-0.x for tools


Ignore:
Timestamp:
03/14/07 19:01:04 (17 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@3241
Message:

dim_to_lgf

  • use the argparser class

arg_parser improvments

  • usage of assert in cc
  • const char argv

error

  • handling the string parameter
Location:
tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tools/dim_to_lgf.cc

    r2404 r2410  
    1919///\ingroup demos
    2020///\file
    21 ///\brief DIMACS to LGF converter (demo).
     21///\brief DIMACS to LGF converter.
    2222///
    2323/// This program converts various DIMACS formats to the LEMON Graph Format
     
    3434#include <lemon/graph_writer.h>
    3535
     36#include <lemon/arg_parser.h>
     37
    3638using namespace std;
    3739using namespace lemon;
    38 
    39 const char* versionString =
    40 "dim_to_lgf - part of lemon library\n";
    41 
    42 const char* helpString =
    43 "DIMACS to LGF converter\n"
    44 "Usage: dim_to_lgf [OPTIONS]\n"
    45 "\n"
    46 "Examples:\n"
    47 "  dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
    48 "\n"
    49 "Options:\n"
    50 "  -i FILE, --input FILE    use FILE as input instead of standard input\n"
    51 "  -o FILE, --output FILE   use FILE as output instead of standard output\n"
    52 "  -t TYPE, --type TYPE     set up the type of the graph\n"
    53 "                             Possible types:\n"
    54 "                               mincostflow\n"
    55 "                               maxflow (default)\n"
    56 "                               shortestpath\n"
    57 "                               capacitated\n"
    58 "                               plain\n"
    59 "  -v, --version            shows the version of the converter\n"
    60 "  -h, --help               shows the help of the converter\n";
    6140
    6241
     
    7453  std::string typeName;
    7554
    76   bool help = false;
    77   bool version = false;
     55  bool mincostflow;
     56  bool maxflow;
     57  bool shortestpath;
     58  bool capacitated;
     59  bool plain;
    7860
    79   for (int arg = 1; arg < argc; ++arg) {
    80     if (strcmp(argv[arg], "--type") == 0 ||
    81         strcmp(argv[arg], "-t") == 0) {
    82       if (!typeName.empty()) {
    83         cerr << "Multiple type description" << endl;
    84         return -1;
    85       }
    86       if (arg + 1 == argc) {
    87         cerr << "Parameter without value" << endl;
    88         return -1;
    89       }
    90       typeName = argv[++arg];
    91     }
    92     else if (strcmp(argv[arg], "--input") == 0 ||
    93              strcmp(argv[arg], "-i") == 0) {
    94       if (!inputName.empty()) {
    95         cerr << "Multiple input description" << endl;
    96         return -1;
    97       }
    98       if (arg + 1 == argc) {
    99         cerr << "Parameter without value" << endl;
    100         return -1;
    101       }
    102       inputName = argv[++arg];
    103     }
    104     else if (strcmp(argv[arg], "--output") == 0 ||
    105              strcmp(argv[arg], "-o") == 0) {
    106       if (!outputName.empty()) {
    107         cerr << "Multiple input description" << endl;
    108         return -1;
    109       }
    110       if (arg + 1 == argc) {
    111         cerr << "Parameter without value" << endl;
    112         return -1;
    113       }
    114       outputName = argv[++arg];
    115     } else if (strcmp(argv[arg], "--help") == 0 ||
    116                strcmp(argv[arg], "-h") == 0) {
    117       help = true;
    118     } else if (strcmp(argv[arg], "--version") == 0 ||
    119                strcmp(argv[arg], "-v") == 0) {
    120       version = true;
    121     } else {
    122       cerr << "Invalid option: " << argv[arg] << endl;
    123       return -1;
    124     }
    125   }
     61  bool version;
    12662
    127   if (version) {
    128     cout << versionString;
    129   }
    130   if (help) {
    131     cout << helpString;
    132   }
    133   if (help || version) {
    134     return 0;
    135   }
     63  ArgParser ap(argc, argv);
     64  ap.refOption("-input",
     65               "use FILE as input instead of standard input",
     66               inputName).synonym("i", "-input")
     67    .refOption("-output",
     68               "use FILE as output instead of standard output",
     69               outputName).synonym("o", "-output")
     70    .refOption("-mincostflow",
     71               "set the type of the graph to \"mincostflow\" graph",
     72               mincostflow)
     73    .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
     74    .refOption("-maxflow",
     75               "set the type of the graph to \"maxflow\" graph",
     76               maxflow)
     77    .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
     78    .refOption("-shortestpath",
     79               "set the type of the graph to \"shortestpath\" graph",
     80               shortestpath)
     81    .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
     82    .refOption("-capacitated",
     83               "set the type of the graph to \"capacitated\" graph",
     84               capacitated)
     85    .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
     86    .refOption("-plain",
     87               "set the type of the graph to \"plain\" graph",
     88               plain)
     89    .optionGroup("type", "-plain").synonym("pl", "-plain")
     90    .onlyOneGroup("type")
     91    .mandatoryGroup("type")
     92    .refOption("-version", "show version information", version)
     93    .synonym("v", "-version")
     94    .run();
    13695
    13796  ifstream input;
     
    155114  ostream& os = (outputName.empty() ? cout : output);
    156115
    157   if (typeName.empty()) {
    158     typeName = "maxflow";
    159   }
    160 
    161   if (typeName == "mincostflow") {
     116  if (mincostflow) {
    162117    Graph graph;
    163118    Node s, t;
     
    170125      writeEdgeMap("cost", cost).
    171126      run();
    172   } else if (typeName == "maxflow") {
     127  } else if (maxflow) {
    173128    Graph graph;
    174129    Node s, t;
     
    180135      writeNode("target", t).
    181136      run();
    182   } else if (typeName == "shortestpath") {
     137  } else if (shortestpath) {
    183138    Graph graph;
    184139    Node s;
     
    189144      writeNode("source", s).
    190145      run();
    191   } else if (typeName == "capacitated") {
     146  } else if (capacitated) {
    192147    Graph graph;
    193148    DoubleMap capacity(graph);
     
    196151      writeEdgeMap("capacity", capacity).
    197152      run();
    198   } else if (typeName == "plain") {
     153  } else if (plain) {
    199154    Graph graph;
    200155    readDimacs(is, graph);
  • tools/lgf-gen.cc

    r2402 r2410  
    280280
    281281
    282 int main(int argc,char **argv)
     282int main(int argc,const char **argv)
    283283{
    284284  ArgParser ap(argc,argv);
Note: See TracChangeset for help on using the changeset viewer.