source: trunk/bin/Parameter.cc @ 248

Last change on this file since 248 was 241, checked in by Peter Johansson, 15 years ago

some more lippstick for help output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1// $Id: Parameter.cc 241 2007-04-28 01:52:07Z 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, 2);
174    cs2.width(0)=24;
175    cs2.width(2)=52;
176    cs2.margin(0)=2;
177
178    std::cout << "usage: svndigest [options]\n"
179              << "\n";
180
181    cs << "svndigest traverses a directory structure (controlled by "
182       << "subversion) and calculates developer statistics entries. "
183       << "The top level directory of the directory structure to "
184       << "traverse is set with the -r option. The result is written to "
185       << "the current working directory or a directory set with the "
186       << "-t option.\n";
187    std::cout << "\nValid options:\n";
188
189    cs2  << "    --copyright\tupdate copyright statement\n" 
190         << "    --config-file=ARG\tconfiguration file " 
191         << "[<ROOT>/.svndigest/config]\n"
192         << "-f, --force\tremove target directory/file if it exists "
193         << "NOTE recursive delete.\n"
194         << "-g, --generate-config\twrite configuration file " 
195         << "to standard output and exit\n"
196         << "-h, --help\tdisplay this help and exit\n"
197         << "-r, --root=ROOT\tsvn controlled directory to perform "
198         << "statistics calculation on [" << root_ << "]\n"
199         << "    --revisions\tUse revision numbers as time scale "
200         << "instead of dates [dates].\n"
201         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
202         << "-v, --verbose\texplain what is being done\n"
203         << "    --version\tprint version information and exit\n";
204    std::cout << std::endl;
205  }
206
207
208  void Parameter::version(void) const
209  {
210    using namespace std;
211    cout << PACKAGE_STRING
212         << "\nCopyright (C) 2006-2007 Jari Häkkinen and Peter Johansson.\n\n"
213         << "This is free software; see the source for copying conditions.\n"
214         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
215         << "A PARTICULAR PURPOSE." << endl;
216  }
217
218}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.