Started fleshing out the new commandline_options class.

Added support for several simple switches and linked it inside
game_controller and parse_command_args().
This commit is contained in:
Lukasz Dobrogowski 2011-06-09 09:52:15 +00:00
parent 041ce87a7a
commit dd2bdaaf9a
6 changed files with 81 additions and 28 deletions

View File

@ -70,6 +70,7 @@ else(MSVC)
${SDL_LIBRARY}
${Boost_IOSTREAMS_LIBRARY}
${Boost_REGEX_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
)
endif(MSVC)

View File

@ -15,7 +15,9 @@
#include "commandline_options.hpp"
commandline_options::commandline_options ( int /* argc*/, char** /*argv*/ ) :
namespace po = boost::program_options;
commandline_options::commandline_options ( int argc, char** argv ) :
bpp(),
campaign(),
campaign_difficulty(),
@ -34,6 +36,7 @@ commandline_options::commandline_options ( int /* argc*/, char** /*argv*/ ) :
fullscreen(false),
gunzip(),
gzip(),
help(),
log(),
load(),
logdomains(),
@ -70,7 +73,43 @@ commandline_options::commandline_options ( int /* argc*/, char** /*argv*/ ) :
validcache(false),
version(false),
windowed(false),
with_replay(false)
with_replay(false),
argc_(argc),
argv_(argv),
all_(),
visible_(),
hidden_()
{
po::options_description general("General options");
general.add_options()
("data-dir", po::value<std::string>(), "overrides the data directory with the one specified.")
("new-syntax", "enables the new campaign syntax parsing.")
("help,h", "prints this message and exits.")
;
hidden_.add_options()
("new-widgets", "")
("new_storyscreens", "")
;
visible_.add(general);
all_.add(visible_).add(hidden_);
po::variables_map vm;
po::store(po::parse_command_line(argc_,argv_,all_),vm);
if (vm.count("help"))
help = true;
if (vm.count("new-widgets"))
new_widgets = true;
if (vm.count("new-storyscreens"))
new_storyscreens = true;
}
std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts)
{
os << "Usage:\n";
os << cmdline_opts.visible_;
return os;
}

View File

@ -25,6 +25,7 @@
class commandline_options
{
friend std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts);
public:
commandline_options(int argc, char **argv);
@ -62,6 +63,8 @@ public:
boost::optional<std::string> gunzip;
/// Non-empty if --gzip was given on the command line. Compresses a file to .gz and exits.
boost::optional<std::string> gzip;
/// True if --help was given on the command line. Prints help and exits.
bool help;
/// Contains parsed arguments of --log-* (e.g. --log-debug).
/// Vector of pairs (severity, log domain).
boost::optional<std::vector<std::pair<int, std::string> > > log;
@ -138,6 +141,11 @@ public:
/// True if --with-replay was given on the command line. Shows replay of the loaded file.
bool with_replay;
private:
int argc_;
char **argv_;
boost::program_options::options_description all_;
boost::program_options::options_description visible_;
boost::program_options::options_description hidden_;
};
#endif

View File

@ -20,6 +20,7 @@
#include "about.hpp"
#include "addon/manager.hpp"
#include "commandline_options.hpp"
//#include "ai/configuration.hpp"
//#include "config.hpp"
//#include "config_cache.hpp"
@ -161,11 +162,14 @@ public:
};
/** Process commandline-arguments */
static int process_command_args(int argc, char** argv) {
static int process_command_args(int argc, char** argv, const commandline_options& cmdline_opts) {
const std::string program = argv[0];
game_config::wesnoth_program_dir = directory_name(program);
preprocess_options preproc;
if (cmdline_opts.new_syntax)
game_config::new_syntax = true;
//parse arguments that shouldn't require a display device
int arg;
for(arg = 1; arg != argc; ++arg) {
@ -174,7 +178,7 @@ static int process_command_args(int argc, char** argv) {
continue;
}
if(val == "--help" || val == "-h") {
if(cmdline_opts.help) {
// When adding items don't forget to update doc/man/wesnoth.6
// Options are sorted alphabetically by --long-option.
// Please keep the output to 80 chars per line.
@ -318,8 +322,6 @@ static int process_command_args(int argc, char** argv) {
std::cout << "Battle for Wesnoth" << " " << game_config::version
<< "\n";
return 0;
} else if (val == "--new-syntax") {
game_config::new_syntax = true;
} else if (val == "--config-path") {
std::cout << get_user_data_dir() << '\n';
return 0;
@ -614,7 +616,8 @@ static int do_gameloop(int argc, char** argv)
{
srand(time(NULL));
int finished = process_command_args(argc, argv);
commandline_options cmdline_opts = commandline_options(argc,argv);
int finished = process_command_args(argc, argv,cmdline_opts);
if(finished != -1) {
return finished;
}
@ -626,7 +629,7 @@ static int do_gameloop(int argc, char** argv)
if (game_config::new_syntax)
game = boost::shared_ptr<game_controller_abstract>(new game_controller_new());
else
game = boost::shared_ptr<game_controller_abstract>(new game_controller(argc,argv));
game = boost::shared_ptr<game_controller_abstract>(new game_controller(argc,argv,cmdline_opts));
const int start_ticks = SDL_GetTicks();
init_locale();

View File

@ -66,10 +66,11 @@ static bool less_campaigns_rank(const config &a, const config &b) {
return a["rank"].to_int(1000) < b["rank"].to_int(1000);
}
game_controller::game_controller(int argc, char** argv) :
game_controller::game_controller(int argc, char** argv, const commandline_options& cmdline_opts) :
argc_(argc),
arg_(1),
argv_(argv),
cmdline_opts_(cmdline_opts),
thread_manager(),
font_manager_(),
prefs_manager_(),
@ -116,15 +117,22 @@ game_controller::game_controller(int argc, char** argv) :
const std::string app_basename = file_name(argv[0]);
jump_to_editor_ = app_basename.find("editor") != std::string::npos;
if(cmdline_opts_.fps)
preferences::set_show_fps(true);
if(cmdline_opts_.new_storyscreens)
// This is a hidden option to help testing
// the work-in-progress new storyscreen code.
// Don't document.
set_new_storyscreen(true);
if(cmdline_opts_.new_widgets)
gui2::new_widgets = true;
for(arg_ = 1; arg_ != argc_; ++arg_) {
const std::string val(argv_[arg_]);
if(val.empty()) {
continue;
}
if(val == "--fps") {
preferences::set_show_fps(true);
} else if(val == "--nocache") {
else if(val == "--nocache") {
cache_.set_use_cache(false);
} else if(val == "--max-fps") {
if(arg_+1 != argc_) {
@ -269,11 +277,6 @@ game_controller::game_controller(int argc, char** argv) :
no_sound = true;
} else if(val == "--nomusic") {
no_music = true;
} else if(val == "--new-storyscreens") {
// This is a hidden option to help testing
// the work-in-progress new storyscreen code.
// Don't document.
set_new_storyscreen(true);
} //These commented lines should be used to implement support of connection
//through a proxy via command line options.
//The ANA network module should implement these methods (while the SDL_net won't.)
@ -312,9 +315,6 @@ game_controller::game_controller(int argc, char** argv) :
}
else
throw std::runtime_error("Proxy password option requires password");
} else if(val == "--new-widgets") {
// This is a hidden option to enable the new widget toolkit.
gui2::new_widgets = true;
}
else if(val == "--clock") {
gui2::show_debug_clock_button = true;
@ -328,7 +328,7 @@ game_controller::game_controller(int argc, char** argv) :
}
} else if(val[0] == '-') {
std::cerr << "unknown option: " << val << std::endl;
throw config::error("unknown option");
//throw config::error("unknown option"); TODO will be unnecessary here once commandline_options is completed
} else {
std::cerr << "Overriding data directory with " << val << std::endl;
#ifdef _WIN32

View File

@ -17,6 +17,7 @@
#include "game_controller_abstract.hpp"
#include "commandline_options.hpp"
#include "config_cache.hpp"
#include "filesystem.hpp"
#include "gamestatus.hpp"
@ -45,7 +46,7 @@ public:
class game_controller : public game_controller_abstract
{
public:
game_controller(int argc, char** argv);
game_controller(int argc, char** argv, const commandline_options& cmdline_opts);
~game_controller();
bool init_config() { return init_config(false); }
@ -96,6 +97,7 @@ private:
const int argc_;
int arg_;
const char* const * const argv_;
const commandline_options& cmdline_opts_;
//this should get destroyed *after* the video, since we want
//to clean up threads after the display disappears.