source: trunk/bin/Parameter.cc @ 138

Last change on this file since 138 was 122, checked in by Jari Häkkinen, 17 years ago

Cleaning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1// $Id: Parameter.cc 122 2006-07-23 07:30:20Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat 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  svnstat 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 svnstat {
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
75      if (!ok)
76        throw std::runtime_error("svnstat: invalid option: " + myargv +
77                                 "\nType 'svnstat --help' for usage.");
78    }
79
80    analyse();
81  }
82
83
84  void Parameter::analyse(void)
85  {
86    // making root absolute
87    std::string workdir(pwd());
88    chdir(root_.c_str());
89    root_ = pwd();
90    // remove trailing '/'
91    if (*root_.rbegin()=='/')
92      root_.erase(root_.begin()+root_.size()-1);
93    chdir(workdir.c_str());
94
95    // making targetdir absolute
96    chdir(targetdir_.c_str());
97    targetdir_ = pwd();
98    // remove trailing '/'
99    if (*targetdir_.rbegin()=='/')
100      root_.erase(targetdir_.begin()+targetdir_.size()-1);
101    chdir(workdir.c_str());
102
103    struct stat buf;
104    // check that root directory exists
105    if (stat(root_.c_str(),&buf))
106      throw std::runtime_error("\nsvnstat: " + root_ + ": No such directory.");
107
108    // check that target directory exists
109    if (stat(targetdir_.c_str(),&buf)){
110      throw std::runtime_error("\nsvnstat: " + targetdir_ + 
111                               ": No such directory.");
112    }
113
114  }
115
116
117  void Parameter::defaults(void)
118  {
119    force_=false;
120    revisions_=false;
121    root_=".";
122    targetdir_=".";
123    verbose_=false;
124  }
125
126
127  void Parameter::help(void)
128  {
129    defaults();
130    std::cout << "\n"
131              << "usage: svnstat [options]\n"
132              << "\n"
133              << "svnstat traverses a directory structure (controlled by\n"
134              << "subversion) and calculates developer statistics entries.\n"
135              << "The top level directory of the directory structure to\n"
136              << "traverse is set with the -r option. The result is written to\n"
137              << "the current working directory or a directory set with the\n"
138              << "-t option.\n"
139              << "\n"
140              << "Valid options:\n"
141              << "  -f [--force]   : remove target directory/file if it exists\n"
142              << "                   [no force]. NOTE recursive delete.\n"
143              << "  -h [--help]    : display this help and exit\n"
144              << "  -r [--root] arg : svn controlled directory to perform\n"
145              << "                    statistics calculation on [" 
146              << root_ << "]\n"
147              << "  -rev [--revisions]: Use revision numbers as time scale\n"
148              << "                      instead of dates [dates].\n"
149              << "  -t [--target] arg : output directory [" 
150              << targetdir_ << "]\n"
151              << "  -v [--verbose] : explain what is being done\n"
152              << "  --version      : print version information and exit\n"
153              << std::endl;
154  }
155
156
157  void Parameter::version(void) const
158  {
159    using namespace std;
160    cout << PACKAGE_STRING
161         << "\nCopyright (C) 2006 Jari Häkkinen and Peter Johansson.\n\n"
162         << "This is free software; see the source for copying conditions.\n"
163         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
164         << "A PARTICULAR PURPOSE." << endl;
165  }
166
167}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.