source: trunk/bin/Parameter.cc @ 431

Last change on this file since 431 was 431, checked in by Peter Johansson, 14 years ago

Type help should be Try help

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1// $Id: Parameter.cc 431 2007-07-06 23:46:42Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Parameter.h"
25
26#include "ColumnStream.h"
27#include "utility.h"
28#include <config.h> // this header file is created by configure
29
30#include <fstream>
31#include <iostream>
32#include <sstream>
33#include <stdexcept>
34#include <string>
35#include <sys/stat.h>
36
37namespace theplu {
38namespace svndigest {
39
40  Parameter::Parameter(const int argc,const char *argv[])
41  {
42    defaults();
43    for (int i=1; i<argc; ++i) {
44      bool ok=false;
45      std::string myargv(argv[i]);
46      if (myargv=="--config-file"){
47        if (++i<argc){
48          config_file_= std::string(argv[i]);
49          ok=true;
50        }
51      }
52      else if (myargv=="--copyright"){
53          copyright_=true;
54          ok=true;
55      }
56      else if (myargv=="-f" || myargv=="--force"){
57          force_=true;
58          ok=true;
59      }
60      else if (myargv=="-g" || myargv=="--generate-config"){
61          generate_config_=true;
62          ok=true;
63      }
64      else if (myargv=="-h" || myargv=="--help"){
65        help();
66        exit(0);      // always exit after printing help
67      }
68      else if (myargv=="-r" || myargv=="--root"){
69        if (++i<argc){
70          root_= std::string(argv[i]);
71          ok=true;
72        }
73      }
74      else if (myargv=="--report") {
75          report_=true;
76          ok=true;
77      }
78      else if (myargv=="--no-report") {
79          report_=false;
80          ok=true;
81      }
82      else if (myargv=="--revisions") {
83          revisions_=true;
84          ok=true;
85      }
86      else if (myargv=="-t" || myargv=="--target"){
87        if (++i<argc){
88          targetdir_= std::string(argv[i]);
89          ok=true;
90        }
91      }
92      else if (myargv=="-v" || myargv=="--verbose"){
93          verbose_=true;
94          ok=true;
95      }
96      else if (myargv=="--version"){
97          version();
98          exit(0);
99      }
100      else if (myargv=="-vf" || myargv=="-fv"){
101          verbose_=true;
102          force_=true;
103          ok=true;
104      }
105
106      if (!ok)
107        throw std::runtime_error("svndigest: invalid option: " + myargv +
108                                 "\nTry `svndigest --help' for usage.");
109    }
110
111    analyse();
112  }
113
114
115  void Parameter::analyse(void)
116  {
117    using namespace std;
118
119    string workdir(pwd()); // remember current working directory (cwd).
120
121    // Checking that root_ exists and retrieve the absolute path to root_
122    if (chdir(root_.c_str()))
123      throw runtime_error(string("svndigest: Root directory (") + root_ +
124                          ") access failed.");
125    root_ = pwd();
126
127    // need to get back to cwd if relative paths are used.
128    if (chdir(workdir.c_str()))
129      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
130
131    // Checking that targetdir_ exists and retrieve the absolute path
132    // to targetdir_
133    if (chdir(targetdir_.c_str()))
134      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
135                          ") access failed.");
136    targetdir_ = pwd();
137    // Checking write permissions for targetdir_
138    if (access_rights(targetdir_,"w"))
139      throw runtime_error(string("svndigest: No write permission on target ") +
140                                 "directory (" + targetdir_ +").");
141
142    // return back to cwd
143    if (chdir(workdir.c_str()))
144      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
145
146  }
147
148
149  std::string Parameter::config_file(void) const
150  {
151    // not default
152    if (!config_file_.empty())
153      return config_file_;
154   
155    // default behaviour
156    return root()+"/.svndigest/config";
157  }
158
159
160  void Parameter::defaults(void)
161  {
162    config_file_ = "";
163    copyright_=false;
164    force_=false;
165    generate_config_=false;
166    report_=true;
167    revisions_=false;
168    root_=".";
169    targetdir_=".";
170    verbose_=false;
171  }
172
173
174  void Parameter::help(void)
175  {
176    defaults();
177
178    ColumnStream cs(std::cout, 1);
179    cs.width(0)=79;
180    cs.margin(0)=0;
181    ColumnStream cs2(std::cout, 2);
182    cs2.width(0)=24;
183    cs2.width(1)=52;
184    cs2.margin(0)=2;
185
186    std::cout << "Usage: svndigest [OPTION]...\n"
187              << "\n";
188
189    cs << "Generate statistical report for a subversion repository.\n";
190
191    cs << "\nMandatory arguments to long options are mandatory for "
192       << "short options too.\n";
193
194    cs2  << "    --copyright\tupdate copyright statement\n" 
195         << "    --config-file=ARG\tconfiguration file " 
196         << "[<ROOT>/.svndigest/config]\n"
197         << "-f, --force\tif sub-directory named <ROOT> exists in "
198         << "target directory, remove sub-directory before writing results\n"
199         << "-g, --generate-config\twrite configuration file " 
200         << "to standard output and exit\n"
201         << "-h, --help\tdisplay this help and exit\n"
202         << "    --no-report\tCreate no HTML report\n"
203         << "    --revisions\tUse revision numbers as time scale "
204         << "instead of dates [dates]\n"
205         << "-r, --root=ROOT\tsvn controlled directory to perform "
206         << "statistics calculation on [" << root_ << "]\n"
207         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
208         << "-v, --verbose\texplain what is being done\n"
209         << "    --version\tprint version information and exit\n";
210
211    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
212              << std::endl;
213  }
214
215
216  void Parameter::version(void) const
217  {
218    ColumnStream cs(std::cout, 1);
219    cs.width(0)=79;
220    cs << PACKAGE_STRING
221       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
222       << "This is free software; see the source for copying conditions. "
223       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
224       << "A PARTICULAR PURPOSE.\n";
225  }
226
227}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.