source: trunk/lib/Option.cc @ 791

Last change on this file since 791 was 705, checked in by Peter Johansson, 13 years ago

importing classes for commandline parsing from yat. This fixes #349 and #265

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: Option.cc 705 2008-11-25 23:33:26Z peter $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Option.h"
24#include "CommandLine.h"
25
26#include <iostream>
27#include <sstream>
28#include <stdexcept>
29#include <string>
30
31namespace theplu {
32namespace svndigest {
33
34  Option::Option(CommandLine& cmd, std::string flag, std::string desc)
35    : cmd_(cmd), description_(desc), present_(false)
36  {
37    if (flag.empty())
38      throw std::runtime_error("svndigest: Option: given flag is empty");
39    if (flag.size()==1 || (flag.size()==2 && flag[1]==','))
40      short_name_ = flag[0];
41    else if (flag[1]==','){
42      short_name_ = flag[0];
43      long_name_ = flag.substr(2);
44      if (long_name_.size()==1)
45        long_name_="";
46    }
47    else {
48      short_name_ = '\0';
49      long_name_=flag;
50    }
51    cmd.add(*this);
52  }
53
54
55  Option::~Option(void)
56  {
57  }
58
59
60  const CommandLine& Option::cmd(void) const
61  {
62    return cmd_;
63  }
64
65
66  std::string Option::description(void) const
67  {
68    return description_;
69  }
70
71
72  std::string Option::long_name(void) const
73  {
74    return long_name_;
75  }
76
77
78  void Option::parse(std::vector<std::string>::iterator& first, 
79                     const std::vector<std::string>::iterator& last)
80  {
81    present_=true;
82    do_parse(first, last);
83  }
84
85
86  bool Option::present(void) const
87  {
88    return present_;
89  }
90
91
92  std::string Option::print(void)
93  {
94    return print1()+print2()+print3()+std::string("\t")+print4();
95  }
96
97
98  std::string Option::print1(void) const
99  {
100    std::string str;
101    if (short_name()){
102      str = std::string("-")+short_name();
103      if (!long_name().empty())
104        str += std::string(",");
105    }
106    else
107      str = std::string("   ");
108    return str;
109  }
110
111   
112  std::string Option::print2(void) const
113  {
114    if (long_name().size()) 
115      return std::string(" --")+long_name();
116    return std::string();
117  }
118
119
120  std::string Option::print3(void) const
121  {
122    return std::string();
123  }
124
125
126  std::string Option::print4(void) const
127  {
128    return description_;
129  }
130
131
132  void Option::reset(void)
133  {
134    present_=false;
135  }
136
137
138  char Option::short_name(void) const
139  {
140    return short_name_;
141  }
142
143
144  void Option::validate()
145  {
146    do_validate();
147  }
148
149}} // of namespace svndigest and theplu
Note: See TracBrowser for help on using the repository browser.