Compare commits

..

4 Commits

Author SHA1 Message Date
nobody
c8f95dbda3 This commit was manufactured by cvs2svn to create tag
'Version_1_33_1'.

[SVN r31916]
2005-12-05 14:04:06 +00:00
Vladimir Prus
81aff160d0 Merge fixes for unregisted options and inaccessible 'style_parser' method
from trunk to RC_1_33_0.


[SVN r31615]
2005-11-10 08:46:56 +00:00
Dave Abrahams
705e79b5ee merged from trunk
[SVN r30560]
2005-08-12 19:50:06 +00:00
nobody
3ae92db179 This commit was manufactured by cvs2svn to create branch 'RC_1_33_0'.
[SVN r30300]
2005-07-28 18:22:24 +00:00
45 changed files with 361 additions and 618 deletions

49
build/Jamfile Normal file
View File

@@ -0,0 +1,49 @@
subproject libs/program_options/build ;
SOURCES = cmdline config_file options_description parsers variables_map
value_semantic positional_options utf8_codecvt_facet convert
winmain
;
lib boost_program_options
: ../src/$(SOURCES).cpp
: # build requirements
[ common-names ] # magic for install and auto-link features
<include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)
std::locale-support
: debug release # build variants
;
dll boost_program_options
: ../src/$(SOURCES).cpp
: # build requirements
[ common-names ] # magic for install and auto-link features
<define>BOOST_ALL_DYN_LINK=1 # tell source we're building dll's
<runtime-link>dynamic # build only for dynamic runtimes
<include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)
# The following really turns on static runtime linking
# which leads to runtime crashes when using DLL, so
# seem DLL is not usable on Metrowerks 8
# std::facet-support std::locale-support
: debug release # build variants
;
install program_options lib
: <lib>boost_program_options <dll>boost_program_options
;
stage stage/lib : <lib>boost_program_options <dll>boost_program_options
:
# copy to a path rooted at BOOST_ROOT:
<locate>$(BOOST_ROOT)
# make sure the names of the libraries are correctly named:
[ common-names ]
# add this target to the "stage" and "all" psuedo-targets:
<target>stage
<target>all
:
debug release
;
# end

View File

@@ -17,4 +17,12 @@ lib boost_program_options
<link>shared:<define>BOOST_PROGRAM_OPTIONS_DYN_LINK=1 # tell source we're building dll's
;
boost-install boost_program_options ;
install dist-lib
:
boost_program_options
:
<install-type>LIB
<location>../../../dist/lib
;
explicit dist-lib ;

View File

@@ -2,10 +2,7 @@
import toolset ;
toolset.using doxygen ;
boostbook program_option
: program_options.xml
: <implicit-dependency>autodoc
;
boostbook program_option : program_options.xml ;
doxygen autodoc
: [ glob ../../../boost/program_options/*.hpp ] ;

View File

@@ -52,7 +52,8 @@
<listitem>
<para>ascii input passed to a Unicode value, and Unicode input
passed to an ascii value will be converted using a codecvt
facet (which may be specified by the user).
facet (which may be specified by the user(which can be
specified by the user)
</para>
</listitem>
</itemizedlist>
@@ -82,7 +83,7 @@
</para>
<para>The Unicode support outlined above is not complete. For example, we
don't support Unicode option names. Unicode support is hard and
don't plan allow Unicode in option names. Unicode support is hard and
requires a Boost-wide solution. Even comparing two arbitrary Unicode
strings is non-trivial. Finally, using Unicode in option names is
related to internationalization, which has it's own

View File

@@ -184,7 +184,7 @@ store(command_line_parser(args).options(desc).run(), vm);
options_description general("General options");
general.add_options()
("help", "produce a help message")
("help-module", value<string>(),
("help-module", value<string>()->implicit(),
"produce a help for a given module")
("version", "output the version number")
;
@@ -284,7 +284,7 @@ void validate(boost::any& v,
using namespace boost::program_options;
// Make sure no previous assignment to 'a' was made.
validators::check_first_occurrence(v);
validators::check_first_occurence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const string& s = validators::get_single_string(values);
@@ -412,7 +412,7 @@ $ export LC_CTYPE=ru_RU.KOI8-R
method of that class:
<programlisting>
parsed_options parsed =
command_line_parser(argc, argv).options(desc).allow_unregistered().run();
command_line_parser(argv, argc).options(desc).allow_unregistered().run();
</programlisting>
For each token that looks like an option, but does not have a known name,
@@ -427,7 +427,7 @@ parsed_options parsed =
The function will collect original tokens for all unrecognized values, and optionally, all found positional options.
Say, if your code handles a few options, but does not handles positional options at all, you can use the function like this:
<programlisting>
vector&lt;string&gt; to_pass_further = collect_unrecognized(parsed.options, include_positional);
vector&lt;string&gt; to_pass_further = collect_arguments(parsed.option, include_positional);
</programlisting>
</para>

View File

@@ -4,11 +4,6 @@
</head>
<body>
Automatic redirection failed, please go to
<a href="../../../doc/html/program_options.html">../../../doc/html/program_options.html</a>
&nbsp;<hr>
<p>© Copyright Beman Dawes, 2001</p>
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
file <a href="../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
<a href="../../../doc/html/program_options.html">../../../doc/html/program_options.html</a>
</body>
</html>

View File

@@ -181,7 +181,7 @@ options_description desc;
desc.add_options()
("help", "produce help message")
("compression", value&lt;string&gt;(), "compression level")
("verbose", value&lt;string&gt;()->zero_tokens(), "verbosity level")
("verbose", value&lt;string&gt;()->implicit(), "verbosity level")
("email", value&lt;string&gt;()->multitoken(), "email to send to")
;
</programlisting>
@@ -348,18 +348,11 @@ positional_options_description pd; pd.add("input-file", 1);
given the same name.
<programlisting>
positional_options_description pd;
pd.add("output-file", 2).add("input-file", -1);
pd.add("output-file", 2).add_optional("input-file", -1);
</programlisting>
In the above example, the first two positional options will be associated
with name "output-file", and any others with the name "input-file".
</para>
<warning>
<para>The &positional_options_desc; class only specifies translation from
position to name, and the option name should still be registered with
an instance of the &options_description; class.</para>
</warning>
</section>
@@ -478,45 +471,7 @@ notify(vm);
<title>Specific parsers</title>
<section>
<title>Configuration file parser</title>
<para>The &parse_config_file; function implements parsing
of simple INI-like configuration files. Configuration file
syntax is line based:
</para>
<itemizedlist>
<listitem><para>A line in the form:</para>
<screen>
<replaceable>name</replaceable>=<replaceable>value</replaceable>
</screen>
<para>gives a value to an option.</para>
</listitem>
<listitem><para>A line in the form:</para>
<screen>
[<replaceable>section name</replaceable>]
</screen>
<para>introduces a new section in the configuration file.</para>
</listitem>
<listitem><para>The <literal>#</literal> character introduces a
comment that spans until the end of the line.</para>
</listitem>
</itemizedlist>
<para>The option names are relative to the section names, so
the following configuration file part:</para>
<screen>
[gui.accessibility]
visual_bell=yes
</screen>
<para>is equivalent to</para>
<screen>
gui.accessibility.visual_bell=yes
</screen>
</section>
<section>
<title>Environment variables parser</title>
<title>Environment variables</title>
<para><firstterm>Environment variables</firstterm> are string variables
which are available to all programs via the <code>getenv</code> function
@@ -526,7 +481,7 @@ gui.accessibility.visual_bell=yes
<filename>autoexec.bat</filename> file or (on recent versions) the
<filename>Control Panel/System/Advanced/Environment Variables</filename>
dialog, and on Unix &#x2014;, the <filename>/etc/profile</filename>,
<filename>~/.profile</filename> and <filename>~/.bash_profile</filename>
<filename>~/profile</filename> and <filename>~/bash_profile</filename>
files. Because environment variables can be set for the entire system,
they are particularly suitable for options which apply to all programs.
</para>
@@ -605,15 +560,8 @@ gui.accessibility.visual_bell=yes
<row>
<entry>&parse_command_line;</entry>
<entry>parses command line (simpified interface)</entry>
<entry>parses command line</entry>
</row>
<row>
<entry>&basic_command_line_parser;</entry>
<entry>parses command line (extended interface)</entry>
</row>
<row>
<entry>&parse_config_file;</entry>
<entry>parses config file</entry>

View File

@@ -83,5 +83,5 @@
<xi:include href="design.xml"/>
<xi:include href="acknowledgements.xml"/>
<xi:include href="autodoc.xml"/>
<xi:include href="autodoc.boostbook"/>
</library>

View File

@@ -1,6 +1,4 @@
Say that variables_map is actually derived from std::map.
Make parse_config_file("foo.cfg", desc) work.
Document handling of positional options which depends on precedding options.

View File

@@ -96,7 +96,7 @@ Compression level was set to 10.
<para>An option value, surely, can have other types than <code>int</code>, and
can have other interesting properties, which we'll discuss right now. The
complete version of the code snipped below can be found in
<filename>example/options_description.cpp</filename>.</para>
"example/options_description.cpp".</para>
<para>Imagine we're writing a compiler. It should take the optimization
level, a number of include paths, and a number of input files, and perform some
@@ -115,29 +115,21 @@ desc.add_options()
</programlisting>
</para>
<para>The <literal>"help"</literal> option should be familiar from
the previous example. It's a good idea to have this option in all cases.
</para>
<para>The "--help" option should be familiar from the previous example.
It's a good idea to have this option in all cases.</para>
<para>The <literal>"optimization"</literal> option shows two new features. First, we specify
<para>The "optimization" option shows two new features. First, we specify
the address of the variable(<code>&amp;opt</code>). After storing values, that
variable will have the value of the option. Second, we specify a default
value of 10, which will be used if no value is specified by the user.
</para>
<para>The <literal>"include-path"</literal> option is an example of the
only case where the interface of the <code>options_description</code>
class serves only one
<para>The "include-path" option is an example of the only case where
the interface of the <code>options_description</code> class serves only one
source -- the command line. Users typically like to use short option names
for common options, and the "include-path,I" name specifies that short
option name is "I". So, both "--include-path" and "-I" can be used.
</para>
<para>Note also that the type of the <literal>"include-path"</literal>
option is <type>std::vector</type>. The library provides special
support for vectors -- it will be possible to specify the option several
times, and all specified values will be collected in one vector.
</para>
<para>The "input-file" option specifies the list of files to
process. That's okay for a start, but, of course, writing something like:
@@ -345,7 +337,7 @@ Optimization level is 4
<!--
Local Variables:
mode: nxml
mode: xml
sgml-indent-data: t
sgml-parent-document: ("program_options.xml" "section")
sgml-set-face: t

23
example/Jamfile Normal file
View File

@@ -0,0 +1,23 @@
subproject libs/program_options/example ;
rule program-options-example ( name extra-sources * )
{
exe $(name) : $(name).cpp <lib>../build/boost_program_options $(extra-sources)
: <include>$(BOOST_ROOT) ;
}
program-options-example first ;
program-options-example options_description ;
program-options-example multiple_sources ;
program-options-example custom_syntax ;
program-options-example response_file ;
program-options-example option_groups ;
program-options-example real ;
program-options-example regex <lib>../../regex/build/boost_regex ;
#program-options-example prefix ;

View File

@@ -1,6 +1,6 @@
project
: requirements <library>../build//boost_program_options
: requirements <library>../build//program_options
<hardcode-dll-paths>true
;
@@ -9,5 +9,7 @@ exe options_description : options_description.cpp ;
exe multiple_sources : multiple_sources.cpp ;
exe custom_syntax : custom_syntax.cpp ;
exe real : real.cpp ;
exe regex : regex.cpp /boost/regex//boost_regex ;
exe a : a.cpp ;
#exe real : real.cpp ;
#exe regex : regex.cpp /boost/regex//boost_regex ;

View File

@@ -26,17 +26,11 @@ int main(int ac, char* av[])
{
try {
int opt;
int portnum;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("optimization", po::value<int>(&opt)->default_value(10),
"optimization level")
("verbose,v", po::value<int>()->implicit_value(1),
"enable verbosity (optionally specify level)")
("listen,l", po::value<int>(&portnum)->implicit_value(1001)
->default_value(0,"no"),
"listen on a port.")
("include-path,I", po::value< vector<string> >(),
"include path")
("input-file", po::value< vector<string> >(), "input file")
@@ -68,14 +62,7 @@ int main(int ac, char* av[])
<< vm["input-file"].as< vector<string> >() << "\n";
}
if (vm.count("verbose")) {
cout << "Verbosity enabled. Level is " << vm["verbose"].as<int>()
<< "\n";
}
cout << "Optimization level is " << opt << "\n";
cout << "Listen port is " << portnum << "\n";
}
catch(exception& e)
{

View File

@@ -3,8 +3,6 @@
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See www.boost.org/libs/program_options for documentation.
#ifndef PROGRAM_OPTIONS_VP_2003_05_19
#define PROGRAM_OPTIONS_VP_2003_05_19

View File

@@ -12,13 +12,13 @@ namespace boost { namespace program_options { namespace command_line_style {
There are "long" options, which start with "--" and "short",
which start with either "-" or "/". Both kinds can be allowed or
disallowed, see allow_long and allow_short. The allowed character
for short options is also configurable.
for short option is also configurable.
Option's value can be specified in the same token as name
Option's value can be specified in the same token as value
("--foo=bar"), or in the next token.
It's possible to introduce long options by the same character as
short options, see allow_long_disguise.
It's possible to introduce long option by the same character as
long option, see allow_long_disguise.
Finally, guessing (specifying only prefix of option) and case
insensitive processing are supported.
@@ -39,7 +39,7 @@ namespace boost { namespace program_options { namespace command_line_style {
@endverbatim
*/
long_allow_adjacent = allow_slash_for_short << 1,
/** Allow option parameter in the next token for
/** Allow option parameter in the same token for
long options. */
long_allow_next = long_allow_adjacent << 1,
/** Allow option parameter in the same token for
@@ -59,7 +59,7 @@ namespace boost { namespace program_options { namespace command_line_style {
/** Allow abbreviated spellings for long options,
if they unambiguously identify long option.
No long option name should be prefix of other
long option name if guessing is in effect.
long option name is guessing is in effect.
*/
allow_guessing = allow_sticky << 1,
/** Ignore the difference in case for options.

View File

@@ -70,8 +70,7 @@ namespace boost { namespace program_options { namespace detail {
public:
common_config_file_iterator() { found_eof(); }
common_config_file_iterator(
const std::set<std::string>& allowed_options,
bool allow_unregistered = false);
const std::set<std::string>& allowed_options);
virtual ~common_config_file_iterator() {}
@@ -104,7 +103,6 @@ namespace boost { namespace program_options { namespace detail {
// Invariant: no element is prefix of other element.
std::set<std::string> allowed_prefixes;
std::string m_prefix;
bool m_allow_unregistered;
};
template<class charT>
@@ -118,8 +116,7 @@ namespace boost { namespace program_options { namespace detail {
/** Creates a config file parser for the specified stream.
*/
basic_config_file_iterator(std::basic_istream<charT>& is,
const std::set<std::string>& allowed_options,
bool allow_unregistered = false);
const std::set<std::string>& allowed_options);
private: // base overrides
@@ -142,9 +139,8 @@ namespace boost { namespace program_options { namespace detail {
template<class charT>
basic_config_file_iterator<charT>::
basic_config_file_iterator(std::basic_istream<charT>& is,
const std::set<std::string>& allowed_options,
bool allow_unregistered)
: common_config_file_iterator(allowed_options, allow_unregistered)
const std::set<std::string>& allowed_options)
: common_config_file_iterator(allowed_options)
{
this->is.reset(&is, null_deleter());
get();

View File

@@ -38,7 +38,7 @@ namespace boost { namespace program_options {
: detail::cmdline(
// Explicit template arguments are required by gcc 3.3.1
// (at least mingw version), and do no harm on other compilers.
to_internal(detail::make_vector<charT, charT**>(argv+1, argv+argc+!argc)))
to_internal(detail::make_vector<charT, charT**>(argv+1, argv+argc)))
{}

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
// Copyright © 2001 Ronald Garcia, Indiana University (garcia@osl.iu.edu)
// Andrew Lumsdaine, Indiana University (lums@osl.iu.edu). Permission to copy,
// use, modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided "as is"

View File

@@ -16,13 +16,7 @@ namespace boost { namespace program_options {
std::string
typed_value<T, charT>::name() const
{
if (!m_implicit_value.empty() && !m_implicit_value_as_text.empty()) {
std::string msg = "[=arg(=" + m_implicit_value_as_text + ")]";
if (!m_default_value.empty() && !m_default_value_as_text.empty())
msg += " (=" + m_default_value_as_text + ")";
return msg;
}
else if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
if (!m_default_value.empty() && !m_default_value_as_text.empty()) {
return arg + " (=" + m_default_value_as_text + ")";
} else {
return arg;
@@ -140,14 +134,7 @@ namespace boost { namespace program_options {
for (unsigned i = 0; i < s.size(); ++i)
{
try {
/* We call validate so that if user provided
a validator for class T, we use it even
when parsing vector<T>. */
boost::any a;
std::vector<std::basic_string<charT> > v;
v.push_back(s[i]);
validate(a, v, (T*)0, 0);
tv->push_back(boost::any_cast<T>(a));
tv->push_back(boost::lexical_cast<T>(s[i]));
}
catch(const bad_lexical_cast& /*e*/) {
boost::throw_exception(invalid_option_value(s[i]));
@@ -161,13 +148,7 @@ namespace boost { namespace program_options {
xparse(boost::any& value_store,
const std::vector<std::basic_string<charT> >& new_tokens) const
{
// If no tokens were given, and the option accepts an implicit
// value, then assign the implicit value as the stored value;
// otherwise, validate the user-provided token(s).
if (new_tokens.empty() && !m_implicit_value.empty())
value_store = m_implicit_value;
else
validate(value_store, new_tokens, (T*)0, 0);
validate(value_store, new_tokens, (T*)0, 0);
}
template<class T>

View File

@@ -82,12 +82,11 @@ namespace boost { namespace program_options {
validation_error(const std::string& what) : error(what) {}
~validation_error() throw() {}
void set_option_name(const std::string& option);
const char* what() const throw();
private:
mutable std::string m_message; // For on-demand formatting in 'what'
std::string m_option_name; // The name of the option which
// caused the exception.
const char* what() const throw();
};
class BOOST_PROGRAM_OPTIONS_DECL invalid_option_value

View File

@@ -78,12 +78,10 @@ namespace program_options {
virtual ~option_description();
enum match_result { no_match, full_match, approximate_match };
/** Given 'option', specified in the input source,
return 'true' is 'option' specifies *this.
*/
match_result match(const std::string& option, bool approx) const;
bool match(const std::string& option, bool approx) const;
/** Return the key that should identify the option, in
particular in the variables_map class.
@@ -155,7 +153,7 @@ namespace program_options {
*/
class BOOST_PROGRAM_OPTIONS_DECL options_description {
public:
static const unsigned m_default_line_length;
static const unsigned m_default_line_length = 80;
/** Creates the instance. */
options_description(unsigned line_length = m_default_line_length);

View File

@@ -153,8 +153,7 @@ namespace boost { namespace program_options {
BOOST_PROGRAM_OPTIONS_DECL
#endif
basic_parsed_options<charT>
parse_config_file(std::basic_istream<charT>&, const options_description&,
bool allow_unregistered = false);
parse_config_file(std::basic_istream<charT>&, const options_description&);
/** Controls if the 'collect_unregistered' function should
include positional options, or not. */

View File

@@ -37,8 +37,7 @@ namespace boost { namespace program_options {
No calls to 'add' can be made after call with 'max_value' equal to
'-1'.
*/
positional_options_description&
add(const char* name, int max_count);
void add(const char* name, int max_count);
/** Returns the maximum number of positional options that can
be present. Can return numeric_limits<unsigned>::max() to

View File

@@ -14,9 +14,9 @@
#include <boost/lexical_cast.hpp>
#include <string>
#include <vector>
#include <typeinfo>
namespace boost { namespace program_options {
@@ -74,13 +74,6 @@ namespace boost { namespace program_options {
// Nothing here. Specializations to follow.
};
/** Helper conversion class for values that accept ascii
strings as input.
Overrides the 'parse' method and defines new 'xparse'
method taking std::string. Depending on whether input
to parse is ascii or UTF8, will pass it to xparse unmodified,
or with UTF8->ascii conversion.
*/
template<>
class BOOST_PROGRAM_OPTIONS_DECL
value_semantic_codecvt_helper<char> : public value_semantic {
@@ -94,13 +87,6 @@ namespace boost { namespace program_options {
const = 0;
};
/** Helper conversion class for values that accept ascii
strings as input.
Overrides the 'parse' method and defines new 'xparse'
method taking std::wstring. Depending on whether input
to parse is ascii or UTF8, will recode input to Unicode, or
pass it unmodified.
*/
template<>
class BOOST_PROGRAM_OPTIONS_DECL
value_semantic_codecvt_helper<wchar_t> : public value_semantic {
@@ -115,7 +101,6 @@ namespace boost { namespace program_options {
const = 0;
#endif
};
/** Class which specifies a simple handling of a value: the value will
have string type and only one token is allowed. */
class BOOST_PROGRAM_OPTIONS_DECL
@@ -149,29 +134,9 @@ namespace boost { namespace program_options {
bool m_zero_tokens;
};
/** Base class for all option that have a fixed type, and are
willing to announce this type to the outside world.
Any 'value_semantics' for which you want to find out the
type can be dynamic_cast-ed to typed_value_base. If conversion
succeeds, the 'type' method can be called.
*/
class typed_value_base
{
public:
// Returns the type of the value described by this
// object.
virtual const std::type_info& value_type() const = 0;
// Not really needed, since deletion from this
// class is silly, but just in case.
virtual ~typed_value_base() {}
};
/** Class which handles value of a specific type. */
template<class T, class charT = char>
class typed_value : public value_semantic_codecvt_helper<charT>,
public typed_value_base
{
class typed_value : public value_semantic_codecvt_helper<charT> {
public:
/** Ctor. The 'store_to' parameter tells where to store
the value when it's known. The parameter can be NULL. */
@@ -204,38 +169,6 @@ namespace boost { namespace program_options {
return this;
}
/** Specifies an implicit value, which will be used
if the option is given, but without an adjacent value.
Using this implies that an explicit value is optional, but if
given, must be strictly adjacent to the option, i.e.: '-ovalue'
or '--option=value'. Giving '-o' or '--option' will cause the
implicit value to be applied.
*/
typed_value* implicit_value(const T &v)
{
m_implicit_value = boost::any(v);
m_implicit_value_as_text =
boost::lexical_cast<std::string>(v);
return this;
}
/** Specifies an implicit value, which will be used
if the option is given, but without an adjacent value.
Using this implies that an explicit value is optional, but if
given, must be strictly adjacent to the option, i.e.: '-ovalue'
or '--option=value'. Giving '-o' or '--option' will cause the
implicit value to be applied.
Unlike the above overload, the type 'T' need not provide
operator<< for ostream, but textual representation of default
value must be provided by the user.
*/
typed_value* implicit_value(const T &v, const std::string& textual)
{
m_implicit_value = boost::any(v);
m_implicit_value_as_text = textual;
return this;
}
/** Specifies a function to be called when the final value
is determined. */
typed_value* notifier(function1<void, const T&> f)
@@ -275,7 +208,7 @@ namespace boost { namespace program_options {
unsigned min_tokens() const
{
if (m_zero_tokens || !m_implicit_value.empty()) {
if (m_zero_tokens) {
return 0;
} else {
return 1;
@@ -294,7 +227,7 @@ namespace boost { namespace program_options {
/** Creates an instance of the 'validator' class and calls
its operator() to perform the actual conversion. */
its operator() to perform athe ctual conversion. */
void xparse(boost::any& value_store,
const std::vector< std::basic_string<charT> >& new_tokens)
const;
@@ -317,13 +250,6 @@ namespace boost { namespace program_options {
when creating *this, stores the value there. Otherwise,
does nothing. */
void notify(const boost::any& value_store) const;
public: // typed_value_base overrides
const std::type_info& value_type() const
{
return typeid(T);
}
private:
@@ -333,8 +259,6 @@ namespace boost { namespace program_options {
// as boost::optional to avoid unnecessary instantiations.
boost::any m_default_value;
std::string m_default_value_as_text;
boost::any m_implicit_value;
std::string m_implicit_value_as_text;
bool m_composing, m_implicit, m_multitoken, m_zero_tokens;
boost::function1<void, const T&> m_notifier;
};

View File

@@ -24,29 +24,6 @@ namespace boost { namespace program_options {
class value_semantic;
class variables_map;
// forward declaration
/** Stores in 'm' all options that are defined in 'options'.
If 'm' already has a non-defaulted value of an option, that value
is not changed, even if 'options' specify some value.
*/
BOOST_PROGRAM_OPTIONS_DECL
void store(const basic_parsed_options<char>& options, variables_map& m,
bool utf8 = false);
/** Stores in 'm' all options that are defined in 'options'.
If 'm' already has a non-defaulted value of an option, that value
is not changed, even if 'options' specify some value.
This is wide character variant.
*/
BOOST_PROGRAM_OPTIONS_DECL
void store(const basic_parsed_options<wchar_t>& options,
variables_map& m);
/** Runs all 'notify' function for options in 'm'. */
BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
/** Class holding value of option. Contains details about how the
value is set and allows to conveniently obtain the value.
*/
@@ -59,15 +36,10 @@ namespace boost { namespace program_options {
/** If stored value if of type T, returns that value. Otherwise,
throws boost::bad_any_cast exception. */
template<class T>
const T& as() const {
return boost::any_cast<const T&>(v);
}
/** @overload */
template<class T>
T& as() {
return boost::any_cast<T&>(v);
}
template<class T> const T& as() const;
/** @overload */
template<class T> T& as();
/// Returns true if no value is stored.
bool empty() const;
@@ -89,10 +61,10 @@ namespace boost { namespace program_options {
// be easily accessible, so we need to store semantic here.
shared_ptr<const value_semantic> m_value_semantic;
friend BOOST_PROGRAM_OPTIONS_DECL
void store(const basic_parsed_options<char>& options,
friend void BOOST_PROGRAM_OPTIONS_DECL
store(const basic_parsed_options<char>& options,
variables_map& m, bool);
friend BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
friend void BOOST_PROGRAM_OPTIONS_DECL notify(variables_map& m);
};
/** Implements string->string mapping with convenient value casting
@@ -132,11 +104,7 @@ namespace boost { namespace program_options {
const abstract_variables_map* m_next;
};
/** Concrete variables map which store variables in real map.
This class is derived from std::map<std::string, variable_value>,
so you can use all map operators to examine its content.
*/
/** Concrete variables map which store variables in real map. */
class BOOST_PROGRAM_OPTIONS_DECL variables_map : public abstract_variables_map,
public std::map<std::string, variable_value>
{
@@ -157,12 +125,30 @@ namespace boost { namespace program_options {
be changed by subsequence assignments. */
std::set<std::string> m_final;
friend BOOST_PROGRAM_OPTIONS_DECL
void store(const basic_parsed_options<char>& options,
friend void store(const basic_parsed_options<char>& options,
variables_map& xm,
bool utf8);
};
/** Stores in 'm' all options that are defined in 'options'.
If 'm' already has a non-defaulted value of an option, that value
is not changed, even if 'options' specify some value.
*/
BOOST_PROGRAM_OPTIONS_DECL void store(const basic_parsed_options<char>& options, variables_map& m,
bool utf8 = false);
/** Stores in 'm' all options that are defined in 'options'.
If 'm' already has a non-defaulted value of an option, that value
is not changed, even if 'options' specify some value.
This is wide character variant.
*/
BOOST_PROGRAM_OPTIONS_DECL void store(const basic_parsed_options<wchar_t>& options,
variables_map& m);
/** Runs all 'notify' function for options in 'm'. */
BOOST_PROGRAM_OPTIONS_DECL void notify(variables_map& m);
/*
* Templates/inlines
@@ -194,6 +180,18 @@ namespace boost { namespace program_options {
return v;
}
template<class T>
const T&
variable_value::as() const {
return boost::any_cast<const T&>(v);
}
template<class T>
T&
variable_value::as() {
return boost::any_cast<T&>(v);
}
}}
#endif

View File

@@ -4,11 +4,6 @@
</head>
<body>
Automatic redirection failed, please go to
<a href="../../doc/html/program_options.html">../../doc/html/program_options.html</a>
&nbsp;<hr>
<p>© Copyright Beman Dawes, 2001</p>
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
<a href="../../doc/html/program_options.html">../../doc/html/program_options.html</a>
</body>
</html>
</html>

View File

@@ -98,10 +98,10 @@ namespace boost { namespace program_options { namespace detail {
{
#if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
vector<string> args;
copy(argv+1, argv+argc+!argc, inserter(args, args.end()));
copy(argv+1, argv+argc, inserter(args, args.end()));
init(args);
#else
init(vector<string>(argv+1, argv+argc+!argc));
init(vector<string>(argv+1, argv+argc));
#endif
}
@@ -290,8 +290,8 @@ namespace boost { namespace program_options { namespace detail {
// First check that the option is valid, and get its description.
// TODO: case-sensitivity.
const option_description* xd = m_desc->find_nothrow(opt.string_key,
(m_style & allow_guessing) ? true : false);
const option_description* xd =
m_desc->find_nothrow(opt.string_key, (m_style & allow_guessing));
if (!xd)
{
@@ -329,14 +329,6 @@ namespace boost { namespace program_options { namespace detail {
max_tokens -= opt.value.size();
// A value is optional if min_tokens == 0, but max_tokens > 0.
// If a value is optional, it must appear in opt.value (because
// it was 'adjacent'. Otherwise, remove the expectation of a
// non-adjacent value. (For now, we just check max_tokens == 1,
// as there is no current support for max_tokens>1)
if (min_tokens == 0 && max_tokens == 1 && opt.value.empty())
--max_tokens;
// Everything's OK, move the values to the result.
for(;!other_tokens.empty() && max_tokens--; ) {
opt.value.push_back(other_tokens[0]);
@@ -470,8 +462,7 @@ namespace boost { namespace program_options { namespace detail {
((m_style & allow_slash_for_short) && tok[0] == '/')))
{
if (m_desc->find_nothrow(tok.substr(1, tok.find('=')-1),
(m_style & allow_guessing) ? true : false))
{
m_style & allow_guessing)) {
args[0].insert(0, "-");
if (args[0][1] == '/')
args[0][1] = '-';
@@ -507,8 +498,7 @@ namespace boost { namespace program_options { namespace detail {
if (!r.first.empty()) {
option next;
next.string_key = r.first;
if (!r.second.empty())
next.value.push_back(r.second);
next.value.push_back(r.second);
result.push_back(next);
args.erase(args.begin());
}

View File

@@ -21,10 +21,8 @@ namespace boost { namespace program_options { namespace detail {
using namespace std;
common_config_file_iterator::common_config_file_iterator(
const std::set<std::string>& allowed_options,
bool allow_unregistered)
: allowed_options(allowed_options),
m_allow_unregistered(allow_unregistered)
const std::set<std::string>& allowed_options)
: allowed_options(allowed_options)
{
for(std::set<std::string>::const_iterator i = allowed_options.begin();
i != allowed_options.end();
@@ -102,8 +100,7 @@ namespace boost { namespace program_options { namespace detail {
string name = m_prefix + trim_ws(s.substr(0, n));
string value = trim_ws(s.substr(n+1));
bool registered = allowed_option(name);
if (!registered && !m_allow_unregistered)
if (!allowed_option(name))
boost::throw_exception(unknown_option(name));
if (value.empty())
@@ -113,7 +110,6 @@ namespace boost { namespace program_options { namespace detail {
this->value().string_key = name;
this->value().value.clear();
this->value().value.push_back(value);
this->value().unregistered = !registered;
break;
} else {

View File

@@ -23,7 +23,6 @@
#include <cstring>
#include <cstdarg>
#include <sstream>
#include <iterator>
using namespace std;
namespace boost { namespace program_options {
@@ -54,10 +53,10 @@ namespace boost { namespace program_options {
{
}
option_description::match_result
bool
option_description::match(const std::string& option, bool approx) const
{
match_result result = no_match;
bool result = false;
if (!m_long_name.empty()) {
if (*m_long_name.rbegin() == '*')
@@ -66,26 +65,23 @@ namespace boost { namespace program_options {
// prefix is OK.
if (option.find(m_long_name.substr(0, m_long_name.length()-1))
== 0)
result = approximate_match;
result = true;
}
if (approx)
{
if (m_long_name.find(option) == 0)
if (m_long_name == option)
result = full_match;
else
result = approximate_match;
result = true;
}
else
{
if (m_long_name == option)
result = full_match;
result = true;
}
}
if (m_short_name == option)
result = full_match;
result = true;
return result;
}
@@ -201,8 +197,6 @@ namespace boost { namespace program_options {
return *this;
}
const unsigned options_description::m_default_line_length = 80;
options_description::options_description(unsigned line_length)
: m_line_length(line_length)
{}
@@ -254,47 +248,38 @@ namespace boost { namespace program_options {
return m_options;
}
const option_description*
const option_description*
options_description::find_nothrow(const std::string& name,
bool approx) const
{
shared_ptr<option_description> found;
vector<string> approximate_matches;
int found = -1;
// We use linear search because matching specified option
// name with the declared option name need to take care about
// case sensitivity and trailing '*' and so we can't use simple map.
for(unsigned i = 0; i < m_options.size(); ++i)
{
option_description::match_result r =
m_options[i]->match(name, approx);
if (r == option_description::no_match)
continue;
// If we have a full patch, and an approximate match,
// ignore approximate match instead of reporting error.
// Say, if we have options "all" and "all-chroots", then
// "--all" on the command line should select the first one,
// without ambiguity.
//
// For now, we don't check the situation when there are
// two full matches.
if (r == option_description::full_match)
if (m_options[i]->match(name, approx))
{
return m_options[i].get();
if (found != -1)
{
vector<string> alts;
// FIXME: the use of 'key' here might not
// be the best approach.
alts.push_back(m_options[found]->key(name));
alts.push_back(m_options[i]->key(name));
boost::throw_exception(ambiguous_option(name, alts));
}
else
{
found = i;
}
}
found = m_options[i];
// FIXME: the use of 'key' here might not
// be the best approach.
approximate_matches.push_back(m_options[i]->key(name));
}
if (approximate_matches.size() > 1)
boost::throw_exception(
ambiguous_option(name, approximate_matches));
return found.get();
if (found != -1) {
return m_options[found].get();
} else {
return 0;
}
}
BOOST_PROGRAM_OPTIONS_DECL
@@ -306,26 +291,11 @@ namespace boost { namespace program_options {
namespace {
/* Given a string 'par', that contains no newline characters
outputs it to 'os' with wordwrapping, that is, as several
line.
Each output line starts with 'indent' space characters,
following by characters from 'par'. The total length of
line is no longer than 'line_length'.
*/
void format_paragraph(std::ostream& os,
std::string par,
unsigned indent,
unsigned first_column_width,
unsigned line_length)
{
// Through reminder of this function, 'line_length' will
// be the length available for characters, not including
// indent.
assert(indent < line_length);
line_length -= indent;
// index of tab (if present) is used as additional indent relative
// to first_column_width if paragrapth is spanned over multiple
// lines if tab is not on first line it is ignored
@@ -349,16 +319,16 @@ namespace boost { namespace program_options {
// this assert may fail due to user error or
// environment conditions!
assert(par_indent < line_length);
assert(par_indent < (line_length - first_column_width));
// ignore tab if not on first line
if (par_indent >= line_length)
if (par_indent >= (line_length - first_column_width))
{
par_indent = 0;
}
}
if (par.size() < line_length)
if (par.size() < (line_length - first_column_width))
{
os << par;
}
@@ -367,16 +337,17 @@ namespace boost { namespace program_options {
string::const_iterator line_begin = par.begin();
const string::const_iterator par_end = par.end();
bool first_line = true; // of current paragraph!
bool first_line = true; // of current paragraph!
unsigned indent = first_column_width;
while (line_begin < par_end) // paragraph lines
{
if (!first_line)
{
// If line starts with space, but second character
// is not space, remove the leading space.
// We don't remove double spaces because those
// might be intentianal.
// trimm leading single spaces
// if (firstchar == ' ') &&
// ((exists(firstchar + 1) && (firstchar + 1 != ' '))
if ((*line_begin == ' ') &&
((line_begin + 1 < par_end) &&
(*(line_begin + 1) != ' ')))
@@ -385,30 +356,35 @@ namespace boost { namespace program_options {
}
}
// Take care to never increment the iterator past
// the end, since MSVC 8.0 (brokenly), assumes that
// doing that, even if no access happens, is a bug.
unsigned remaining = distance(line_begin, par_end);
string::const_iterator line_end = line_begin +
((remaining < line_length) ? remaining : line_length);
string::const_iterator line_end;
if (line_begin + (line_length - indent) > par_end)
{
line_end = par_end;
}
else
{
line_end = line_begin + (line_length - indent);
}
// prevent chopped words
// Is line_end between two non-space characters?
// if (lastchar != ' ') &&
// ((exists(lastchar + 1) && (lastchar + 1 != ' '))
if ((*(line_end - 1) != ' ') &&
((line_end < par_end) && (*line_end != ' ')))
{
// find last ' ' in the second half of the current paragraph line
string::const_iterator last_space =
find(reverse_iterator<string::const_iterator>(line_end),
reverse_iterator<string::const_iterator>(line_begin),
find(reverse_iterator<string::const_iterator>(line_end - 1),
reverse_iterator<string::const_iterator>(line_begin - 1),
' ')
.base();
if (last_space != line_begin)
if (last_space != line_begin - 1)
{
// is last_space within the second half ot the
// current line
if ((unsigned)distance(last_space, line_end) <
if (unsigned(distance(last_space, line_end)) <
(line_length - indent) / 2)
{
line_end = last_space;
@@ -421,7 +397,7 @@ namespace boost { namespace program_options {
if (first_line)
{
indent += par_indent;
indent = first_column_width + par_indent;
first_line = false;
}
@@ -460,7 +436,7 @@ namespace boost { namespace program_options {
assert(line_length > first_column_width);
// Note: can't use 'tokenizer' as name of typedef -- borland
// will consider uses of 'tokenizer' below as uses of
// will consider subsequence uses of 'tokenizer' as uses of
// boost::tokenizer, not typedef.
typedef boost::tokenizer<boost::char_separator<char> > tok;

View File

@@ -88,8 +88,7 @@ namespace boost { namespace program_options {
template<class charT>
basic_parsed_options<charT>
parse_config_file(std::basic_istream<charT>& is,
const options_description& desc,
bool allow_unregistered)
const options_description& desc)
{
set<string> allowed_options;
@@ -107,8 +106,7 @@ namespace boost { namespace program_options {
// Parser return char strings
parsed_options result(&desc);
copy(detail::basic_config_file_iterator<charT>(
is, allowed_options, allow_unregistered),
copy(detail::basic_config_file_iterator<charT>(is, allowed_options),
detail::basic_config_file_iterator<charT>(),
back_inserter(result.options));
// Convert char strings into desired type.
@@ -118,15 +116,13 @@ namespace boost { namespace program_options {
template
BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<char>
parse_config_file(std::basic_istream<char>& is,
const options_description& desc,
bool allow_unregistered);
const options_description& desc);
#ifndef BOOST_NO_STD_WSTRING
template
BOOST_PROGRAM_OPTIONS_DECL basic_parsed_options<wchar_t>
parse_config_file(std::basic_istream<wchar_t>& is,
const options_description& desc,
bool allow_unregistered);
const options_description& desc);
#endif
// This versio, which accepts any options without validation, is disabled,

View File

@@ -17,7 +17,7 @@ namespace boost { namespace program_options {
positional_options_description::positional_options_description()
{}
positional_options_description&
void
positional_options_description::add(const char* name, int max_count)
{
assert(max_count != -1 || m_trailing.empty());
@@ -27,7 +27,7 @@ namespace boost { namespace program_options {
else {
m_names.resize(m_names.size() + max_count, name);
}
return *this;
}
unsigned

View File

@@ -122,8 +122,8 @@ namespace boost { namespace program_options {
/* Validates bool value.
Any of "1", "true", "yes", "on" will be converted to "1".<br>
Any of "0", "false", "no", "off" will be converted to "0".<br>
Case is ignored. The 'xs' vector can either be empty, in which
case the value is 'true', or can contain explicit value.
Case is ignored. Regardless of name passed, parameter will always
be optional.
*/
BOOST_PROGRAM_OPTIONS_DECL void validate(any& v, const vector<string>& xs,
bool*, int)
@@ -170,12 +170,9 @@ namespace boost { namespace program_options {
{
check_first_occurrence(v);
string s(get_single_string(xs));
if (!s.empty() && (
(*s.begin() == '\'' && *s.rbegin() == '\'' ||
*s.begin() == '"' && *s.rbegin() == '"')))
{
if (*s.begin() == '\'' && *s.rbegin() == '\'' ||
*s.begin() == '"' && *s.rbegin() == '"')
v = any(s.substr(1, s.size()-2));
}
else
v = any(s);
}

View File

@@ -35,25 +35,14 @@ namespace boost { namespace program_options {
std::set<std::string> new_final;
// Declared once, to please Intel in VC++ mode;
unsigned i;
// First, convert/store all given options
for (i = 0; i < options.options.size(); ++i) {
for (size_t i = 0; i < options.options.size(); ++i) {
const string& name = options.options[i].string_key;
// Skip positional options without name
if (name.empty())
continue;
// Ignore unregistered option. The 'unregistered'
// field can be true only if user has explicitly asked
// to allow unregistered options. We can't store them
// to variables map (lacking any information about paring),
// so just ignore them.
if (options.options[i].unregistered)
continue;
// If option has final value, skip this assignment
if (xm.m_final.count(name))
continue;
@@ -95,7 +84,7 @@ namespace boost { namespace program_options {
// Second, apply default values.
const vector<shared_ptr<option_description> >& all = desc.options();
for(i = 0; i < all.size(); ++i)
for(unsigned i = 0; i < all.size(); ++i)
{
const option_description& d = *all[i];
string key = d.key("");

View File

@@ -12,12 +12,10 @@ namespace boost { namespace program_options {
using namespace std;
// Take a command line string and splits in into tokens, according
// to the rules windows command line processor uses.
//
// The rules are pretty funny, see
// The rules for windows command line are pretty funny, see
// http://article.gmane.org/gmane.comp.lib.boost.user/3005
// http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp
BOOST_PROGRAM_OPTIONS_DECL
std::vector<std::string> split_winmain(const std::string& input)
{
@@ -25,7 +23,7 @@ namespace boost { namespace program_options {
string::const_iterator i = input.begin(), e = input.end();
for(;i != e; ++i)
if (!isspace((unsigned char)*i))
if (!isspace(*i))
break;
if (i != e) {
@@ -57,11 +55,11 @@ namespace boost { namespace program_options {
current.append(backslash_count, '\\');
backslash_count = 0;
}
if (isspace((unsigned char)*i) && !inside_quoted) {
if (isspace(*i) && !inside_quoted) {
// Space outside quoted section terminate the current argument
result.push_back(current);
current.resize(0);
for(;i != e && isspace((unsigned char)*i); ++i)
for(;i != e && isspace(*i); ++i)
;
--i;
} else {
@@ -88,7 +86,7 @@ namespace boost { namespace program_options {
{
vector<wstring> result;
vector<string> aux = split_winmain(to_internal(cmdline));
for (unsigned i = 0, e = aux.size(); i < e; ++i)
for (unsigned i = 0, e = result.size(); i < e; ++i)
result.push_back(from_utf8(aux[i]));
return result;
}

47
test/Jamfile Normal file
View File

@@ -0,0 +1,47 @@
subproject libs/program_options/test ;
import testing ;
rule program-options-test ( name )
{
return [
run $(name).cpp <lib>../build/boost_program_options
<lib>../../test/build/boost_test_exec_monitor : :
: <include>$(BOOST_ROOT)
std::locale-support ]
;
}
rule program-options-dll-test ( name )
{
return [
run $(name).cpp <dll>../build/boost_program_options
<lib>../../test/build/boost_test_exec_monitor : :
: <include>$(BOOST_ROOT)
<define>BOOST_ALL_DYN_LINK=1
<runtime-link>dynamic
: $(name)_dll ]
;
}
test-suite program_options :
[ program-options-test options_description_test ]
[ program-options-test parsers_test ]
[ program-options-test variable_map_test ]
[ program-options-test cmdline_test ]
[ program-options-test positional_options_test ]
[ program-options-test unicode_test ]
[ program-options-test winmain ]
[ program-options-dll-test options_description_test ]
[ program-options-dll-test parsers_test ]
[ program-options-dll-test variable_map_test ]
[ program-options-dll-test cmdline_test ]
[ program-options-dll-test positional_options_test ]
[ program-options-dll-test unicode_test ]
[ program-options-dll-test winmain ]
;

View File

@@ -2,8 +2,8 @@
project
: requirements
<library>../build//boost_program_options
<library>/boost/test//boost_test_exec_monitor
<link>static
<variant>debug
# <define>_GLIBCXX_CONCEPT_CHECKS
# <define>_GLIBCXX_DEBUG
@@ -13,8 +13,7 @@ rule po-test ( source )
{
return
[ run $(source) ]
[ run $(source) : : : <link>shared <define>BOOST_PROGRAM_OPTIONS_DYN_LINK=1
: $(source:B)_dll ]
[ run $(source) : : : <link>shared : $(source:B)_dll ]
;
}

View File

@@ -9,14 +9,15 @@
using namespace boost::program_options;
using boost::program_options::detail::cmdline;
#include <boost/test/test_tools.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include <cassert>
using namespace std;
#include "minitest.hpp"
/* To facilitate testing, declare a number of error codes. Otherwise,
we'd have to specify the type of exception that should be thrown.
*/
@@ -599,7 +600,7 @@ void test_unregistered()
// It's not clear yet, so I'm leaving the decision till later.
}
int main(int ac, char* av[])
int test_main(int ac, char* av[])
{
test_long_options();
test_short_options();

View File

@@ -1,25 +0,0 @@
#ifndef BOOST_PROGRAM_OPTIONS_MINITEST
#define BOOST_PROGRAM_OPTIONS_MINITEST
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#define BOOST_REQUIRE(b) assert(b)
#define BOOST_CHECK(b) assert(b)
#define BOOST_CHECK_EQUAL(a, b) assert(a == b)
#define BOOST_ERROR(description) std::cerr << description; std::cerr << "\n"; abort();
#define BOOST_CHECK_THROW(expression, exception) \
try \
{ \
expression; \
BOOST_ERROR("expected exception not thrown");\
throw 10; \
} \
catch(exception &) \
{ \
}
#endif

View File

@@ -10,50 +10,21 @@ using namespace boost::program_options;
#include <boost/function.hpp>
using namespace boost;
#define BOOST_INCLUDE_MAIN // for testing, include rather than link
#include <boost/test/test_tools.hpp>
#include <utility>
#include <string>
#include <sstream>
using namespace std;
#include "minitest.hpp"
void test_type()
{
options_description desc;
desc.add_options()
("foo", value<int>(), "")
("bar", value<std::string>(), "")
;
const typed_value_base* b = dynamic_cast<const typed_value_base*>
(desc.find("foo", false).semantic().get());
BOOST_CHECK(b);
BOOST_CHECK(b->value_type() == typeid(int));
const typed_value_base* b2 = dynamic_cast<const typed_value_base*>
(desc.find("bar", false).semantic().get());
BOOST_CHECK(b2);
BOOST_CHECK(b2->value_type() == typeid(std::string));
}
void test_approximation()
{
options_description desc;
desc.add_options()
("foo", new untyped_value())
("fee", new untyped_value())
("baz", new untyped_value())
("all-chroots", new untyped_value())
("all-sessions", new untyped_value())
("all", new untyped_value())
;
("foo", new untyped_value())
("fee", new untyped_value())
("baz", new untyped_value());
BOOST_CHECK_EQUAL(desc.find("fo", true).long_name(), "foo");
BOOST_CHECK_EQUAL(desc.find("all", true).long_name(), "all");
BOOST_CHECK_EQUAL(desc.find("all-ch", true).long_name(), "all-chroots");
// BOOST_CHECK(desc.count_approx("foo") == 1);
// set<string> a = desc.approximations("f");
// BOOST_CHECK(a.size() == 2);
@@ -61,25 +32,8 @@ void test_approximation()
// BOOST_CHECK(*(++a.begin()) == "foo");
}
void test_formatting()
int test_main(int, char* [])
{
// Long option descriptions used to crash on MSVC-8.0.
options_description desc;
desc.add_options()(
"test", new untyped_value(),
"foo foo foo foo foo foo foo foo foo foo foo foo foo foo"
"foo foo foo foo foo foo foo foo foo foo foo foo foo foo"
"foo foo foo foo foo foo foo foo foo foo foo foo foo foo"
"foo foo foo foo foo foo foo foo foo foo foo foo foo foo");
stringstream ss;
ss << desc;
}
int main(int, char* [])
{
test_type();
test_approximation();
test_formatting();
return 0;
}

View File

@@ -6,7 +6,6 @@
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
using namespace boost::program_options;
// We'll use po::value everywhere to workaround vc6 bug.
namespace po = boost::program_options;
@@ -14,17 +13,13 @@ namespace po = boost::program_options;
#include <boost/function.hpp>
using namespace boost;
#define BOOST_INCLUDE_MAIN // for testing, include rather than link
#include <boost/test/test_tools.hpp>
#include <sstream>
#include <iostream>
using namespace std;
#if defined(__sun)
#include <stdlib.h> // for putenv on solaris
#else
#include <cstdlib> // for putenv
#endif
#include "minitest.hpp"
#define TEST_CHECK_THROW(expression, exception, description) \
try \
@@ -137,16 +132,6 @@ void test_command_line()
check_value(a4[0], "foo", "4");
check_value(a4[1], "bar", "11");
// Check that we don't crash on empty values of type 'string'
char* cmdline4[] = {"", "--open", ""};
options_description desc2;
desc2.add_options()
("open", po::value<string>())
;
variables_map vm;
po::store(po::parse_command_line(3, cmdline4, desc2), vm);
}
@@ -234,31 +219,12 @@ void test_unregistered()
BOOST_CHECK(a2[1] == "--bar");
BOOST_CHECK(a2[2] == "1");
// Test that storing unregisted options has no effect
variables_map vm;
store(command_line_parser(cmdline1).options(desc).
allow_unregistered().run(),
vm);
BOOST_CHECK_EQUAL(vm.size(), 0u);
const char content1[] =
"gv1 = 0\n"
"[m1]\n"
"v1 = 1\n"
;
stringstream ss(content1);
vector<option> a3 = parse_config_file(ss, desc, true).options;
BOOST_REQUIRE(a3.size() == 2);
cout << "XXX" << a3[0].value.front() << "\n";
check_value(a3[0], "gv1", "0");
check_value(a3[1], "m1.v1", "1");
}
int main(int, char* [])
int test_main(int, char* [])
{
test_command_line();
test_config_file();

View File

@@ -12,8 +12,7 @@ namespace po = boost::program_options;
#include <boost/limits.hpp>
#include "minitest.hpp"
#include <boost/test/test_tools.hpp>
#include <vector>
using namespace std;
@@ -50,31 +49,28 @@ void test_parsing()
("first", po::value<int>())
("second", po::value<int>())
("input-file", po::value< vector<string> >())
("some-other", po::value<string>())
;
positional_options_description p;
p.add("input-file", 2).add("some-other", 1);
p.add("input-file", 2);
vector<string> args;
args.push_back("--first=10");
args.push_back("file1");
args.push_back("--second=10");
args.push_back("file2");
args.push_back("file3");
// Check that positional options are handled.
parsed_options parsed =
command_line_parser(args).options(desc).positional(p).run();
BOOST_REQUIRE(parsed.options.size() == 5);
BOOST_REQUIRE(parsed.options.size() == 4);
BOOST_CHECK_EQUAL(parsed.options[1].string_key, "input-file");
BOOST_CHECK_EQUAL(parsed.options[1].value[0], "file1");
BOOST_CHECK_EQUAL(parsed.options[3].string_key, "input-file");
BOOST_CHECK_EQUAL(parsed.options[3].value[0], "file2");
BOOST_CHECK_EQUAL(parsed.options[4].value[0], "file3");
args.push_back("file4");
args.push_back("file3");
// Check that excessive number of positional options is detected.
BOOST_CHECK_THROW(command_line_parser(args).options(desc).positional(p)
@@ -82,7 +78,7 @@ void test_parsing()
too_many_positional_options_error);
}
int main(int, char* [])
int test_main(int, char* [])
{
test_positional_options();
test_parsing();

View File

@@ -3,11 +3,11 @@
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <cassert>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cassert>
#include <boost/progress.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>
@@ -15,8 +15,6 @@
#include <boost/program_options/detail/convert.hpp>
#include <boost/program_options/detail/utf8_codecvt_facet.hpp>
#include "minitest.hpp"
using namespace std;
string file_content(const string& filename)
@@ -77,7 +75,7 @@ std::wstring from_8_bit_2(const std::string& s,
void test_convert(const std::string& input,
const std::string& expected_output)
{
boost::program_options::detail::utf8_codecvt_facet facet;
boost::program_options::detail::utf8_codecvt_facet<wchar_t, char> facet;
std::wstring output;
{
@@ -96,7 +94,7 @@ void test_convert(const std::string& input,
facet);
}
BOOST_CHECK(output.size()*2 == expected_output.size());
assert(output.size()*2 == expected_output.size());
for(unsigned i = 0; i < output.size(); ++i) {
@@ -105,23 +103,23 @@ void test_convert(const std::string& input,
low &= 0xFF;
unsigned low2 = expected_output[2*i];
low2 &= 0xFF;
BOOST_CHECK(low == low2);
assert(low == low2);
}
{
unsigned high = output[i];
high >>= 8;
high &= 0xFF;
unsigned high2 = expected_output[2*i+1];
BOOST_CHECK(high == high2);
assert(high == high2);
}
}
string ref = boost::to_8_bit(output, facet);
BOOST_CHECK(ref == input);
assert(ref == input);
}
int main(int ac, char* av[])
int test_main(int ac, char* av[])
{
std::string input = file_content("utf8.txt");
std::string expected = file_content("ucs2.txt");

View File

@@ -15,11 +15,12 @@ namespace po = boost::program_options;
#include <boost/function.hpp>
using namespace boost;
#define BOOST_INCLUDE_MAIN // for testing, include rather than link
#include <boost/test/test_tools.hpp>
#include <sstream>
using namespace std;
#include "minitest.hpp"
// Test that unicode input is forwarded to unicode option without
// problems.
void test_unicode_to_unicode()
@@ -148,7 +149,7 @@ void test_config_file()
BOOST_CHECK(vm["foo"].as<string>() == "\xD1\x8F");
}
int main(int, char* [])
int test_main(int, char* [])
{
test_unicode_to_unicode();
test_unicode_to_native();

View File

@@ -15,10 +15,22 @@ namespace po = boost::program_options;
#include <boost/function.hpp>
using namespace boost;
#define BOOST_INCLUDE_MAIN // for testing, include rather than link
#include <boost/test/test_tools.hpp>
#include <sstream>
using namespace std;
#include "minitest.hpp"
#define TEST_CHECK_THROW(expression, exception, description) \
try \
{ \
expression; \
BOOST_ERROR(description);\
throw 10; \
} \
catch(exception &) \
{ \
}
vector<string> sv(char* array[], unsigned size)
{
@@ -90,25 +102,6 @@ void test_variable_map()
BOOST_CHECK(vm3["vee"].as<string>() == "42");
BOOST_CHECK(vm3["voo"].as<string>() == "1");
BOOST_CHECK(vm3["iii"].as<int>() == 123);
options_description desc3;
desc3.add_options()
("imp", po::value<int>()->implicit_value(100))
("iim", po::value<int>()->implicit_value(200)->default_value(201))
("mmp,m", po::value<int>()->implicit_value(123)->default_value(124))
;
char* cmdline6_[] = { "--imp=1", "-m" };
vector<string> cmdline6 = sv(cmdline6_,
sizeof(cmdline6_)/sizeof(cmdline6_[0]));
parsed_options a6 = command_line_parser(cmdline6).options(desc3).run();
variables_map vm4;
store(a6, vm4);
notify(vm4);
BOOST_REQUIRE(vm4.size() == 3);
BOOST_CHECK(vm4["imp"].as<int>() == 1);
BOOST_CHECK(vm4["iim"].as<int>() == 201);
BOOST_CHECK(vm4["mmp"].as<int>() == 123);
}
int stored_value;
@@ -276,7 +269,7 @@ void test_multiple_assignments_with_different_option_description()
}
int main(int, char* [])
int test_main(int, char* [])
{
test_variable_map();
test_semantic_values();

View File

@@ -7,42 +7,26 @@
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <boost/program_options/parsers.hpp>
using namespace boost::program_options;
void check_equal(const std::vector<string>& actual, char **expected, int n)
{
if (actual.size() != n)
{
std::cerr << "Size mismatch between expected and actual data\n";
abort();
}
for (int i = 0; i < n; ++i)
{
if (actual[i] != expected[i])
{
std::cerr << "Unexpected content\n";
abort();
}
}
}
#include <boost/test/test_tools.hpp>
#include <boost/preprocessor/cat.hpp>
void test_winmain()
{
using namespace std;
#define C ,
#define TEST(input, expected) \
char* BOOST_PP_CAT(e, __LINE__)[] = expected;\
vector<string> BOOST_PP_CAT(v, __LINE__) = split_winmain(input);\
check_equal(BOOST_PP_CAT(v, __LINE__), BOOST_PP_CAT(e, __LINE__),\
sizeof(BOOST_PP_CAT(e, __LINE__))/sizeof(char*));
BOOST_CHECK_EQUAL_COLLECTIONS(BOOST_PP_CAT(v, __LINE__).begin(),\
BOOST_PP_CAT(v, __LINE__).end(),\
BOOST_PP_CAT(e, __LINE__),\
BOOST_PP_CAT(e, __LINE__) + \
sizeof(BOOST_PP_CAT(e, __LINE__))/sizeof(char*));
// The following expectations were obtained in Win2000 shell:
TEST("1 ", {"1"});
@@ -61,13 +45,13 @@ void test_winmain()
TEST("1\\\\1 ", {"1\\\\1"});
}
int main(int, char*[])
int test_main(int, char*[])
{
test_winmain();
return 0;
}
#else
int main(int, char*[])
int test_main(int, char*[])
{
return 0;
}