source: trunk/bin/Parameter.cc @ 374

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

fixes #216 improved descr. of --force option.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1// $Id: Parameter.cc 374 2007-06-19 23:05:14Z 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=="--revisions") {
75          revisions_=true;
76          ok=true;
77      }
78      else if (myargv=="-t" || myargv=="--target"){
79        if (++i<argc){
80          targetdir_= std::string(argv[i]);
81          ok=true;
82        }
83      }
84      else if (myargv=="-v" || myargv=="--verbose"){
85          verbose_=true;
86          ok=true;
87      }
88      else if (myargv=="--version"){
89          version();
90          exit(0);
91      }
92      else if (myargv=="-vf" || myargv=="-fv"){
93          verbose_=true;
94          force_=true;
95          ok=true;
96      }
97
98      if (!ok)
99        throw std::runtime_error("svndigest: invalid option: " + myargv +
100                                 "\nType `svndigest --help' for usage.");
101    }
102
103    analyse();
104  }
105
106
107  void Parameter::analyse(void)
108  {
109    using namespace std;
110
111    string workdir(pwd()); // remember current working directory (cwd).
112
113    // Checking that root_ exists and retrieve the absolute path to root_
114    if (chdir(root_.c_str()))
115      throw runtime_error(string("svndigest: Root directory (") + root_ +
116                          ") access failed.");
117    root_ = pwd();
118
119    // need to get back to cwd if relative paths are used.
120    if (chdir(workdir.c_str()))
121      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
122
123    // Checking that targetdir_ exists and retrieve the absolute path
124    // to targetdir_
125    if (chdir(targetdir_.c_str()))
126      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
127                          ") access failed.");
128    targetdir_ = pwd();
129    // Checking write permissions for targetdir_
130    if (access_rights(targetdir_,"w"))
131      throw runtime_error(string("svndigest: No write permission on target ") +
132                                 "directory (" + targetdir_ +").");
133
134    // return back to cwd
135    if (chdir(workdir.c_str()))
136      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
137
138  }
139
140
141  std::string Parameter::config_file(void) const
142  {
143    // not default
144    if (!config_file_.empty())
145      return config_file_;
146   
147    // default behaviour
148    return root()+"/.svndigest/config";
149  }
150
151
152  void Parameter::defaults(void)
153  {
154    config_file_ = "";
155    copyright_=false;
156    force_=false;
157    generate_config_=false;
158    revisions_=false;
159    root_=".";
160    targetdir_=".";
161    verbose_=false;
162  }
163
164
165  void Parameter::help(void)
166  {
167    defaults();
168
169    ColumnStream cs(std::cout, 1);
170    cs.width(0)=79;
171    cs.margin(0)=0;
172    ColumnStream cs2(std::cout, 2);
173    cs2.width(0)=24;
174    cs2.width(1)=52;
175    cs2.margin(0)=2;
176
177    std::cout << "Usage: svndigest [OPTION]...\n"
178              << "\n";
179
180    cs << "Generate statistical report for a subversion repository.\n";
181
182    cs << "\nMandatory arguments to long options are mandatory for "
183       << "short options too.\n";
184
185    cs2  << "    --copyright\tupdate copyright statement\n" 
186         << "    --config-file=ARG\tconfiguration file " 
187         << "[<ROOT>/.svndigest/config]\n"
188         << "-f, --force\tif sub-directory named <ROOT> exists in "
189         << "target directory, remove sub-directory before writing results.\n"
190         << "-g, --generate-config\twrite configuration file " 
191         << "to standard output and exit\n"
192         << "-h, --help\tdisplay this help and exit\n"
193         << "-r, --root=ROOT\tsvn controlled directory to perform "
194         << "statistics calculation on [" << root_ << "]\n"
195         << "    --revisions\tUse revision numbers as time scale "
196         << "instead of dates [dates].\n"
197         << "-t, --target=TARGET\toutput directory [" << targetdir_ << "]\n"
198         << "-v, --verbose\texplain what is being done\n"
199         << "    --version\tprint version information and exit\n";
200
201    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
202              << std::endl;
203  }
204
205
206  void Parameter::version(void) const
207  {
208    ColumnStream cs(std::cout, 1);
209    cs.width(0)=79;
210    cs << PACKAGE_STRING
211       << "\nCopyright (C) 2007 Jari Häkkinen and Peter Johansson.\n\n"
212       << "This is free software; see the source for copying conditions. "
213       << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR "
214       << "A PARTICULAR PURPOSE.\n";
215  }
216
217}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.