source: trunk/yat/Option.cc @ 1556

Last change on this file since 1556 was 1556, checked in by Peter Johansson, 9 years ago

update to latest yat

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