source: trunk/bin/Parameter.cc @ 196

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

fixes #71 concatenation of parameters -v and -f is now allowed

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