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

Last change on this file since 1178 was 1178, checked in by Peter, 13 years ago

fixes #344

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