aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar David Leverton <levertond@googlemail.com> 2011-10-04 22:18:50 +0100
committerAvatar David Leverton <levertond@googlemail.com> 2011-10-04 22:19:54 +0100
commit7af62a2f93f5e8a78fc85ed2540e2543478bb11f (patch)
treeaff9c61014191559eaff17ee64b9b96f4e8639c5
parenta415d9a3a6065927d64719b803d7f9b40ec0b55d (diff)
downloadpaludis-7af62a2f93f5e8a78fc85ed2540e2543478bb11f.tar.gz
paludis-7af62a2f93f5e8a78fc85ed2540e2543478bb11f.tar.xz
Support negated short options
-rw-r--r--NEWS4
-rw-r--r--paludis/args/args_TEST.cc9
-rw-r--r--paludis/args/args_handler.cc11
-rw-r--r--paludis/args/man.cc7
4 files changed, 23 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index 8951935..1a127a3 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,10 @@ master:
version is still supported for compatibility). The short version is
still '-s'.
+ * Command-line arguments that have a short version and can be negated
+ with '--no-*' can now be negated in short form by using '+' instead
+ of '-', for example '+x' for '--no-execute'.
+
0.68.0:
* Licence groups are now supported.
diff --git a/paludis/args/args_TEST.cc b/paludis/args/args_TEST.cc
index 4e04d46..69d6b54 100644
--- a/paludis/args/args_TEST.cc
+++ b/paludis/args/args_TEST.cc
@@ -46,6 +46,7 @@ namespace
IntegerArg arg_somenum;
EnumArg arg_enum;
SwitchArg arg_spider;
+ SwitchArg arg_spy;
ArgsGroup group_three;
EnumArg arg_other_enum;
@@ -88,6 +89,7 @@ CommandLine::CommandLine() :
arg_enum(&group_two, "enum", 'e', "One of three",
EnumArg::EnumArgOptions("one", "Option one")("two", "option two")("three", "option three"), "two"),
arg_spider(&group_two, "spider", '\0', "A spider?", true),
+ arg_spy(&group_two, "spy", 'y', "A spy?", true),
group_three(main_options_section(), "Group three", "Description of group three"),
arg_other_enum(&group_three, "something-else", '\0', "Blah.", EnumArg::EnumArgOptions("a", "A")("b", "B")("c", "C"), "b"),
@@ -101,10 +103,10 @@ CommandLine::~CommandLine()
TEST(Args, Simple)
{
- const char * args[] = { "program-name", "--other-monkey", "chimp", "--other-baz", "--spider", "--no-spider",
+ const char * args[] = { "program-name", "--other-monkey", "chimp", "--other-baz", "--spider", "--no-spider", "--spy", "+y",
"-fsne", "blah", "7", "three", "--", "--dummy", "one", "two" };
CommandLine c1;
- c1.run(14, args, "", "", "");
+ c1.run(16, args, "", "", "");
EXPECT_TRUE(c1.arg_foo.specified());
EXPECT_TRUE(! c1.arg_bar.specified());
EXPECT_TRUE(c1.arg_baz.specified());
@@ -121,6 +123,7 @@ TEST(Args, Simple)
EXPECT_TRUE(c1.arg_monkey.specified());
EXPECT_TRUE(c1.arg_monkey.argument() == "chimp");
EXPECT_TRUE(! c1.arg_spider.specified());
+ EXPECT_TRUE(! c1.arg_spy.specified());
ASSERT_EQ(3, std::distance(c1.begin_parameters(), c1.end_parameters()));
EXPECT_EQ("--dummy", *c1.begin_parameters());
@@ -138,8 +141,10 @@ TEST(Args, MissingParameters)
TEST(Args, NoNo)
{
const char *args[] = { "program-name", "--no-num" };
+ const char *args2[] = { "program-name", "+f" };
CommandLine c1;
EXPECT_THROW(c1.run(2, args, "", "", ""), BadArgument);
+ EXPECT_THROW(c1.run(2, args2, "", "", ""), BadArgument);
}
TEST(Args, Removed)
diff --git a/paludis/args/args_handler.cc b/paludis/args/args_handler.cc
index afb3966..3c13602 100644
--- a/paludis/args/args_handler.cc
+++ b/paludis/args/args_handler.cc
@@ -246,8 +246,9 @@ ArgsHandler::run(
it->second->accept(visitor);
}
}
- else if (arg[0] == '-')
+ else if (arg[0] == '-' || arg[0] == '+')
{
+ bool negate(arg[0] == '+');
arg.erase(0, 1);
for (std::string::iterator c = arg.begin(); c != arg.end(); ++c)
{
@@ -261,11 +262,11 @@ ArgsHandler::run(
std::map<char, ArgsOption *>::iterator it = _imp->shortopts.find(*c);
if (it == _imp->shortopts.end())
- {
- throw BadArgument(std::string("-") + *c);
- }
+ throw BadArgument(std::string(negate ? "+" : "-") + *c);
+ if (negate && ! it->second->can_be_negated())
+ throw BadArgument(std::string("+") + *c);
- ArgsVisitor visitor(&argit, arge, env_prefix, remaining_chars, false, specifiedness);
+ ArgsVisitor visitor(&argit, arge, env_prefix, remaining_chars, negate, specifiedness);
it->second->accept(visitor);
if (maybe_second_char_used && remaining_chars.empty())
diff --git a/paludis/args/man.cc b/paludis/args/man.cc
index 7cd0fa4..d33a7f7 100644
--- a/paludis/args/man.cc
+++ b/paludis/args/man.cc
@@ -315,7 +315,12 @@ AsciidocWriter::arg_group_item(const char & short_name, const std::string & long
_os << "-" << short_name << " , ";
_os << "--" << long_name;
if (! negated_long_name.empty())
- _os << " (" << "--" << negated_long_name << ")";
+ {
+ _os << " (";
+ if (short_name)
+ _os << "+" << short_name << " , ";
+ _os << "--" << negated_long_name << ")";
+ }
_os << "*::" << endl;
_os << " ";
escape_asciidoc(_os, description);