Author: zaroth
Date: Thu Jun  9 11:52:15 2011
New Revision: 49797

URL: http://svn.gna.org/viewcvs/wesnoth?rev=49797&view=rev
Log:
Started fleshing out the new commandline_options class. Added support
for several simple switches and linked it inside game_controller and
parse_command_args().

Modified:
    trunk/src/CMakeLists.txt
    trunk/src/commandline_options.cpp
    trunk/src/commandline_options.hpp
    trunk/src/game.cpp
    trunk/src/game_controller.cpp
    trunk/src/game_controller.hpp

Modified: trunk/src/CMakeLists.txt
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/CMakeLists.txt?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/CMakeLists.txt (original)
+++ trunk/src/CMakeLists.txt Thu Jun  9 11:52:15 2011
@@ -70,6 +70,7 @@
                ${SDL_LIBRARY}
                ${Boost_IOSTREAMS_LIBRARY}
                ${Boost_REGEX_LIBRARY}
+               ${Boost_PROGRAM_OPTIONS_LIBRARY}
        )
 endif(MSVC)
 

Modified: trunk/src/commandline_options.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/commandline_options.cpp?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/commandline_options.cpp (original)
+++ trunk/src/commandline_options.cpp Thu Jun  9 11:52:15 2011
@@ -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 @@
        fullscreen(false),
        gunzip(),
        gzip(),
+       help(),
        log(),
        load(),
        logdomains(),
@@ -70,7 +73,43 @@
        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;
+}

Modified: trunk/src/commandline_options.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/commandline_options.hpp?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/commandline_options.hpp (original)
+++ trunk/src/commandline_options.hpp Thu Jun  9 11:52:15 2011
@@ -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 @@
        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 @@
        /// 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

Modified: trunk/src/game.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game.cpp?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/game.cpp (original)
+++ trunk/src/game.cpp Thu Jun  9 11:52:15 2011
@@ -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,10 +162,13 @@
 };
 
 /** 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;
@@ -174,7 +178,7 @@
                        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 @@
                        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 @@
 {
        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 @@
        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();

Modified: trunk/src/game_controller.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_controller.cpp?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/game_controller.cpp (original)
+++ trunk/src/game_controller.cpp Thu Jun  9 11:52:15 2011
@@ -66,10 +66,11 @@
        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_(),
@@ -115,16 +116,23 @@
 
        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,15 +277,10 @@
                        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.)
-            else if(val == "--proxy") {
+               }   //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.)
+        else if(val == "--proxy") {
             network::enable_connection_through_proxy();
         } else if(val == "--proxy-address") {
             if ( argv_[ arg_ + 1][0] != '-')
@@ -312,10 +315,7 @@
             }
             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;
                } else if(val == "-e" || val == "--editor") {
@@ -328,7 +328,7 @@
                        }
                } 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

Modified: trunk/src/game_controller.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/game_controller.hpp?rev=49797&r1=49796&r2=49797&view=diff
==============================================================================
--- trunk/src/game_controller.hpp (original)
+++ trunk/src/game_controller.hpp Thu Jun  9 11:52:15 2011
@@ -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 @@
 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 @@
        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.


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to