Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/lib/parser/preferences/cmd_line_prefs_reader.cc @ 7253

Last change on this file since 7253 was 7253, checked in by rennerc, 18 years ago

added license to argparser

File size: 4.1 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christoph Renner
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "cmd_line_prefs_reader.h"
19
20#include "preferences.h"
21
22using namespace std;
23
24RegistredArgs CmdLinePrefsReader::regArgs;
25
26/**
27 * standard constructor
28*/
29CmdLinePrefsReader::CmdLinePrefsReader( )
30{
31
32}
33
34
35/**
36 * standard deconstructor
37*/
38CmdLinePrefsReader::~CmdLinePrefsReader ()
39{
40}
41
42bool CmdLinePrefsReader::callBack( ArgTableEntry entry, void * data, const std::string & arg, const std::vector<MultiType> & argArgs )
43{
44  CallbackData * cbd = (CallbackData *)data;
45 
46  switch ( entry.id )
47  {
48    case ID_SET_INI:
49    {
50      std::string section = arg;
51      section.erase( 0, entry.longOption.length()+1 );
52      std::string key = section;
53      section.erase( section.find(".") );
54      key.erase( 0, key.find(".")+1 );
55      //PRINTF(0)("SECTION '%s', KEY '%s'\n", section.c_str(), key.c_str());
56     
57      if ( key == "" || section == "" || argArgs.size() != 1 )
58      {
59        PRINTF(1)("usage: --set-section.key=value\n");
60        return false;
61      }
62     
63      //Preferences::getInstance()->setMultiType( section, key, argArgs[0], true );
64      cbd->iniEntries.push_back( IniEntry() );
65      cbd->iniEntries.back().section = section;
66      cbd->iniEntries.back().key = key;
67      cbd->iniEntries.back().value = argArgs[0].getString();
68      return true;
69    }
70    case ID_HELP:
71      cbd->parser->showHelp();
72      return true;
73    case ID_LICENSE:
74      PRINT(0)(ORXONOX_LICENSE_SHORT);
75      return true;
76  }
77 
78  if ( entry.id >= ID_LAST && entry.id - ID_LAST < regArgs.size() )
79  {
80    if ( regArgs[entry.id - ID_LAST].value == "%arg%" )
81    {
82      assert( argArgs.size() == 1 );
83     
84      cbd->iniEntries.push_back( IniEntry() );
85      cbd->iniEntries.back().section = regArgs[entry.id - ID_LAST].section;
86      cbd->iniEntries.back().key = regArgs[entry.id - ID_LAST].key;
87      cbd->iniEntries.back().value = argArgs[0].getString();
88    }
89    else
90    {
91      cbd->iniEntries.push_back( IniEntry() );
92      cbd->iniEntries.back().section = regArgs[entry.id - ID_LAST].section;
93      cbd->iniEntries.back().key = regArgs[entry.id - ID_LAST].key;
94      cbd->iniEntries.back().value = regArgs[entry.id - ID_LAST].value;
95    }
96  }
97  else
98  {
99    assert(false);
100  }
101 
102  return true;
103}
104
105
106bool CmdLinePrefsReader::parse( int argc, char ** argv )
107{
108  CmdLineParser parser;
109 
110  parser.add( ID_HELP, "help", 'h', 0, "", "Shows this help");
111  parser.add( ID_LICENSE, "license", 'l', 0, "", "Prints the licence and exit");
112 
113  for ( int i = 0; i<regArgs.size(); i++ )
114  {
115    if ( regArgs[i].value == "%arg%" )
116    {
117      parser.add( ID_LAST + i, regArgs[i].longOption, regArgs[i].shortOption, 1, regArgs[i].argName, regArgs[i].help );
118    }
119    else
120    {
121      parser.add( ID_LAST + i, regArgs[i].longOption, regArgs[i].shortOption, 0, "", regArgs[i].help );
122    }
123  }
124 
125  parser.add( ID_SET_INI, "set-%", '\0', 1, "value", "Override a configuration element." );
126 
127  CallbackData cbd;
128 
129  cbd.parser = &parser;
130 
131  if ( parser.parse( &callBack, &cbd, argc, argv ) )
132  {
133    std::list<IniEntry>::const_iterator it;
134    for ( it = cbd.iniEntries.begin(); it != cbd.iniEntries.end(); it++ )
135    {
136      Preferences::getInstance()->setString( it->section, it->key, it->value, true);
137    }
138  }
139  else
140  {
141    exit(EXIT_FAILURE);
142  }
143 
144  return true;
145}
146
147bool CmdLinePrefsReader::registerArgument( const char shortOption, const std::string & longOption, const std::string & section, const std::string & key, const std::string & help, const std::string & argName, const std::string & value )
148{
149  RegistredArgument arg;
150 
151  arg.longOption = longOption;
152  arg.shortOption = shortOption;
153  arg.value = value;
154  arg.help = help;
155  arg.argName = argName;
156  arg.section = section;
157  arg.key = key;
158 
159  regArgs.push_back( arg );
160}
161
162
Note: See TracBrowser for help on using the repository browser.