source: trunk/yat/utility/Option.cc @ 1566

Last change on this file since 1566 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1// $Id: Option.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 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 "Option.h"
24#include "CommandLine.h"
25
26#include <iostream>
27#include <sstream>
28#include <stdexcept>
29#include <string>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35
36  Option::Option(CommandLine& cmd, std::string flag, std::string desc)
37    : cmd_(cmd), description_(desc), present_(false)
38  {
39    if (flag.empty())
40      throw std::runtime_error("yat: Option: given flag is empty");
41    if (flag.size()==1 || (flag.size()==2 && flag[1]==','))
42      short_name_ = flag[0];
43    else if (flag[1]==','){
44      short_name_ = flag[0];
45      long_name_ = flag.substr(2);
46      if (long_name_.size()==1)
47        long_name_="";
48    }
49    else {
50      short_name_ = '\0';
51      long_name_=flag;
52    }
53    cmd.add(*this);
54  }
55
56
57  Option::~Option(void)
58  {
59  }
60
61
62  const CommandLine& Option::cmd(void) const
63  {
64    return cmd_;
65  }
66
67
68  std::string Option::description(void) const
69  {
70    return description_;
71  }
72
73
74  std::string Option::long_name(void) const
75  {
76    return long_name_;
77  }
78
79
80  void Option::parse(std::vector<std::string>::iterator& first, 
81                     const std::vector<std::string>::iterator& last)
82  {
83    present_=true;
84    do_parse(first, last);
85  }
86
87
88  bool Option::present(void) const
89  {
90    return present_;
91  }
92
93
94  std::string Option::print(void)
95  {
96    return print1()+print2()+print3()+std::string("\t")+print4();
97  }
98
99
100  std::string Option::print1(void) const
101  {
102    std::string str;
103    if (short_name()){
104      str = std::string("-")+short_name();
105      if (!long_name().empty())
106        str += std::string(",");
107    }
108    else
109      str = std::string("   ");
110    return str;
111  }
112
113   
114  std::string Option::print2(void) const
115  {
116    if (long_name().size()) 
117      return std::string(" --")+long_name();
118    return std::string();
119  }
120
121
122  std::string Option::print3(void) const
123  {
124    return std::string();
125  }
126
127
128  std::string Option::print4(void) const
129  {
130    return description_;
131  }
132
133
134  void Option::reset(void)
135  {
136    present_=false;
137  }
138
139
140  char Option::short_name(void) const
141  {
142    return short_name_;
143  }
144
145
146  void Option::validate()
147  {
148    do_validate();
149  }
150
151}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.