Browse Source

Command line parsing!

* Added MIT license.
    * Added cxxopts and gave it some basic things to parse.
    * Removed redundant "lib" from `libplip`.
master
Ian Burgmyer 4 years ago
parent
commit
9810258bb2
  1. 4
      CMakeLists.txt
  2. 19
      LICENSE
  3. 2104
      plip-sdl/cxxopts.hpp
  4. 88
      plip-sdl/main.cpp

4
CMakeLists.txt

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)
set(product_name "Plip")
set(gui_name "plip-sdl")
set(lib_name "libplip")
set(lib_name "plip")
project(${gui_name})
set(CMAKE_CXX_STANDARD 14)
@ -52,7 +52,7 @@ target_include_directories(${gui_name}
)
target_link_libraries(${gui_name}
libplip
plip
${SDL2_INCLUDE_DIRS}
)

19
LICENSE

@ -0,0 +1,19 @@
Copyright (c) 2020 Ian Burgmyer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2104
plip-sdl/cxxopts.hpp

File diff suppressed because it is too large Load Diff

88
plip-sdl/main.cpp

@ -1,8 +1,90 @@
/* main.cpp
*
* The big, magical entry point.
*/
#include <iostream>
#include "Hello.h"
#include "cxxopts.hpp"
#include "version.h"
cxxopts::ParseResult parseCmdLine(int argc, char **argv) {
try {
cxxopts::Options options(argv[0]);
options.positional_help("CORE FILENAME")
.show_positional_help();
options.add_options("Hidden")
("c,core", "the core that should be used", cxxopts::value<std::string>())
("f,filename", "the path to the ROM", cxxopts::value<std::string>())
("positional", "", cxxopts::value<std::vector<std::string>>())
;
options.add_options()
("h,help", "shows this help screen and exits")
("l,list-cores", "shows a list of all supported cores and exits")
("V,version", "displays the version information and exits");
options.add_options("Video")
("s,scale", "sets the default window scaling", cxxopts::value<int>()->default_value("1"));
options.parse_positional({"core", "filename", "positional"});
auto result = options.parse(argc, argv);
if(result.count("help")) {
// Icky hack. As far as I can tell there's no other way of hiding
// unwanted/positional arguments using cxxopts.
std::cout << options.help({
"",
"Video"
}) << std::endl;
exit(0);
}
return result;
} catch(cxxopts::argument_incorrect_type &ex) {
std::cerr << "Invalid argument type (" << ex.what() << ")" << std::endl;
exit(1);
} catch(cxxopts::missing_argument_exception &ex) {
std::cerr << ex.what() << std::endl;
exit(1);
} catch(cxxopts::OptionException &ex) {
std::cerr << "Error parsing arguments (" << ex.what() << ")" << std::endl;
exit(1);
}
}
int main(int argc, char **argv) {
#ifndef GIT_FOUND
const char *windowTitle = PRODUCT_NAME;
#else
#ifndef GIT_TAG
const char *windowTitle = PRODUCT_NAME " (" GIT_BRANCH "@" GIT_REVISION ")";
#else
const char *windowTitle = PRODUCT_NAME " " GIT_TAG;
#endif // GIT_TAG
#endif // GIT_FOUND
auto opts = parseCmdLine(argc, argv);
if(!opts.count("core") || !opts.count("filename")) {
std::cerr << "The name of the core and the filename must be specified!\n\n"
<< "Please see the usage information (" << argv[0] << " -h) for more information." << std::endl;
return 1;
}
if(opts.count("version")) {
std::cout << windowTitle << std::endl;
return 0;
}
std::cout << "scale: " << opts["scale"].as<int>()
<< "\ncore: " << opts["core"].as<std::string>()
<< "\nfile: " << opts["filename"].as<std::string>()
<< std::endl;
int main() {
std::cout << "Hello world! The answer is " << Hello::Calculate() << "." << std::endl;
return 0;
}

Loading…
Cancel
Save