source: trunk/bin/Parameter.cc @ 240

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

removed -rev flag (there is still --revsions). Changed help output to be more gnu-like. Changed default config-file to be <ROOT>/.svndigest/config.

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