1 // Copyright Vladimir Prus 2002-2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #define BOOST_PROGRAM_OPTIONS_SOURCE
7 #include <boost/program_options/config.hpp>
9 #include <boost/config.hpp>
11 #include <boost/program_options/detail/cmdline.hpp>
12 #include <boost/program_options/errors.hpp>
13 #include <boost/program_options/value_semantic.hpp>
14 #include <boost/program_options/options_description.hpp>
15 #include <boost/program_options/positional_options.hpp>
16 #include <boost/throw_exception.hpp>
18 #include <boost/bind.hpp>
32 namespace boost
{ namespace program_options
{
35 using namespace boost::program_options::command_line_style
;
39 invalid_syntax::get_template(kind_t kind
)
41 // Initially, store the message in 'const char*' variable,
42 // to avoid conversion to string in all cases.
46 case empty_adjacent_parameter
:
47 msg
= "the argument for option '%canonical_option%' should follow immediately after the equal sign";
49 case missing_parameter
:
50 msg
= "the required argument for option '%canonical_option%' is missing";
52 case unrecognized_line
:
53 msg
= "the options configuration file contains an invalid line '%invalid_line%'";
55 // none of the following are currently used:
56 case long_not_allowed
:
57 msg
= "the unabbreviated option '%canonical_option%' is not valid";
59 case long_adjacent_not_allowed
:
60 msg
= "the unabbreviated option '%canonical_option%' does not take any arguments";
62 case short_adjacent_not_allowed
:
63 msg
= "the abbreviated option '%canonical_option%' does not take any arguments";
66 msg
= "option '%canonical_option%' does not take any arguments";
69 msg
= "unknown command line syntax error for '%s'";
78 namespace boost
{ namespace program_options
{ namespace detail
{
80 // vc6 needs this, but borland chokes when this is added.
81 #if BOOST_WORKAROUND(_MSC_VER, < 1300)
83 using namespace program_options
;
87 cmdline::cmdline(const vector
<string
>& args
)
92 cmdline::cmdline(int argc
, const char*const * argv
)
94 #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
96 copy(argv
+1, argv
+argc
+!argc
, inserter(args
, args
.end()));
99 init(vector
<string
>(argv
+1, argv
+argc
+!argc
));
104 cmdline::init(const vector
<string
>& args
)
107 m_style
= command_line_style::default_style
;
110 m_allow_unregistered
= false;
114 cmdline::style(int style
)
117 style
= default_style
;
120 this->m_style
= style_t(style
);
124 cmdline::allow_unregistered()
126 this->m_allow_unregistered
= true;
130 cmdline::check_style(int style
) const
132 bool allow_some_long
=
133 (style
& allow_long
) || (style
& allow_long_disguise
);
135 const char* error
= 0;
136 if (allow_some_long
&&
137 !(style
& long_allow_adjacent
) && !(style
& long_allow_next
))
138 error
= "boost::program_options misconfiguration: "
139 "choose one or other of 'command_line_style::long_allow_next' "
140 "(whitespace separated arguments) or "
141 "'command_line_style::long_allow_adjacent' ('=' separated arguments) for "
144 if (!error
&& (style
& allow_short
) &&
145 !(style
& short_allow_adjacent
) && !(style
& short_allow_next
))
146 error
= "boost::program_options misconfiguration: "
147 "choose one or other of 'command_line_style::short_allow_next' "
148 "(whitespace separated arguments) or "
149 "'command_line_style::short_allow_adjacent' ('=' separated arguments) for "
152 if (!error
&& (style
& allow_short
) &&
153 !(style
& allow_dash_for_short
) && !(style
& allow_slash_for_short
))
154 error
= "boost::program_options misconfiguration: "
155 "choose one or other of 'command_line_style::allow_slash_for_short' "
156 "(slashes) or 'command_line_style::allow_dash_for_short' (dashes) for "
160 boost::throw_exception(invalid_command_line_style(error
));
162 // Need to check that if guessing and long disguise are enabled
163 // -f will mean the same as -foo
167 cmdline::is_style_active(style_t style
) const
169 return ((m_style
& style
) ? true : false);
173 cmdline::set_options_description(const options_description
& desc
)
179 cmdline::set_positional_options(
180 const positional_options_description
& positional
)
182 m_positional
= &positional
;
186 cmdline::get_canonical_option_prefix()
188 if (m_style
& allow_long
)
191 if (m_style
& allow_long_disguise
)
192 return allow_long_disguise
;
194 if ((m_style
& allow_short
) && (m_style
& allow_dash_for_short
))
195 return allow_dash_for_short
;
197 if ((m_style
& allow_short
) && (m_style
& allow_slash_for_short
))
198 return allow_slash_for_short
;
206 // The parsing is done by having a set of 'style parsers'
207 // and trying then in order. Each parser is passed a vector
208 // of unparsed tokens and can consume some of them (by
209 // removing elements on front) and return a vector of options.
211 // We try each style parser in turn, untill some input
212 // is consumed. The returned vector of option may contain the
213 // result of just syntactic parsing of token, say --foo will
214 // be parsed as option with name 'foo', and the style parser
215 // is not required to care if that option is defined, and how
216 // many tokens the value may take.
217 // So, after vector is returned, we validate them.
220 vector
<style_parser
> style_parsers
;
223 style_parsers
.push_back(m_style_parser
);
225 if (m_additional_parser
)
226 style_parsers
.push_back(
227 boost::bind(&cmdline::handle_additional_parser
, this, _1
));
229 if (m_style
& allow_long
)
230 style_parsers
.push_back(
231 boost::bind(&cmdline::parse_long_option
, this, _1
));
233 if ((m_style
& allow_long_disguise
))
234 style_parsers
.push_back(
235 boost::bind(&cmdline::parse_disguised_long_option
, this, _1
));
237 if ((m_style
& allow_short
) && (m_style
& allow_dash_for_short
))
238 style_parsers
.push_back(
239 boost::bind(&cmdline::parse_short_option
, this, _1
));
241 if ((m_style
& allow_short
) && (m_style
& allow_slash_for_short
))
242 style_parsers
.push_back(boost::bind(&cmdline::parse_dos_option
, this, _1
));
244 style_parsers
.push_back(boost::bind(&cmdline::parse_terminator
, this, _1
));
246 vector
<option
> result
;
250 for(unsigned i
= 0; i
< style_parsers
.size(); ++i
)
252 unsigned current_size
= static_cast<unsigned>(args
.size());
253 vector
<option
> next
= style_parsers
[i
](args
);
255 // Check that option names
256 // are valid, and that all values are in place.
260 for(unsigned k
= 0; k
< next
.size()-1; ++k
) {
261 finish_option(next
[k
], e
, style_parsers
);
263 // For the last option, pass the unparsed tokens
264 // so that they can be added to next.back()'s values
266 finish_option(next
.back(), args
, style_parsers
);
267 for (unsigned j
= 0; j
< next
.size(); ++j
)
268 result
.push_back(next
[j
]);
271 if (args
.size() != current_size
) {
279 opt
.value
.push_back(args
[0]);
280 opt
.original_tokens
.push_back(args
[0]);
281 result
.push_back(opt
);
282 args
.erase(args
.begin());
286 /* If an key option is followed by a positional option,
287 can can consume more tokens (e.g. it's multitoken option),
288 give those tokens to it. */
289 vector
<option
> result2
;
290 for (unsigned i
= 0; i
< result
.size(); ++i
)
292 result2
.push_back(result
[i
]);
293 option
& opt
= result2
.back();
295 if (opt
.string_key
.empty())
298 const option_description
* xd
;
301 xd
= m_desc
->find_nothrow(opt
.string_key
,
302 is_style_active(allow_guessing
),
303 is_style_active(long_case_insensitive
),
304 is_style_active(short_case_insensitive
));
306 catch(error_with_option_name
& e
)
308 // add context and rethrow
309 e
.add_context(opt
.string_key
, opt
.original_tokens
[0], get_canonical_option_prefix());
316 unsigned min_tokens
= xd
->semantic()->min_tokens();
317 unsigned max_tokens
= xd
->semantic()->max_tokens();
318 if (min_tokens
< max_tokens
&& opt
.value
.size() < max_tokens
)
320 // This option may grab some more tokens.
321 // We only allow to grab tokens that are not already
322 // recognized as key options.
324 int can_take_more
= max_tokens
- static_cast<int>(opt
.value
.size());
326 for (; can_take_more
&& j
< result
.size(); --can_take_more
, ++j
)
328 option
& opt2
= result
[j
];
329 if (!opt2
.string_key
.empty())
332 if (opt2
.position_key
== INT_MAX
)
334 // We use INT_MAX to mark positional options that
335 // were found after the '--' terminator and therefore
336 // should stay positional forever.
340 assert(opt2
.value
.size() == 1);
342 opt
.value
.push_back(opt2
.value
[0]);
344 assert(opt2
.original_tokens
.size() == 1);
346 opt
.original_tokens
.push_back(opt2
.original_tokens
[0]);
351 result
.swap(result2
);
354 // Assign position keys to positional options.
355 int position_key
= 0;
356 for(unsigned i
= 0; i
< result
.size(); ++i
) {
357 if (result
[i
].string_key
.empty())
358 result
[i
].position_key
= position_key
++;
363 unsigned position
= 0;
364 for (unsigned i
= 0; i
< result
.size(); ++i
) {
365 option
& opt
= result
[i
];
366 if (opt
.position_key
!= -1) {
367 if (position
>= m_positional
->max_total_count())
369 boost::throw_exception(too_many_positional_options_error());
371 opt
.string_key
= m_positional
->name_for_position(position
);
377 // set case sensitive flag
378 for (unsigned i
= 0; i
< result
.size(); ++i
) {
379 if (result
[i
].string_key
.size() > 2 ||
380 (result
[i
].string_key
.size() > 1 && result
[i
].string_key
[0] != '-'))
382 // it is a long option
383 result
[i
].case_insensitive
= is_style_active(long_case_insensitive
);
387 // it is a short option
388 result
[i
].case_insensitive
= is_style_active(short_case_insensitive
);
396 cmdline::finish_option(option
& opt
,
397 vector
<string
>& other_tokens
,
398 const vector
<style_parser
>& style_parsers
)
400 if (opt
.string_key
.empty())
405 // will have no original token if option created by handle_additional_parser()
406 std::string original_token_for_exceptions
= opt
.string_key
;
407 if (opt
.original_tokens
.size())
408 original_token_for_exceptions
= opt
.original_tokens
[0];
412 // First check that the option is valid, and get its description.
413 const option_description
* xd
= m_desc
->find_nothrow(opt
.string_key
,
414 is_style_active(allow_guessing
),
415 is_style_active(long_case_insensitive
),
416 is_style_active(short_case_insensitive
));
420 if (m_allow_unregistered
) {
421 opt
.unregistered
= true;
424 boost::throw_exception(unknown_option());
427 const option_description
& d
= *xd
;
430 opt
.string_key
= d
.key(opt
.string_key
);
432 // We check that the min/max number of tokens for the option
433 // agrees with the number of tokens we have. The 'adjacent_value'
434 // (the value in --foo=1) counts as a separate token, and if present
435 // must be consumed. The following tokens on the command line may be
437 unsigned min_tokens
= d
.semantic()->min_tokens();
438 unsigned max_tokens
= d
.semantic()->max_tokens();
440 unsigned present_tokens
= static_cast<unsigned>(opt
.value
.size() + other_tokens
.size());
442 if (present_tokens
>= min_tokens
)
444 if (!opt
.value
.empty() && max_tokens
== 0)
446 boost::throw_exception(
447 invalid_command_line_syntax(invalid_command_line_syntax::extra_parameter
));
450 // Grab min_tokens values from other_tokens, but only if those tokens
451 // are not recognized as options themselves.
452 if (opt
.value
.size() <= min_tokens
)
454 min_tokens
-= static_cast<unsigned>(opt
.value
.size());
461 // Everything's OK, move the values to the result.
462 for(;!other_tokens
.empty() && min_tokens
--; )
464 // check if extra parameter looks like a known option
465 // we use style parsers to check if it is syntactically an option,
466 // additionally we check if an option_description exists
467 vector
<option
> followed_option
;
468 vector
<string
> next_token(1, other_tokens
[0]);
469 for (unsigned i
= 0; followed_option
.empty() && i
< style_parsers
.size(); ++i
)
471 followed_option
= style_parsers
[i
](next_token
);
473 if (!followed_option
.empty())
475 original_token_for_exceptions
= other_tokens
[0];
476 const option_description
* od
= m_desc
->find_nothrow(other_tokens
[0],
477 is_style_active(allow_guessing
),
478 is_style_active(long_case_insensitive
),
479 is_style_active(short_case_insensitive
));
481 boost::throw_exception(
482 invalid_command_line_syntax(invalid_command_line_syntax::missing_parameter
));
484 opt
.value
.push_back(other_tokens
[0]);
485 opt
.original_tokens
.push_back(other_tokens
[0]);
486 other_tokens
.erase(other_tokens
.begin());
491 boost::throw_exception(
492 invalid_command_line_syntax(invalid_command_line_syntax::missing_parameter
));
496 // use only original token for unknown_option / ambiguous_option since by definition
497 // they are unrecognised / unparsable
498 catch(error_with_option_name
& e
)
500 // add context and rethrow
501 e
.add_context(opt
.string_key
, original_token_for_exceptions
, get_canonical_option_prefix());
508 cmdline::parse_long_option(vector
<string
>& args
)
510 vector
<option
> result
;
511 const string
& tok
= args
[0];
512 if (tok
.size() >= 3 && tok
[0] == '-' && tok
[1] == '-')
514 string name
, adjacent
;
516 string::size_type p
= tok
.find('=');
519 name
= tok
.substr(2, p
-2);
520 adjacent
= tok
.substr(p
+1);
521 if (adjacent
.empty())
522 boost::throw_exception( invalid_command_line_syntax(
523 invalid_command_line_syntax::empty_adjacent_parameter
,
526 get_canonical_option_prefix()) );
530 name
= tok
.substr(2);
533 opt
.string_key
= name
;
534 if (!adjacent
.empty())
535 opt
.value
.push_back(adjacent
);
536 opt
.original_tokens
.push_back(tok
);
537 result
.push_back(opt
);
538 args
.erase(args
.begin());
545 cmdline::parse_short_option(vector
<string
>& args
)
547 const string
& tok
= args
[0];
548 if (tok
.size() >= 2 && tok
[0] == '-' && tok
[1] != '-')
550 vector
<option
> result
;
552 string name
= tok
.substr(0,2);
553 string adjacent
= tok
.substr(2);
555 // Short options can be 'grouped', so that
556 // "-d -a" becomes "-da". Loop, processing one
557 // option at a time. We exit the loop when either
558 // we've processed all the token, or when the remainder
559 // of token is considered to be value, not further grouped
562 const option_description
* d
;
566 d
= m_desc
->find_nothrow(name
, false, false,
567 is_style_active(short_case_insensitive
));
569 catch(error_with_option_name
& e
)
571 // add context and rethrow
572 e
.add_context(name
, name
, get_canonical_option_prefix());
577 // FIXME: check for 'allow_sticky'.
578 if (d
&& (m_style
& allow_sticky
) &&
579 d
->semantic()->max_tokens() == 0 && !adjacent
.empty()) {
580 // 'adjacent' is in fact further option.
582 opt
.string_key
= name
;
583 result
.push_back(opt
);
585 if (adjacent
.empty())
587 args
.erase(args
.begin());
591 name
= string("-") + adjacent
[0];
592 adjacent
.erase(adjacent
.begin());
596 opt
.string_key
= name
;
597 opt
.original_tokens
.push_back(tok
);
598 if (!adjacent
.empty())
599 opt
.value
.push_back(adjacent
);
600 result
.push_back(opt
);
601 args
.erase(args
.begin());
607 return vector
<option
>();
611 cmdline::parse_dos_option(vector
<string
>& args
)
613 vector
<option
> result
;
614 const string
& tok
= args
[0];
615 if (tok
.size() >= 2 && tok
[0] == '/')
617 string name
= "-" + tok
.substr(1,1);
618 string adjacent
= tok
.substr(2);
621 opt
.string_key
= name
;
622 if (!adjacent
.empty())
623 opt
.value
.push_back(adjacent
);
624 opt
.original_tokens
.push_back(tok
);
625 result
.push_back(opt
);
626 args
.erase(args
.begin());
632 cmdline::parse_disguised_long_option(vector
<string
>& args
)
634 const string
& tok
= args
[0];
635 if (tok
.size() >= 2 &&
636 ((tok
[0] == '-' && tok
[1] != '-') ||
637 ((m_style
& allow_slash_for_short
) && tok
[0] == '/')))
641 if (m_desc
->find_nothrow(tok
.substr(1, tok
.find('=')-1),
642 is_style_active(allow_guessing
),
643 is_style_active(long_case_insensitive
),
644 is_style_active(short_case_insensitive
)))
646 args
[0].insert(0, "-");
647 if (args
[0][1] == '/')
649 return parse_long_option(args
);
652 catch(error_with_option_name
& e
)
654 // add context and rethrow
655 e
.add_context(tok
, tok
, get_canonical_option_prefix());
659 return vector
<option
>();
663 cmdline::parse_terminator(vector
<string
>& args
)
665 vector
<option
> result
;
666 const string
& tok
= args
[0];
669 for(unsigned i
= 1; i
< args
.size(); ++i
)
672 opt
.value
.push_back(args
[i
]);
673 opt
.original_tokens
.push_back(args
[i
]);
674 opt
.position_key
= INT_MAX
;
675 result
.push_back(opt
);
683 cmdline::handle_additional_parser(vector
<string
>& args
)
685 vector
<option
> result
;
686 pair
<string
, string
> r
= m_additional_parser(args
[0]);
687 if (!r
.first
.empty()) {
689 next
.string_key
= r
.first
;
690 if (!r
.second
.empty())
691 next
.value
.push_back(r
.second
);
692 result
.push_back(next
);
693 args
.erase(args
.begin());
699 cmdline::set_additional_parser(additional_parser p
)
701 m_additional_parser
= p
;
705 cmdline::extra_style_parser(style_parser s
)