source: trunk/bin/Parameter.cc @ 221

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

refs #36 optionally a copyright information, based on Stats, can be displayed using --copyright flag. This will flag will be added to usage output when this feature is mature.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1// $Id: Parameter.cc 198 2006-09-09 10:04:27Z peter $
2
3/*
4  Copyright (C) 2006 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#include "utility.h"
26#include <config.h> // this header file is created by configure
27
28#include <iostream>
29#include <stdexcept>
30#include <string>
31#include <sys/stat.h>
32
33namespace theplu {
34namespace svndigest {
35
36  Parameter::Parameter(const int argc,const char *argv[])
37  {
38    defaults();
39    for (int i=1; i<argc; ++i) {
40      bool ok=false;
41      std::string myargv(argv[i]);
42      if (myargv=="-f" || myargv=="--force"){
43          force_=true;
44          ok=true;
45      }
46      if (myargv=="--copyright"){
47          copyright_=true;
48          ok=true;
49      }
50      else if (myargv=="-h" || myargv=="--help"){
51        help();
52        exit(0);      // always exit after printing help
53      }
54      else if (myargv=="-r" || myargv=="--root"){
55        if (++i<argc){
56          root_= std::string(argv[i]);
57          ok=true;
58        }
59      }
60      else if (myargv=="-rev" || myargv=="--revisions") {
61          revisions_=true;
62          ok=true;
63      }
64      else if (myargv=="-t" || myargv=="--target"){
65        if (++i<argc){
66          targetdir_= std::string(argv[i]);
67          ok=true;
68        }
69      }
70      else if (myargv=="-v" || myargv=="--verbose"){
71          verbose_=true;
72          ok=true;
73      }
74      else if (myargv=="--version"){
75          version();
76          exit(0);
77      }
78      else if (myargv=="-vf" || myargv=="-fv"){
79          verbose_=true;
80          force_=true;
81          ok=true;
82      }
83
84      if (!ok)
85        throw std::runtime_error("svndigest: invalid option: " + myargv +
86                                 "\nType 'svndigest --help' for usage.");
87    }
88
89    analyse();
90  }
91
92
93  void Parameter::analyse(void)
94  {
95    using namespace std;
96
97    string workdir(pwd()); // remember current working directory (cwd).
98
99    // Checking that root_ exists and retrieve the absolute path to root_
100    if (chdir(root_.c_str()))
101      throw runtime_error(string("svndigest: Root directory (") + root_ +
102                          ") access failed.");
103    root_ = pwd();
104
105    // need to get back to cwd if relative paths are used.
106    if (chdir(workdir.c_str()))
107      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
108
109    // Checking that targetdir_ exists and retrieve the absolute path
110    // to targetdir_
111    if (chdir(targetdir_.c_str()))
112      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
113                          ") access failed.");
114    targetdir_ = pwd();
115    // Checking write permissions for targetdir_
116    if (access_rights(targetdir_,"w"))
117      throw runtime_error(string("svndigest: No write permission on target ") +
118                                 "directory (" + targetdir_ +").");
119
120    // return back to cwd
121    if (chdir(workdir.c_str()))
122      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
123  }
124
125
126  void Parameter::defaults(void)
127  {
128    copyright_=false;
129    force_=false;
130    revisions_=false;
131    root_=".";
132    targetdir_=".";
133    verbose_=false;
134  }
135
136
137  void Parameter::help(void)
138  {
139    defaults();
140    std::cout << "\n"
141              << "usage: svndigest [options]\n"
142              << "\n"
143              << "svndigest traverses a directory structure (controlled by\n"
144              << "subversion) and calculates developer statistics entries.\n"
145              << "The top level directory of the directory structure to\n"
146              << "traverse is set with the -r option. The result is written to\n"
147              << "the current working directory or a directory set with the\n"
148              << "-t option.\n"
149              << "\n"
150              << "Valid options:\n"
151              << "  -f [--force]   : remove target directory/file if it exists\n"
152              << "                   [no force]. NOTE recursive delete.\n"
153              << "  -h [--help]    : display this help and exit\n"
154              << "  -r [--root] arg : svn controlled directory to perform\n"
155              << "                    statistics calculation on [" 
156              << root_ << "]\n"
157              << "  -rev [--revisions]: Use revision numbers as time scale\n"
158              << "                      instead of dates [dates].\n"
159              << "  -t [--target] arg : output directory [" 
160              << targetdir_ << "]\n"
161              << "  -v [--verbose] : explain what is being done\n"
162              << "  --version      : print version information and exit\n"
163              << std::endl;
164  }
165
166
167  void Parameter::version(void) const
168  {
169    using namespace std;
170    cout << PACKAGE_STRING
171         << "\nCopyright (C) 2006 Jari Häkkinen and Peter Johansson.\n\n"
172         << "This is free software; see the source for copying conditions.\n"
173         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
174         << "A PARTICULAR PURPOSE." << endl;
175  }
176
177}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.