Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ Date: unreleased
[DESCRIPTION]
This is the development changelog for the next Gecode release.

[ENTRY]
Module: driver
What: new
Rank: minor
Issue: 144
[DESCRIPTION]
Options support opt-in strict parsing through parse_strict(), which reports
unrecognised arguments, prints help, and exits with failure. The named methods
parse_strict() and parse_permissive() select the strictness of parse(). Existing
parsing calls retain their behaviour. Examples that accept only named options
use strict parsing.
Strict parsing will become the default in a future major release.

[ENTRY]
Module: example
What: bug
Rank: minor
[DESCRIPTION]
Fix duplicate registration of EFPA's symmetry option, which hid its other
command-line options.

[ENTRY]
Module: search
What: bug
Expand Down
2 changes: 1 addition & 1 deletion examples/alpha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ main(int argc, char* argv[]) {
opt.branching(Alpha::BRANCH_NONE, "none");
opt.branching(Alpha::BRANCH_INVERSE, "inverse");
opt.branching(Alpha::BRANCH_SIZE, "size");
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Alpha,DFS,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/archimedean-spiral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int main(int argc, char* argv[]) {
Options opt("ArchimedeanSpiral");
opt.solutions(0);
opt.step(0.1);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
FloatMaximizeScript::run<ArchimedeanSpiral,BAB,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/cartesian-heart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int main(int argc, char* argv[]) {
Options opt("CartesianHeart");
opt.solutions(0);
opt.step(0.01);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<CartesianHeart,BAB,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/colored-matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ColoredMatrixOptions : public Options {

/// Parse options from arguments \a argv (number is \a argc)
void parse(int& argc, char* argv[]) {
Options::parse(argc,argv);
Options::parse_strict(argc,argv);
}

/// Return height
Expand Down
2 changes: 1 addition & 1 deletion examples/common-pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ main(int argc, char* argv[]) {
opt.search(CommonPattern::SEARCH_DFS);
opt.search(CommonPattern::SEARCH_BAB,"bab","find a longest pattern");
opt.search(CommonPattern::SEARCH_DFS,"dfs","enumerate frequent patterns");
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
try {
opt.read();
} catch (const std::invalid_argument& error) {
Expand Down
2 changes: 1 addition & 1 deletion examples/crew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ int
main(int argc, char* argv[]) {
Options o("Crew");
o.iterations(100);
o.parse(argc,argv);
o.parse_strict(argc,argv);
Script::run<Crew,DFS,Options>(o);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/descartes-folium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int main(int argc, char* argv[]) {
Options opt("DescartesFolium");
opt.solutions(0);
opt.step(0.1);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
FloatMaximizeScript::run<DescartesFolium,BAB,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/donald.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ main(int argc, char* argv[]) {
opt.model(Donald::MODEL_CARRY, "carry", "use carry");
opt.solutions(0);
opt.iterations(1500);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Donald,DFS,Options>(opt);
return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions examples/efpa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class EFPAOptions : public Options {
add(_q);
add(_v);
add(_permutation);
add(_symmetry);

// Add permutation options
_permutation.add(true, "full" );
Expand All @@ -79,7 +78,7 @@ class EFPAOptions : public Options {
}
/// Parse options from arguments \a argv (number is \a argc)
void parse(int& argc, char* argv[]) {
Options::parse(argc,argv);
Options::parse_strict(argc,argv);
}
/// Get v, number of sequences
int v(void) const { return _v.value(); }
Expand Down
2 changes: 1 addition & 1 deletion examples/eq20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ main(int argc, char* argv[]) {
Options opt("Eq20");
opt.solutions(0);
opt.iterations(1000);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Eq20,DFS,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fifteen-puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ run(PuzzleOptions& opt) {
int
main(int argc, char* argv[]) {
PuzzleOptions opt;
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
if (opt.mode() == SM_SOLUTION) {
std::cout << "Initial board (seed " << opt.seed() << "):\n";
print_board(std::cout,initial_board(opt));
Expand Down
2 changes: 1 addition & 1 deletion examples/golden-spiral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ int main(int argc, char* argv[]) {
Options opt("GoldenSpiral");
opt.solutions(0);
opt.step(0.1);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
FloatMaximizeScript::run<GoldenSpiral,BAB,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/golf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ main(int argc, char* argv[]) {
opt.propagation(Golf::PROP_MIXED, "mixed", "Use set intersection cardinality and integer distinct for pair play constraints");
opt.ipl(IPL_DOM);
opt.solutions(1);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Golf,DFS,GolfOptions>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/grocery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int
main(int argc, char* argv[]) {
Options opt("Grocery");
opt.iterations(20);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Grocery,DFS,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hamming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Hamming : public Script {
int
main(int argc, char* argv[]) {
HammingOptions opt("Hamming",20,3,32);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Hamming,DFS,HammingOptions>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/money.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ main(int argc, char* argv[]) {
opt.model(Money::MODEL_CARRY, "carry", "use carry");
opt.solutions(0);
opt.iterations(20000);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<Money,DFS,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/open-route.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ main(int argc, char* argv[]) {
"Assign the current prefix before extending the route");
opt.branching(OpenRoute::BRANCH_LENGTH,"length",
"Choose the complete route length first");
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
Script::run<OpenRoute,DFS,Options>(opt);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/warehouses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ main(int argc, char* argv[]) {
opt.model(MODEL_LEXCOST, "lex", "use lexicographic cost");
opt.solutions(0);
opt.iterations(10);
opt.parse(argc,argv);
opt.parse_strict(argc,argv);
switch (opt.model()) {
case MODEL_SUMCOST:
IntMinimizeScript::run<SumCostWarehouses,BAB,Options>(opt);
Expand Down
20 changes: 20 additions & 0 deletions gecode/driver.hh
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,29 @@ namespace Gecode {
* The options are parsed from position one onwards until no more options
* are detected. After parsing, the parsed arguments have been removed.
*
* Strict parsing will become the default in a future major release.
* Use parse_permissive() to explicitly retain partial parsing.
*
*/
void parse(int& argc, char* argv[]);

/**
* \brief Parse options, optionally rejecting unrecognised arguments
*
* With \a strict set to false, this behaves like the two-argument
* overload. With \a strict set to true, any remaining argument causes
* an error message and help text to be printed, followed by exit with
* failure. Use strict parsing when no positional arguments or further
* parsing are expected. Help options are handled as in the two-argument
* overload.
*/
void parse(int& argc, char* argv[], bool strict);

/// Parse options with strict checking (equivalent to parse() with \c true)
void parse_strict(int& argc, char* argv[]);
/// Parse options partially (equivalent to parse() with \c false)
void parse_permissive(int& argc, char* argv[]);

/// Return name of script
const char* name(void) const;
/// Set name of script
Expand Down
11 changes: 11 additions & 0 deletions gecode/driver/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,17 @@ namespace Gecode {
return;
}

void
BaseOptions::parse(int& argc, char* argv[], bool strict) {
parse(argc,argv);
if (strict && (argc > 1)) {
std::cerr << "Unrecognised argument \"" << argv[1] << "\""
<< std::endl;
help();
exit(EXIT_FAILURE);
}
}

BaseOptions::~BaseOptions(void) {
Driver::BaseOption::strdel(_name);
}
Expand Down
9 changes: 9 additions & 0 deletions gecode/driver/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ namespace Gecode {
* Options
*
*/
inline void
BaseOptions::parse_strict(int& argc, char* argv[]) {
parse(argc,argv,true);
}
inline void
BaseOptions::parse_permissive(int& argc, char* argv[]) {
parse(argc,argv,false);
}

inline const char*
BaseOptions::name(void) const {
return _name;
Expand Down
50 changes: 50 additions & 0 deletions test/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,56 @@ namespace Test { namespace Instance {
return true;
});

/// Observe help requests without terminating the test runner
class HelpOptions : public Gecode::Options {
public:
struct HelpShown {};
HelpOptions(void) : Gecode::Options("Strict parsing test") {}
void help(void) override {
Gecode::Options::help();
throw HelpShown();
}
};

Case strict_options("Driver::Options::Strict", [](void) {
Gecode::Options options("Test");
char name[] = "test", iterations[] = "-iterations", value[] = "200";
char unknown[] = "--typo", help[] = "--help", positional[] = "instance.txt";
char* valid_argv[] = {name, iterations, value, nullptr};
int valid_argc = 3;
options.parse_strict(valid_argc, valid_argv);
CHECK(valid_argc == 1 && options.iterations() == 200);

// Both the original overload and explicit non-strict parsing leave
// arguments for callers to interpret.
char* remaining_argv[] = {name, unknown, positional, nullptr};
int remaining_argc = 3;
options.parse(remaining_argc, remaining_argv);
CHECK(remaining_argc == 3 && remaining_argv[1] == unknown);
options.parse_permissive(remaining_argc, remaining_argv);
CHECK(remaining_argc == 3 && remaining_argv[2] == positional);

HelpOptions strict;
char* invalid_argv[] = {name, iterations, value, unknown, help, nullptr};
int invalid_argc = 5;
bool help_shown = false;
std::ostringstream output;
std::streambuf* previous = std::cerr.rdbuf(output.rdbuf());
try {
strict.parse_strict(invalid_argc, invalid_argv);
} catch (const HelpOptions::HelpShown&) {
help_shown = true;
} catch (...) {
std::cerr.rdbuf(previous);
throw;
}
std::cerr.rdbuf(previous);
CHECK(help_shown);
CHECK(output.str().find("Unrecognised argument \"--typo\"") != std::string::npos);
CHECK(output.str().find("Options for Strict parsing test:") != std::string::npos);
return true;
});

Case options("Driver::InstanceOptions", [](void) {
// Unique names in the temporary directory
const char* tmp = std::getenv("TMPDIR");
Expand Down
Loading