- ``required`` - this option must be specified exactly once (or, in case of
the list options without the ``multi_val`` property, at least
- once). Incompatible with ``zero_or_one`` and ``one_or_more``.
-
- - ``one_or_more`` - the option must be specified at least one time. Useful
- only for list options in conjunction with ``multi_val``; for ordinary lists
- it is synonymous with ``required``. Incompatible with ``required`` and
- ``zero_or_one``.
-
- - ``optional`` - the option can be specified zero or one times. Useful only
- for list options in conjunction with ``multi_val``. Incompatible with
- ``required`` and ``one_or_more``.
+ once). Incompatible with ``optional`` and ``one_or_more``.
+
+ - ``optional`` - the option can be specified either zero times or exactly
+ once. The default for switch options. Useful only for list options in
+ conjunction with ``multi_val``. Incompatible with ``required``,
+ ``zero_or_more`` and ``one_or_more``.
+
+ - ``one_or_more`` - the option must be specified at least once. Can be useful
+ to allow switch options be both obligatory and be specified multiple
+ times. For list options is useful only in conjunction with ``multi_val``;
+ for ordinary it is synonymous with ``required``. Incompatible with
+ ``required``, ``optional`` and ``zero_or_more``.
+
+ - ``zero_or_more`` - the option can be specified zero or more times. Useful
+ to allow a single switch option to be specified more than
+ once. Incompatible with ``required``, ``optional`` and ``one_or_more``.
- ``hidden`` - the description of this option will not appear in
the ``--help`` output (but will appear in the ``--help-hidden``
enum OptionDescriptionFlags { Required = 0x1, Hidden = 0x2,
ReallyHidden = 0x4, Extern = 0x8,
OneOrMore = 0x10, Optional = 0x20,
- CommaSeparated = 0x40, ForwardNotSplit = 0x80 };
+ CommaSeparated = 0x40, ForwardNotSplit = 0x80,
+ ZeroOrMore = 0x100 };
}
/// OptionDescription - Represents data contained in a single
/// Merge - Merge two option descriptions.
void Merge (const OptionDescription& other);
+ /// CheckConsistency - Check that the flags are consistent.
+ void CheckConsistency() const;
+
// Misc convenient getters/setters.
bool isAlias() const;
bool isOneOrMore() const;
void setOneOrMore();
+ bool isZeroOrMore() const;
+ void setZeroOrMore();
+
bool isOptional() const;
void setOptional();
};
+void OptionDescription::CheckConsistency() const {
+ unsigned i = 0;
+
+ i += this->isRequired();
+ i += this->isOptional();
+ i += this->isOneOrMore();
+ i += this->isZeroOrMore();
+
+ if (i > 1) {
+ throw "Only one of (required), (optional), (one_or_more) or "
+ "(zero_or_more) properties is allowed!";
+ }
+}
+
void OptionDescription::Merge (const OptionDescription& other)
{
if (other.Type != Type)
Flags |= OptionDescriptionFlags::OneOrMore;
}
+bool OptionDescription::isZeroOrMore() const {
+ return Flags & OptionDescriptionFlags::ZeroOrMore;
+}
+void OptionDescription::setZeroOrMore() {
+ Flags |= OptionDescriptionFlags::ZeroOrMore;
+}
+
bool OptionDescription::isOptional() const {
return Flags & OptionDescriptionFlags::Optional;
}
AddHandler("init", &CollectOptionProperties::onInit);
AddHandler("multi_val", &CollectOptionProperties::onMultiVal);
AddHandler("one_or_more", &CollectOptionProperties::onOneOrMore);
+ AddHandler("zero_or_more", &CollectOptionProperties::onZeroOrMore);
AddHandler("really_hidden", &CollectOptionProperties::onReallyHidden);
AddHandler("required", &CollectOptionProperties::onRequired);
AddHandler("optional", &CollectOptionProperties::onOptional);
void onRequired (const DagInit& d) {
CheckNumberOfArguments(d, 0);
- if (optDesc_.isOneOrMore() || optDesc_.isOptional())
- throw "Only one of (required), (optional) or "
- "(one_or_more) properties is allowed!";
+
optDesc_.setRequired();
+ optDesc_.CheckConsistency();
}
void onInit (const DagInit& d) {
void onOneOrMore (const DagInit& d) {
CheckNumberOfArguments(d, 0);
- if (optDesc_.isRequired() || optDesc_.isOptional())
- throw "Only one of (required), (optional) or "
- "(one_or_more) properties is allowed!";
- if (!OptionType::IsList(optDesc_.Type))
- llvm::errs() << "Warning: specifying the 'one_or_more' property "
- "on a non-list option will have no effect.\n";
+
optDesc_.setOneOrMore();
+ optDesc_.CheckConsistency();
+ }
+
+ void onZeroOrMore (const DagInit& d) {
+ CheckNumberOfArguments(d, 0);
+
+ if (OptionType::IsList(optDesc_.Type))
+ llvm::errs() << "Warning: specifying the 'zero_or_more' property "
+ "on a list option has no effect.\n";
+
+ optDesc_.setZeroOrMore();
+ optDesc_.CheckConsistency();
}
void onOptional (const DagInit& d) {
CheckNumberOfArguments(d, 0);
- if (optDesc_.isRequired() || optDesc_.isOneOrMore())
- throw "Only one of (required), (optional) or "
- "(one_or_more) properties is allowed!";
+
if (!OptionType::IsList(optDesc_.Type))
llvm::errs() << "Warning: specifying the 'optional' property"
- "on a non-list option will have no effect.\n";
+ "on a non-list option has no effect.\n";
+
optDesc_.setOptional();
+ optDesc_.CheckConsistency();
}
void onMultiVal (const DagInit& d) {
else
O << ", cl::Required";
}
- else if (val.isOneOrMore() && val.isList()) {
- O << ", cl::OneOrMore";
- }
- else if (val.isOptional() && val.isList()) {
+
+ if (val.isOptional())
O << ", cl::Optional";
- }
+
+ if (val.isOneOrMore())
+ O << ", cl::OneOrMore";
+
+ if (val.isZeroOrMore())
+ O << ", cl::ZeroOrMore";
if (val.isReallyHidden())
O << ", cl::ReallyHidden";