//: Everything this project/binary supports. //: This should give you a sense for what to look forward to in later layers. :(before "End Commandline Parsing") if (argc <= 1 || is_equal(argv[1], "--help")) { //: this is the functionality later layers will provide // currently no automated tests for commandline arg parsing cerr << get(Help, "usage"); return 0; } //: Support for option parsing. //: Options always begin with '--' and are always the first arguments. An //: option will never follow a non-option. char** arg = &argv[1]; while (argc > 1 && starts_with(*arg, "--")) { if (false) ; // no-op branch just so any further additions can consistently always start with 'else' // End Commandline Options(*arg) else cerr << "skipping unknown option " << *arg << '\n'; --argc; ++argv; ++arg; } if (is_equal(argv[1], "help")) { if (argc == 2) { cerr << "help on what?\n"; help_contents(); return 0; } string key(argv[2]); // End Help Special-cases(key) if (contains_key(Help, key)) { cerr << get(Help, key); return 0; } else { cerr << "No help found for '" << key << "'\n"; help_contents(); cerr << "Please check your command for typos.\n"; return 1; } } :(code) void help_contents() { cerr << "Available top-level topics:\n"; cerr << " usage\n"; // End Help Contents } :(before "End Globals") map Help; :(before "End Includes") #include using std::map; :(before "End One-time Setup") init_help(); :(code) void init_help() { put(Help, "usage", "bootstrap: the bootstrap translator for SubX.\n" "This program also wraps some miscellaneous useful functionality:\n" " - an x86 emulator: `bootstrap run`\n" " - online help: `bootstrap help`\n" "\n" "== Ways to invoke bootstrap\n" "- See this message:\n" " bootstrap --help\n" "- Convert a textual SubX program into a standard ELF binary that you can\n" " run on your computer:\n" " bootstrap translate input1.subx input2.subx ... -o \n" "- Run a SubX binary using SubX itself (for better error messages):\n" " bootstrap run \n" "- Run all bootstrap's unit tests:\n" " bootstrap test\n" "- Run a single unit test:\n" " bootstrap test \n" " e.g. bootstrap test test_copy_imm32_to_EAX\n" "\n" "== Debugging aids\n" "- Add '--trace' to any of these commands to save a trace to disk at the end.\n" " This can run out of memory for long-running commands.\n" "- Add '--debug' to emit additional debug information during translation.\n" " 'bootstrap --debug translate' will save metadata to disk that\n" " 'bootstrap --trace run' uses to make traces more informative.\n" "\n" "Options starting with '--' must always come before any other arguments.\n" "\n" "To start learning how to write SubX programs, see Re
../ranger/help
l contains_key(T& map, typename T::key_type const& key) { return map.find(key) != map.end(); } template typename T::mapped_type& get_or_insert(T& map, typename T::key_type const& key) { return map[key]; } template typename T::mapped_type const& put_new(T& map, typename T::key_type const& key, typename T::mapped_type const& value) { assert(map.find(key) == map.end()); map[key] = value; return map[key]; } //: The contract: any container that relies on get_or_insert should never call //: contains_key. //: 7. istreams are a royal pain in the arse. You have to be careful about //: what subclass you try to putback into. You have to watch out for the pesky //: failbit and badbit. Just avoid eof() and use this helper instead. :(code) bool has_data(istream& in) { return in && !in.eof(); } :(before "End Includes") #include #include using std::istream; using std::ostream; using std::iostream; using std::cin; using std::cout; using std::cerr; #include #include #include using std::string; #include using std::min; using std::max;