source: trunk/bin/Parameter.cc @ 112

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

closes #43 throwing exceptions in Parameter

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1// $Id: Parameter.cc 106 2006-06-28 09:07:45Z 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 <config.h> // this header file is created by configure
26
27#include <iostream>
28#include <stdexcept>
29#include <string>
30#include <sys/stat.h>
31
32namespace theplu {
33namespace svnstat {
34
35  Parameter::Parameter(const int argc,const char *argv[])
36  {
37    defaults();
38    for (int i=1; i<argc; i++) {
39      bool ok=false;
40      std::string myargv(argv[i]);
41      if (myargv=="-f" || myargv=="--force"){
42          force_=true;
43          ok=true;
44      }
45      else if (myargv=="-h" || myargv=="--help"){
46        help();
47        exit(0);      // always exit after printing help
48      }
49      else if (myargv=="-r" || myargv=="--root"){
50        if (++i<argc){
51          root_= std::string(argv[i]);
52          // if last character is '/' remove
53          if (*root_.rbegin()=='/')
54            root_.erase(root_.begin()+root_.size()-1);
55          ok=true;
56        }
57      }
58      else if (myargv=="-rev" || myargv=="--revisions") {
59          revisions_=true;
60          ok=true;
61      }
62      else if (myargv=="-t" || myargv=="--target"){
63        if (++i<argc){
64          targetdir_= std::string(argv[i]);
65          // if last character is '/' remove
66          if (*targetdir_.rbegin()=='/')
67            targetdir_.erase(targetdir_.begin()+targetdir_.size()-1);
68          ok=true;
69        }
70      }
71      else if (myargv=="-v" || myargv=="--verbose"){
72          verbose_=true;
73          ok=true;
74      }
75      else if (myargv=="--version"){
76          version();
77          exit(0);
78      }
79
80      if (!ok)
81        throw std::runtime_error("svnstat: invalid option: " + myargv +
82                                 "\nType 'svnstat --help' for usage.");
83    }
84
85    analyse();
86  }
87
88
89  void Parameter::analyse(void)
90  {
91    struct stat buf;
92    // check that root directory exists
93    if (stat(root_.c_str(),&buf))
94      throw std::runtime_error("\nsvnstat: " + root_ + ": No such directory.");
95
96    // check that target directory exists
97    if (stat(targetdir_.c_str(),&buf)){
98      throw std::runtime_error("\nsvnstat: " + targetdir_ + 
99                               ": No such directory.");
100    }
101  }
102
103
104  void Parameter::defaults(void)
105  {
106    force_=false;
107    revisions_=false;
108    root_=".";
109    targetdir_=".";
110    verbose_=false;
111  }
112
113
114  void Parameter::help(void)
115  {
116    std::cout << "\n"
117              << "usage: svnstat [options]\n"
118              << "\n"
119              << "svnstat traverses a directory structure (controlled by\n"
120              << "subversion) and calculates developer statistics entries.\n"
121              << "The top level directory of the directory structure to\n"
122              << "traverse is set with the -t option."
123              << "The result is written to a\n"
124              << "sub-directory, svnstat, that is created in the current\n"
125              << "working directory.\n"
126              << "\n"
127              << "Valid options:\n"
128              << "  -f [--force]   : remove target directory/file if it exists\n"
129              << "                   [no force]. NOTE recursive delete.\n"
130              << "  -h [--help]    : display this help and exit\n"
131              << "  -r [--root] arg : svn controlled directory to perform\n"
132              << "                    statistics calculation on [.]\n"
133              << "  -rev [--revisions]: Use revision numbers as time scale\n"
134              << "                      instead of dates [dates].\n"
135              << "  -t [--target] arg : output directory [.]\n"
136              << "  -v [--verbose] : explain what is being done\n"
137              << "  --version      : print version information and exit\n"
138              << std::endl;
139  }
140
141
142  void Parameter::version(void) const
143  {
144    using namespace std;
145    cout << PACKAGE_STRING
146         << "\nCopyright (C) 2006 Jari Häkkinen and Peter Johansson.\n\n"
147         << "This is free software; see the source for copying conditions.\n"
148         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
149         << "A PARTICULAR PURPOSE." << endl;
150  }
151
152}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.