source: trunk/bin/Parameter.cc @ 104

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

closes #39 checks that target and root exist, default parameter is changed to ., there is no longer a creation of a target directory, program burps if root directory already exists in the target directory.

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