source: trunk/bin/Parameter.cc @ 116

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

fixes #48 #49

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