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

Last change on this file since 974 was 974, checked in by Peter, 14 years ago

improve help output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1// $Id: Option.cc 974 2007-10-17 15:13:16Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of the yat library, http://trac.thep.lu.se/trac/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    : description_(desc), present_(false)
39  {
40    if (flag.empty())
41      throw std::runtime_error("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  std::string Option::description(void) const
64  {
65    return description_;
66  }
67
68
69  std::string Option::long_name(void) const
70  {
71    return long_name_;
72  }
73
74
75  void Option::parse(std::vector<std::string>::iterator first, 
76                     std::vector<std::string>::iterator last)
77  {
78    present_=true;
79    do_parse(first, last);
80  }
81
82
83  bool Option::present(void) const
84  {
85    return present_;
86  }
87
88
89  std::string Option::print(void)
90  {
91    return print1()+print2()+print3()+std::string("\t")+print4();
92  }
93
94
95  std::string Option::print1(void) const
96  {
97    if (short_name())
98      return std::string("-")+short_name()+std::string(",");
99    return std::string("   ");
100  }
101
102   
103  std::string Option::print2(void) const
104  {
105    if (long_name().size()) 
106      return std::string(" --")+long_name();
107    return std::string();
108  }
109
110
111  std::string Option::print3(void) const
112  {
113    return std::string();
114  }
115
116
117  std::string Option::print4(void) const
118  {
119    return description_;
120  }
121
122
123  void Option::reset(void)
124  {
125    present_=false;
126  }
127
128
129  char Option::short_name(void) const
130  {
131    return short_name_;
132  }
133
134
135  void Option::validate()
136  {
137    do_validate();
138  }
139
140}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.