Replace CheckArgs with app::AppOptions/base::ProgramOptions classes

This commit is contained in:
David Capello 2012-09-05 21:14:42 -03:00
parent 6051387211
commit 3c1ee303c6
8 changed files with 193 additions and 303 deletions

View File

@ -155,7 +155,6 @@ set(all_libs ${aseprite_libraries} ${libs3rdparty} ${sys_libs})
add_library(aseprite-library
app.cpp
app_menus.cpp
check_args.cpp
console.cpp
context.cpp
context_flags.cpp
@ -180,6 +179,7 @@ add_library(aseprite-library
ui_context.cpp
undo_transaction.cpp
xml_exception.cpp
app/app_options.cpp
app/backup.cpp
app/check_update.cpp
app/color.cpp

View File

@ -20,6 +20,7 @@
#include "app.h"
#include "app/app_options.h"
#include "app/check_update.h"
#include "app/color_utils.h"
#include "app/data_recovery.h"
@ -27,7 +28,6 @@
#include "app/load_widget.h"
#include "base/exception.h"
#include "base/unique_ptr.h"
#include "check_args.h"
#include "commands/commands.h"
#include "commands/params.h"
#include "console.h"
@ -82,7 +82,6 @@ class App::Modules
{
public:
ConfigModule m_configModule;
CheckArgs m_checkArgs;
LoggerModule m_loggerModule;
FileSystemModule m_file_system_module;
tools::ToolBox m_toolbox;
@ -91,20 +90,17 @@ public:
RecentFiles m_recent_files;
app::DataRecovery m_recovery;
Modules(const std::vector<base::string>& args)
: m_checkArgs(args)
, m_loggerModule(m_checkArgs.isVerbose())
Modules(bool verbose)
: m_loggerModule(verbose)
, m_recovery(&m_ui_context) {
}
};
App* App::m_instance = NULL;
static char *palette_filename = NULL;
// Initializes the application loading the modules, setting the
// graphics mode, loading the configuration and resources, etc.
App::App(int argc, char* argv[])
App::App(int argc, const char* argv[])
: m_modules(NULL)
, m_legacy(NULL)
, m_isGui(false)
@ -112,12 +108,12 @@ App::App(int argc, char* argv[])
ASSERT(m_instance == NULL);
m_instance = this;
for (int i = 0; i < argc; ++i)
m_args.push_back(argv[i]);
app::AppOptions options(argc, argv);
m_modules = new Modules(m_args);
m_isGui = !(m_modules->m_checkArgs.isConsoleOnly());
m_modules = new Modules(options.verbose());
m_isGui = options.startUI();
m_legacy = new LegacyModules(isGui() ? REQUIRE_INTERFACE: 0);
m_files = options.files();
// Register well-known image file types.
FileFormatsManager::instance().registerAllFormats();
@ -128,14 +124,14 @@ App::App(int argc, char* argv[])
// Load RenderEngine configuration
RenderEngine::loadConfig();
/* custom default palette? */
if (palette_filename) {
PRINTF("Loading custom palette file: %s\n", palette_filename);
// Default palette.
if (!options.paletteFileName().empty()) {
const char* palFile = options.paletteFileName().c_str();
PRINTF("Loading custom palette file: %s\n", palFile);
UniquePtr<Palette> pal(Palette::load(palette_filename));
UniquePtr<Palette> pal(Palette::load(palFile));
if (pal.get() == NULL)
throw base::Exception("Error loading default palette from: %s",
static_cast<const char*>(palette_filename));
throw base::Exception("Error loading default palette from: %s", palFile);
set_default_palette(pal.get());
}
@ -173,20 +169,15 @@ int App::run()
{
Console console;
for (CheckArgs::iterator
it = m_modules->m_checkArgs.begin(),
end = m_modules->m_checkArgs.end();
for (FileList::iterator
it = m_files.begin(),
end = m_files.end();
it != end; ++it) {
CheckArgs::Option* option = *it;
switch (option->type()) {
case CheckArgs::Option::OpenSprite: {
// Load the sprite
Document* document = load_document(option->data().c_str());
Document* document = load_document(it->c_str());
if (!document) {
if (!isGui())
console.printf("Error loading file \"%s\"\n", option->data().c_str());
console.printf("Error loading file \"%s\"\n", it->c_str());
}
else {
// Mount and select the sprite
@ -199,10 +190,7 @@ int App::run()
set_document_in_more_reliable_editor(context->getFirstDocument());
// Recent file
getRecentFiles()->addRecentFile(option->data().c_str());
}
}
break;
getRecentFiles()->addRecentFile(it->c_str());
}
}
}

View File

@ -44,16 +44,11 @@ namespace tools { class ToolBox; }
class App
{
public:
App(int argc, char* argv[]);
App(int argc, const char* argv[]);
~App();
static App* instance() { return m_instance; }
// Functions to get the arguments specified in the command line.
int getArgc() const { return m_args.size(); }
const base::string& getArgv(int i) { return m_args[i]; }
const std::vector<base::string>& getArgs() const { return m_args; }
// Returns true if ASEPRITE is running with GUI available.
bool isGui() const { return m_isGui; }
@ -73,6 +68,7 @@ public:
Signal0<void> CurrentToolChange;
private:
typedef std::vector<base::string> FileList;
class Modules;
static App* m_instance;
@ -80,8 +76,8 @@ private:
Modules* m_modules;
LegacyModules* m_legacy;
bool m_isGui;
std::vector<base::string> m_args;
UniquePtr<MainWindow> m_mainWindow;
FileList m_files;
};
void app_refresh_screen(const Document* document);

80
src/app/app_options.cpp Normal file
View File

@ -0,0 +1,80 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "app/app_options.h"
#include "base/path.h"
#include <iostream>
namespace app {
typedef base::ProgramOptions::Option Option;
AppOptions::AppOptions(int argc, const char* argv[])
: m_exeName(base::get_file_name(argv[0]))
, m_startUI(true)
, m_verbose(false)
{
Option& palette = m_po.add("palette").requiresValue("GFXFILE").description("Use a specific palette by default");
Option& verbose = m_po.add("verbose").description("Explain what is being done (in stderr or a log file)");
Option& help = m_po.add("help").mnemonic('?').description("Display this help and exits");
Option& version = m_po.add("version").description("Output version information and exit");
try {
m_po.parse(argc, argv);
m_verbose = verbose.enabled();
m_paletteFileName = palette.value();
if (help.enabled()) {
showHelp();
m_startUI = false;
}
else if (version.enabled()) {
showVersion();
m_startUI = false;
}
}
catch (const std::runtime_error& parseError) {
std::cerr << m_exeName << ": " << parseError.what() << '\n'
<< "Try \"" << m_exeName << " --help\" for more information.\n";
m_startUI = false;
}
}
void AppOptions::showHelp()
{
std::cout
<< PACKAGE << " v" << VERSION << " | A pixel art program\n" << COPYRIGHT
<< "\n\nUsage:\n"
<< " " << m_exeName << " [OPTIONS] [FILES]...\n\n"
<< "Options:\n"
<< m_po
<< "\nFind more information in " << PACKAGE
<< " web site: " << WEBSITE << "\n\n";
}
void AppOptions::showVersion()
{
std::cout << PACKAGE << ' ' << VERSION << '\n';
}
}

56
src/app/app_options.h Normal file
View File

@ -0,0 +1,56 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef APP_APP_OPTIONS_H_INCLUDED
#define APP_APP_OPTIONS_H_INCLUDED
#include <stdexcept>
#include <string>
#include <vector>
#include "base/program_options.h"
namespace app {
class AppOptions {
public:
AppOptions(int argc, const char* argv[]);
bool startUI() const { return m_startUI; }
bool verbose() const { return m_verbose; }
const std::string& paletteFileName() const { return m_paletteFileName; }
const base::ProgramOptions::ValueList& files() const {
return m_po.values();
}
private:
void showHelp();
void showVersion();
std::string m_exeName;
base::ProgramOptions m_po;
bool m_startUI;
bool m_verbose;
std::string m_paletteFileName;
};
}
#endif

View File

@ -1,164 +0,0 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "check_args.h"
#include "base/convert_to.h"
#include "base/split_string.h"
#include "base/string.h"
#include "console.h"
#include "ini_file.h"
CheckArgs::CheckArgs(const std::vector<base::string>& args)
: m_consoleOnly(false)
, m_verbose(false)
{
// Exe name
m_exeName = args[0];
// Convert arguments to recognized options
Console console;
size_t i, n, len;
for (i=1; i<args.size(); i++) {
const base::string& arg(args[i]);
for (n=0; arg[n] == '-'; n++);
len = arg.size()-n;
// Option
if ((n > 0) && (len > 0)) {
base::string option = arg.substr(n);
// Use other palette file
if (option == "palette") {
if (++i < args.size())
m_paletteFilename = args[i];
else
usage(false);
}
// Video resolution
else if (option == "resolution") {
if (++i < args.size()) {
// The following argument should indicate the resolution
// in a format like: 320x240[x8]
std::vector<base::string> parts;
base::split_string(args[i], parts, "x");
switch (parts.size()) {
case 1:
set_config_int("GfxMode", "Depth", base::convert_to<int>(parts[0]));
break;
case 2:
case 3:
set_config_int("GfxMode", "Width", base::convert_to<int>(parts[0]));
set_config_int("GfxMode", "Height", base::convert_to<int>(parts[1]));
if (parts.size() == 3)
set_config_int("GfxMode", "Depth", base::convert_to<int>(parts[2]));
break;
default:
usage(false);
break;
}
}
else {
console.printf("%s: option \"res\" requires an argument\n",
m_exeName.c_str());
usage(false);
}
}
// Verbose mode
else if (option == "verbose") {
m_verbose = true;
}
// Show help
else if (option == "help") {
usage(true);
}
// Show version
else if (option == "version") {
m_consoleOnly = true;
console.printf("%s %s\n", PACKAGE, VERSION);
}
// Invalid argument
else {
usage(false);
}
}
// Graphic file to open
else if (n == 0) {
m_options.push_back(new Option(Option::OpenSprite, args[i]));
}
}
}
CheckArgs::~CheckArgs()
{
clear();
}
void CheckArgs::clear()
{
for (iterator it = begin(); it != end(); ++it) {
Option* option = *it;
delete option;
}
m_options.clear();
}
// Shows the available options for the program
void CheckArgs::usage(bool showHelp)
{
Console console;
// Activate this flag so the GUI is not initialized by the App::run().
m_consoleOnly = true;
// Show options
if (showHelp) {
// Copyright
console.printf
("%s v%s | Allegro Sprite Editor | A pixel art program\n%s\n\n",
PACKAGE, VERSION, COPYRIGHT);
// Usage
console.printf("Usage\n %s [OPTION] [FILE]...\n\n", m_exeName.c_str());
// Available Options
console.printf
("Options:\n"
" -palette GFX-FILE Use a specific palette by default\n"
" -resolution WxH[xBPP] Change the resolution to use\n"
" -verbose Explain what is being done (in stderr or a log file)\n"
" -help Display this help and exits\n"
" -version Output version information and exit\n"
"\n");
// Web-site
console.printf
("Find more information in %s web site: %s\n\n",
PACKAGE, WEBSITE);
}
// How to show options
else {
console.printf("Try \"%s --help\" for more information.\n",
m_exeName.c_str());
}
}

View File

@ -1,73 +0,0 @@
/* ASEPRITE
* Copyright (C) 2001-2012 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef CHECK_ARGS_H_INCLUDED
#define CHECK_ARGS_H_INCLUDED
#include <vector>
#include "base/string.h"
// Parses the input arguments in the command line.
class CheckArgs
{
public:
// Represents one specifial option specified in the parameters that
// need some special operation by the application class.
class Option
{
int m_type;
base::string m_data;
public:
enum {
OpenSprite,
};
Option(int type, const base::string& data) : m_type(type), m_data(data) { }
int type() const { return m_type; }
const base::string& data() const { return m_data; }
};
typedef std::vector<Option*> Options;
typedef Options::iterator iterator;
CheckArgs(const std::vector<base::string>& args);
~CheckArgs();
void clear();
iterator begin() { return m_options.begin(); }
iterator end() { return m_options.end(); }
base::string getPaletteFilename() const { return m_paletteFilename; }
bool isConsoleOnly() const { return m_consoleOnly; }
bool isVerbose() const { return m_verbose; }
private:
void usage(bool showHelp);
Options m_options;
base::string m_paletteFilename;
base::string m_exeName;
bool m_consoleOnly;
bool m_verbose;
};
#endif

View File

@ -30,6 +30,7 @@
#include <cstdlib>
#include <ctime>
#include <iostream>
#ifdef WIN32
#include <windows.h>
@ -82,11 +83,12 @@ int app_main(int argc, char* argv[])
CoInitialize(NULL);
#endif
try {
base::MemoryDump memoryDump;
she::ScopedHandle<she::System> system(she::CreateSystem());
MemLeak memleak;
ui::GuiSystem guiSystem;
App app(argc, argv);
App app(argc, const_cast<const char**>(argv));
scripting::Engine scriptingEngine;
// Change the name of the memory dump file
@ -98,3 +100,8 @@ int app_main(int argc, char* argv[])
return app.run();
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}